准备安装环境
[root@server2 ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
[root@server2 ~]# uname -r
3.10.0-693.el7.x86_64
查询已安装的旧版本mysql
rpm -qa | grep mysql
rpm -qa | grep mariadb
卸载已安装的旧版本mysql
rpm -e --nodeps {file_name}
# {file_name} 是查询到的程序名
选择合适的版本
网址:https://downloads.mysql.com/archives/community/
需要安装wget程序
yum install -y wget
下载安装文件
下载地址:https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.30-1.el7.x86_64.rpm-bundle.tar
[root@server2 ~]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.30-1.el7.x86_64.rpm-bundle.tar
解压缩下载文件
[root@server2 ~]# tar -xvf mysql-5.7.30-1.el7.x86_64.rpm-bundle.tar
安装perl依赖包
yum install -y perl
安装mysql
安装顺序:common→libs→client→server
rpm -ivh mysql-community-common-5.7.30-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-5.7.30-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-5.7.30-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-5.7.30-1.el7.x86_64.rpm
启动mysql
systemctl start mysqld
检查进程与服务端口号
[root@server2 ~]# ps -ef | grep mysqld
mysql 1199 1 0 06:09 ? 00:00:00 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
[root@server2 ~]# netstat -lntup | grep mysqld
tcp6 0 0 :::3306 :::* LISTEN 1199/mysqld
获取初始化root密码
grep 'temporary password' /var/log/mysqld.log
[root@server2 ~]# grep 'temporary password' /var/log/mysqld.log
2021-09-09T22:09:29.134740Z 1 [Note] A temporary password is generated for root@localhost: 4=4kaOLqkgtl
使用初始密码登录mysql
mysql -u root -p'4=4kaOLqkgtl'
[root@server2 ~]# mysql -u root -p'4=4kaOLqkgtl'
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.30
Copyright (c) 2000, 2020, 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>
修改密码策略
set global validate_password_policy=0;
修改有效密码长度
set global validate_password_length=4;
设置新密码
set password for root@localhost=password('1234');
flush privileges;
授权远程连接
grant all privileges on *.* to 'root'@'%' identified by '1234' with grant option;
flush privileges;
修改默认编码,解决中文乱码
修改配置文件 /etc/my.cnf
添加如下配置
[mysqld]
character-set-server=utf8
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
重启服务
systemctl restart mysqld