1.卸载原有MariaDB
[root@node01 ~]# rpm -qa | grep mariadb
mariadb-libs-5.5.68-1.el7.x86_64
[root@node01 ~]#
[root@node01 ~]# rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64
注意:在卸载mariadb依赖包时避免使用yum remove方式,该卸载方式会导致卸载掉很多额外的依赖包,可能会影响到OS其它服务的正常使用,建议使用rpm -e --nodeps mysql-community-libs方式卸载。
2.删除配置文件及数据目录,保证MariaDB完全的卸载
[root@node01 ~]# rm -rf /var/lib/mysql
[root@node01 ~]# rm -f /etc/my.cnf
3.下载MySQL的Tar安装包
https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.22-el7-x86_64.tar.gz
4.在OS上创建mysql用户
检查mysql用户和组是否存在,由于已经存在,所以不用再创建。
如果不存在则创建,命令为:
[root@node01 ~]# useradd mysql
1.解压下载好的mysql-5.7.22-el7-x86_64.tar.gz压缩包到/opt/soft目录下
[root@node01 package]# tar -zxvf mysql-5.7.22-el7-x86_64.tar.gz -C /opt/soft
[root@node01 soft]# mv mysql-5.7.22-el7-x86_64 mysql
2.创建MySQL数据目录并修改目录属主
[root@node01 mysql]# mkdir -p /opt/soft/mysql/data
3.创建MySQL日志目录,并修改目录属主
[root@node01 mysql]# mkdir -p /opt/soft/mysql/log
[root@node01 mysql]# chown -R mysql:mysql /opt/soft/mysql/
[root@node01 mysql]# chmod -R 755 /opt/soft/mysql
[root@node01 mysql]# ll
total 36
drwxr-xr-x 2 mysql mysql 4096 Jun 2 13:28 bin
-rwxr-xr-x 1 mysql mysql 17987 Mar 4 2018 COPYING
drwxr-xr-x 2 mysql mysql 6 Jun 2 13:31 data
drwxr-xr-x 2 mysql mysql 55 Jun 2 13:28 docs
drwxr-xr-x 3 mysql mysql 4096 Jun 2 13:27 include
drwxr-xr-x 5 mysql mysql 229 Jun 2 13:28 lib
drwxr-xr-x 2 mysql mysql 6 Jun 2 13:34 log
drwxr-xr-x 4 mysql mysql 30 Jun 2 13:28 man
-rwxr-xr-x 1 mysql mysql 2478 Mar 4 2018 README
drwxr-xr-x 28 mysql mysql 4096 Jun 2 13:28 share
drwxr-xr-x 2 mysql mysql 90 Jun 2 13:28 support-files
4.进入到MySQL安装目录的bin目录下,执行如下命令初始化MySQL数据库
[root@node01 bin]# ./mysqld --initialize --user=mysql --basedir=/opt/soft/mysql/ --datadir=/opt/soft/mysql/data
2022-06-02T05:51:22.421186Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-06-02T05:51:22.532843Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-06-02T05:51:22.552560Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-06-02T05:51:22.608982Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 083fd512-e238-11ec-a9df-000c295b2b8a.
2022-06-02T05:51:22.609859Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-06-02T05:51:22.611712Z 1 [Note] A temporary password is generated for root@localhost: tsXj?0C?i5MR
初始化成功后,在上图中最后一行可以看到,显示了生成的**默认密码, **需要记录下该密码,在初次登录MySQL时需要使用该密码。
5.修改MySQL配置文件/etc/my.cnf(MySQL服务启动默认加载的是该配置文件)
vim /etc/my.cnf
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8
socket=/opt/soft/mysql/mysql.sock
[mysqld]
#设置3306端口
port = 3306
# 设置mysql的安装目录
basedir=/opt/soft/mysql/
# 设置mysql数据库的数据的存放目录
datadir=/opt/soft/mysql/data
# 允许最大连接数
max_connections=1000
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
max_allowed_packet=16M
socket=/opt/soft/mysql/mysql.sock
log-bin=mysql-bin
server-id=36
binlog_format=MIXED
log_bin_trust_function_creators = 1
#慢日志位置
slow_query_log_file=/opt/soft/mysql/log/slow_query.log
#慢日志时间
long_query_time=1
#开启慢日志
slow_query_log=TRUE
[client]
socket=/opt/soft/mysql/mysql.sock
注意:根据自己MySQL的安装目录、数据目录及日志目录进行相应的修改。
修改my.cnf文件的权限、用户及属组
[root@node01 mysql]# chmod 755 /etc/my.cnf
[root@node01 mysql]# chown mysql:mysql /etc/my.cnf
6.将MySQL服务添加到系统自启动服务列表中
[root@node01 mysql]# cp /opt/soft/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@node01 mysql]# chmod +x /etc/init.d/mysqld
1.设置MySQL开机自启动, 启动MySQL并查看状态
[root@node01 mysql]# systemctl enable mysqld
mysqld.service is not a native service, redirecting to /sbin/chkconfig.
Executing /sbin/chkconfig mysqld on
[root@node01 mysql]# systemctl start mysqld
[root@node01 mysql]# systemctl status mysqld
● mysqld.service - LSB: start and stop MySQL
Loaded: loaded (/etc/rc.d/init.d/mysqld; bad; vendor preset: disabled)
Active: active (running) since Thu 2022-06-02 13:57:49 CST; 4s ago
Docs: man:systemd-sysv-generator(8)
Process: 10083 ExecStart=/etc/rc.d/init.d/mysqld start (code=exited, status=0/SUCCESS)
CGroup: /system.slice/mysqld.service
├─10098 /bin/sh /opt/soft/mysql//bin/mysqld_safe --datadir=/opt/soft/mysql/data --pid-file=/opt/soft/mys...
└─10372 /opt/soft/mysql/bin/mysqld --basedir=/opt/soft/mysql/ --datadir=/opt/soft/mysql/data --plugin-di...
Jun 02 13:57:48 node01 systemd[1]: Starting LSB: start and stop MySQL...
Jun 02 13:57:48 node01 mysqld[10083]: Starting MySQL.Logging to '/opt/soft/mysql/data/node01.err'.
Jun 02 13:57:49 node01 mysqld[10083]: SUCCESS!
Jun 02 13:57:49 node01 systemd[1]: Started LSB: start and stop MySQL.
2.配置mysql环境变量或者使用mysql-client连接mysql
vim /etc/profile
export MYSQL_HOME=/opt/soft/mysql
export PATH=$MYSQL_HOME/bin:$PATH
source /etc/profile
1.初次登录使用初始化数据库时生成的默认密码
[root@node01 mysql]# mysql -uroot -ptsXj?0C?i5MR
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.22-log
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
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>
2.登陆MySQL后,需要修改root用户密码
mysql> set password=password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
验证密码登录
[root@node01 mysql]# 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 3
Server version: 5.7.22-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
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>
3.创建一个测试库及测试表
向表中插入数据,查看插入的数据
缺少libaio.so.1依赖
./mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
[root@node01 bin]# ./mysqld --initialize --user=mysql --basedir=/opt/soft/mysql/ --datadir=/opt/soft/mysql/data
./mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
[root@node01 bin]#
mysql安装时失败,报错如上,原因是没有安装libaio.so.1,安装即可:
yum install -y libaio