$ cat /etc/my.cnf.d/client.cnf # ## These two groups are read by the client library ## Use it for options that affect all clients, but not the server #
## This group is not read by mysql client library, ## If you use the same .cnf file for MySQL and MariaDB, ## use it for MariaDB-only client options [client-mariadb]
1 2 3 4 5 6 7 8 9 10 11
$ cat /etc/my.cnf.d/mysql-clients.cnf # ## These groups are read by MariaDB command-line tools ## Use it for options that affect only one utility #
$ cat /etc/my.cnf.d/mariadb-server.cnf # ## These groups are read by MariaDB server. ## Use it for options that only the server (but not clients) should see # ## See the examples of server my.cnf files in /usr/share/mysql/ #
## this is read by the standalone daemon and embedded servers [server]
## this is only for the mysqld standalone daemon ## Settings user and group are ignored when systemd is used. ## If you need to run mysqld under a different user or group, ## customize your systemd unit file for mysqld/mariadb according to the ## instructions in http://fedoraproject.org/wiki/Systemd [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock log-error=/var/log/mariadb/mariadb.log pid-file=/run/mariadb/mariadb.pid ## 新增以下字符集的配置 collation-server = utf8_general_ci init-connect = 'SET NAMES utf8' character-set-server = utf8 sql-mode = TRADITIONAL
3.测试
3.1 启动/关闭
1
systemctl start mariadb
看看状态
1
systemctl status mariadb
显示如下,那就是正常运行中的
1 2 3 4 5 6 7 8 9 10 11 12 13 14
● mariadb.service - MariaDB 10.3 database server Loaded: loaded (/usr/lib/systemd/system/mariadb.service; disabled; vendor preset: disabled) Active: active (running) since Tue 2023-04-04 14:10:00 CST; 2 days ago Docs: man:mysqld(8) https://mariadb.com/kb/en/library/systemd/ Process: 633453 ExecStartPost=/usr/libexec/mysql-check-upgrade (code=exited, status=0/SUCCESS) Process: 633289 ExecStartPre=/usr/libexec/mysql-prepare-db-dir mariadb.service (code=exited, status=0/SUCCESS) Process: 633265 ExecStartPre=/usr/libexec/mysql-check-socket (code=exited, status=0/SUCCESS) Main PID: 633422 (mysqld) Status: "Taking your SQL requests now..." Tasks: 31 (limit: 75691) Memory: 67.4M CGroup: /system.slice/mariadb.service └─633422 /usr/libexec/mysqld --basedir=/usr
❯ netstat -nltp | grep 3306 (Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.) tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN -
## ls -al /etc/yum.repos.d/ total 100 drwxr-xr-x 1 root root 4096 Apr 7 16:12 . drwxr-xr-x 1 root root 4096 Apr 6 19:14 .. -rw-r--r-- 1 root root 2590 Feb 24 08:08 CentOS-Base.repo -rw-r--r-- 1 root root 966 Feb 24 08:00 CentOS-Linux-AppStream.repo -rw-r--r-- 1 root root 710 Feb 24 07:56 CentOS-Linux-BaseOS.repo -rw-r--r-- 1 root root 1136 Feb 24 07:56 CentOS-Linux-ContinuousRelease.repo -rw-r--r-- 1 root root 318 Feb 24 07:56 CentOS-Linux-Debuginfo.repo -rw-r--r-- 1 root root 738 Feb 24 07:56 CentOS-Linux-Devel.repo -rw-r--r-- 1 root root 710 Feb 24 07:56 CentOS-Linux-Extras.repo -rw-r--r-- 1 root root 725 Feb 24 07:56 CentOS-Linux-FastTrack.repo -rw-r--r-- 1 root root 746 Feb 24 07:56 CentOS-Linux-HighAvailability.repo -rw-r--r-- 1 root root 693 Feb 24 07:56 CentOS-Linux-Media.repo -rw-r--r-- 1 root root 712 Feb 24 07:56 CentOS-Linux-Plus.repo -rw-r--r-- 1 root root 730 Feb 24 07:56 CentOS-Linux-PowerTools.repo -rw-r--r-- 1 root root 1124 Feb 24 07:56 CentOS-Linux-Sources.repo -rw-r--r-- 1 root root 0 Feb 24 08:05 epel-8.repo -rw-r--r-- 1 root root 1698 Oct 4 2022 epel-modular.repo -rw-r--r-- 1 root root 1332 Oct 4 2022 epel.repo -rw-r--r-- 1 root root 1417 Jun 8 2021 epel.repo.rpmnew -rw-r--r-- 1 root root 2318 Feb 24 08:08 epel.repo.rpmsave -rw-r--r-- 1 root root 1797 Oct 4 2022 epel-testing-modular.repo -rw-r--r-- 1 root root 1431 Oct 4 2022 epel-testing.repo -rw-r--r-- 1 root root 1265 Jan 10 2022 mysql-community.repo -rw-r--r-- 1 root root 1321 Jan 10 2022 mysql-community-source.repo -rw-r--r-- 1 root root 164 Feb 27 12:21 vscode.repo
这时候,yum源就已经安装成功了
1
yum list | grep mysql
执行后,可以看到,列在最前面的就是el8-3,即为刚刚我们添加的的yum源中的mysql版本
1 2 3 4 5 6
Repository AppStream is listed more than once in the configuration Repository extras is listed more than once in the configuration mysql80-community-release.noarch el8-3 @System anope-mysql.x86_64 2.0.12-1.el8 epel ansible-collection-community-mysql.noarch 3.5.1-1.el8 epel apr-util-mysql.x86_64 1.6.1-6.el8 AppStream
## systemctl start mysqld Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalctl -xe"for details.
查看报错信息也看不出个所以然,看上去MySQL5.7版本在自动升级为MySQL8?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
## systemctl status mysqld.service ● mysqld.service - MySQL 8.0 database server Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor preset: disabled) Active: failed (Result: exit-code) since Fri 2023-04-07 16:39:51 CST; 7s ago Process: 648073 ExecStopPost=/usr/libexec/mysql-wait-stop (code=exited, status=0/SUCCESS) Process: 648059 ExecStart=/usr/libexec/mysqld --basedir=/usr (code=exited, status=1/FAILURE) Process: 648023 ExecStartPre=/usr/libexec/mysql-prepare-db-dir mysqld.service (code=exited, status=0/SUCCESS) Process: 647999 ExecStartPre=/usr/libexec/mysql-check-socket (code=exited, status=0/SUCCESS) Main PID: 648059 (code=exited, status=1/FAILURE) Status: "Data Dictionary upgrade from MySQL 5.7 in progress"
Apr 07 16:39:50 1c2261732150 systemd[1]: Starting MySQL 8.0 database server... Apr 07 16:39:51 1c2261732150 systemd[1]: mysqld.service: Main process exited, code=exited, status=1/FAILURE Apr 07 16:39:51 1c2261732150 systemd[1]: mysqld.service: Failed with result 'exit-code'. Apr 07 16:39:51 1c2261732150 systemd[1]: Failed to start MySQL 8.0 database server.
❯ sudo docker exec -it mysql57 mysql -uroot -p123456 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.44 MySQL Community Server (GPL)
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.
Type 'help;' or '\h'forhelp. Type '\c' to clear the current input statement.
bash-4.2# mysql -uroot -p123456 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 6 Server version: 5.7.44 MySQL Community Server (GPL)
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use test; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A
Database changed mysql> insert into stu values (4,'小明'); ERROR 1366 (HY000): Incorrect string value: '\xE5\xB0\x8F\xE6\x98\x8E' for column 'name' at row 1
mysql> SHOW VARIABLES LIKE 'character_set_database'; +------------------------+--------+ | Variable_name | Value | +------------------------+--------+ | character_set_database | latin1 | +------------------------+--------+ 1 row in set (0.00 sec)
使用如下命令设置这个数据库test和表stu的字符集为uft8;
1 2
ALTER DATABASE 数据库名字 CHARACTERSET utf8mb4; ALTERTABLE 表名字 CONVERTTOCHARACTERSET utf8mb4;
效果如下
1 2 3 4 5 6
mysql> ALTER DATABASE test CHARACTER SET utf8mb4; Query OK, 1 row affected (0.00 sec)
mysql> ALTER TABLE stu CONVERT TO CHARACTER SET utf8mb4; Query OK, 3 rows affected (0.06 sec) Records: 3 Duplicates: 0 Warnings: 0
此时就能正常插入中文了
1 2 3 4 5 6 7 8 9 10 11 12 13
mysql> insert into stu values (4,'小明'); Query OK, 1 row affected (0.03 sec)
mysql> select * from stu; +------+--------+ | id | name | +------+--------+ | 1 | 1235 | | 2 | 125 | | 3 | | | 4 | 小明 | +------+--------+ 4 rows in set (0.00 sec)