1、检测系统是否已经安装过mysql或其依赖,若已装过要先将其删除
首先需要先停止mysqld服务,否则直接卸载rpm包后,重新安装的mysql没有初始密码
[root@node2 mysql-5.7.26]# systemctl stop mysqld [root@node2 mysql-5.7.26]# rpm -qa|grep mysql mysql-community-libs-5.7.26-1.el7.x86_64 mysql-community-common-5.7.26-1.el7.x86_64 mysql-community-client-5.7.26-1.el7.x86_64 mysql-community-server-5.7.26-1.el7.x86_64
[root@node2 mysql-5.7.26]# rpm -e mysql-community-server-5.7.26-1.el7.x86_64 [root@node2 mysql-5.7.26]# rpm -e mysql-community-client-5.7.26-1.el7.x86_64 [root@node2 mysql-5.7.26]# rpm -e mysql-community-libs-5.7.26-1.el7.x86_64 [root@node2 mysql-5.7.26]# rpm -e mysql-community-common-5.7.26-1.el7.x86_64
卸载顺序为server->client->libs->common
将剩余mysql目录删除干净
[root@node2 mysql-5.7.26]# find / -name mysql /etc/selinux/targeted/active/modules/100/mysql /var/lib/mysql /var/lib/mysql/mysql /usr/share/mysql [root@node2 mysql-5.7.26]# rm -rf var/lib/mysql [root@node2 mysql-5.7.26]# rm -rf /usr/share/mysql [root@node2 mysql-5.7.26]# rm -rf /var/log/mysqld.log
2、下载rpm包
mkdir /usr/local/src/mysql-5.7.26 cd /usr/local/src/ wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-community-server-5.7.26-1.el7.x86_64.rpm wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-community-client-5.7.26-1.el7.x86_64.rpm wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-community-common-5.7.26-1.el7.x86_64.rpm wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-community-libs-5.7.26-1.el7.x86_64.rpm
3、安装rpm包
按照依赖顺序依次安装rpm包,依赖关系为common->libs->client->server
rpm -ivh mysql-community-common-5.7.26-1.el7.x86_64.rpm rpm -ivh mysql-community-libs-5.7.26-1.el7.x86_64.rpm rpm -ivh mysql-community-client-5.7.26-1.el7.x86_64.rpm rpm -ivh mysql-community-server-5.7.26-1.el7.x86_64.rpm
在安装mysql-community-common-5.7.26-1.el7.x86_64.rpm ,可能会报错:
file /usr/share/mysql/czech/errmsg.sys from install of mysql-community-common-5.7.26-1.el7.x86_64 conflicts with file from package mariadb-libs-1:5.5.60-1.el7_5.x86_64 file /usr/share/mysql/danish/errmsg.sys from install of mysql-community-common-5.7.26-1.el7.x86_64 conflicts with file from package mariadb-libs-1:5.5.60-1.el7_5.x86_64
提示是文件与mariadb-libs-1:5.5.60-1.el7_5.x86_64包里的冲突,卸载mariadb-libs-1:5.5.60-1.el7_5.x86_64即可
rpm -e mariadb-libs-1:5.5.60-1.el7_5.x86_64 --nodeps
4、验证
[root@node2 mysql-5.7.26]# whereis mysql mysql: /usr/bin/mysql /usr/lib64/mysql /usr/share/mysql /usr/share/man/man1/mysql.1.gz [root@node2 mysql-5.7.26]# whereis my.cnf my: /etc/my.cnf
编辑my.cnf
[mysqld] datadir=/home/mysql-5.7.26/data user=mysql symbolic-links=0 #server_id=71 #gtid_mode=on #开启gtid模式 #enforce_gtid_consistency=on #强制gtid一致性 #log-bin=slave-71 #开启二进制日志 #binlog_format=row log-error=/home/mysql-5.7.26/log/mysqld.log [mysqld_safe] log-error=/home/mysql-5.7.26/log/mysqld.log
建立相关目录
[root@localhost mysql-5.7.26]# mkdir /home/mysql-5.7.26/ [root@localhost mysql-5.7.26]# mkdir /home/mysql-5.7.26/log [root@localhost mysql-5.7.26]# mkdir /home/mysql-5.7.26/data [root@localhost mysql-5.7.26]# chown -R mysql:mysql /home/mysql-5.7.26/
5、第一次启动mysql服务
启动后报错,查看日志提示:
mysqld: Table 'mysql.plugin' doesn't exist
此时需要执行数据库的初始化操作
先删除之前生成的data目录
[root@localhost mysql-5.7.26]# rm -rf data/
新建data目录
[root@localhost mysql-5.7.26]# mkdir data [root@localhost mysql-5.7.26]# chown -R mysql:mysql data/ [root@localhost mysql-5.7.26]# ll 总用量 0 drwxr-xr-x. 2 mysql mysql 6 7月 26 17:31 data drwxr-xr-x. 2 mysql mysql 24 7月 26 17:19 log
初始化数据库
[root@localhost home]# /usr/bin/mysql_install_db --user=mysql --datadir=/home/mysql-5.7.26/data/ 2021-07-26 17:32:18 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize
再次启动mysqld服务
[root@localhost mysql-5.7.26]# systemctl start mysqld [root@localhost mysql-5.7.26]# systemctl status mysqld ● mysqld.service - MySQL Server Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled) Active: active (running) since 一 2021-07-26 17:32:42 CST; 3s ago Docs: man:mysqld(8) http://dev.mysql.com/doc/refman/en/using-systemd.html Process: 11532 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS) Process: 11513 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS) Main PID: 11534 (mysqld) CGroup: /system.slice/mysqld.service └─11534 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid 7月 26 17:32:42 localhost.localdomain systemd[1]: Starting MySQL Server... 7月 26 17:32:42 localhost.localdomain systemd[1]: Started MySQL Server. [root@localhost mysql-5.7.26]#
成功
[root@node2 src]# netstat -tunlp |grep 3306 tcp6 0 0 :::3306 :::* LISTEN 22905/mysqld
查看3306端口,启动成功,查看第一次运行mysql时,系统分配的root账号临时密码,在日志文件里搜索'temporary password'
[root@node3 mysql-5.7.26]# grep 'temporary password' /var/log/mysqld.log 2019-05-03T07:30:18.845325Z 1 [Note] A temporary password is generated for root@localhost: _:fwrY:W-8;l
6、利用密码,启动mysql客户端并修改root账号密码
[root@node3 mysql-5.7.26]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.26 Copyright (c) 2000, 2019, 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>alter user 'root'@'localhost' identified by 'xxxxxxx'; Query OK, 0 rows affected (0.00 sec) mysql> quit
7、利用新密码重新登录mysql
[root@node3 mysql-5.7.26]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.7.26 MySQL Community Server (GPL) Copyright (c) 2000, 2019, 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>
8、设置远程连接权限
update user set host = ’%’ where user = ’root’; //出现报错可以忽略 grant all privileges on *.* to root@'%' identified by "mypassword"; flush privileges;
9、查看结果
select host,user,password from user;