1. 下载
wget https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz
2. 检测是否有旧版本或者以前的安装残留
rpm -qa | grep mysql
- 如有,类似
mysql-libs-5.1.52-1.el6_0.1.x86_64
- 卸载
rpm -e --nodeps mysql-libs-5.1.52-1.el6_0.1.x86_64
3. 检查是否存在 mariadb 数据库
rpm -qa | grep mariadb
- 如有,类似
rpm -qa | grep mariadb
- 卸载
rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64
4. 解压安装
tar zxf mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz -C /opt
mv /opt/mysql-5.7.25-linux-glibc2.12-x86_64 /opt/mysql-5.7.25
5. 配置MySQL环境变量
tee -a /etc/profile <<-'EOF'
# wlf-2020-09-21
export PATH=$PATH:/opt/mysql-5.7.25/bin
EOF
source /etc/profile
6. 检查系统是否有mysql组和mysql用户
cat /etc/group | grep mysql
#类似
mysql:x:490:
cat /etc/passwd | grep mysql
#类似
mysql:x:496:490::/home/mysql:/bin/bash
- 以上为存在的情况,如无,执行添加命令:
groupadd mysql
useradd -r -g mysql mysql
#useradd -r参数表示mysql用户是系统用户,不可用于登录系统
7. 创建data目录
mkdir /opt/mysql-5.7.25/data
8. 修改MySQL目录属主属组
chown -R mysql.mysql /opt/mysql-5.7.25
9. 创建默认配置文件
cd /opt/mysql-5.7.25/support-files
vim my_default.cnf
- 添加如下内容
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[mysqld]
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
basedir = /opt/mysql-5.7.25
datadir = /opt/mysql-5.7.25/data
port = 3306
socket = /tmp/mysql.sock
character-set-server=utf8
log-error = /opt/mysql-5.7.25/data/mysqld.log
pid-file = /opt/mysql-5.7.25/data/mysqld.pid
10. 拷贝MySQL配置文件
\cp /opt/mysql-5.7.25/support-files/my_default.cnf /etc/my.cnf
11. 初始化 mysqld
mysqld --initialize --user=mysql --basedir=/opt/mysql-5.7.25/ --datadir=/opt/mysql-5.7.25/data/
12. 获取临时密码
[root@cloudansys ~]# grep password /opt/mysql-5.7.25/data/mysqld.log
2020-09-21T02:04:18.317577Z 1 [Note] A temporary password is generated for root@localhost: e#oE#EG,g6Ai
-
如上,我的密码为【
e#oE#EG,g6Ai
】
13. 配置MySQL开机启动
cp /opt/mysql-5.7.25/support-files/mysql.server /etc/init.d/mysql
14. 启动MySQL
service mysql start
15. 登录mysql,密码为初始临时密码
mysql -u root -pe#oE#EG,g6Ai
16.修改密码
mysql> set password=password('123456');
mysql> grant all privileges on *.* to root@'%' identified by '123456';
mysql> flush privileges;
17. 添加远程访问权限
mysql> use mysql;
mysql> update user set host='%' where user = 'root';
mysql> flush privileges;
-
最终如下所示,即最终目的是把用户
root
的
host
设置为
%
mysql> use mysql;
mysql> select user,host,authentication_string from user;
+------+------+-------------------------------------------+
| user | host | authentication_string |
+------+------+-------------------------------------------+
| root | % | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
+------+------+-------------------------------------------+
1 row in set (0.00 sec)
mysql>
- 其他用户都可以删除掉
mysql> use mysql;
mysql> delete from user where user<>'root' and host<>'%';
Query OK, 3 rows affected (0.00 sec)
mysql>
18. 重启mysql生效
service mysql restart
版权声明:本文为qq_42761569原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。