一、【使用网易源安装 MySQL】
1.1 CentOS 7 安装MySQL
(2020-11-17 发现网易yum镜像不能用了,清华可以)
添加yum 源文件: vi /etc/yum.repos.d/mysql-community.repo
[mysql-connectors-community]
name=MySQL Connectors Community
baseurl=https://mirrors.163.com/mysql/yum/mysql-connectors-community-el7-$basearch/
enabled=1
gpgcheck=1
gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql
[mysql-tools-community]
name=MySQL Tools Community
baseurl=https://mirrors.163.com/mysql/yum/mysql-tools-community-el7-$basearch/
enabled=1
gpgcheck=1
gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql
[mysql-5.6-community]
name=MySQL 5.6 Community Server
baseurl=https://mirrors.163.com/mysql/yum/mysql-5.6-community-el7-$basearch/
enabled=0
gpgcheck=1
gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql
[mysql-5.7-community]
name=MySQL 5.7 Community Server
baseurl=https://mirrors.163.com/mysql/yum/mysql-5.7-community-el7-$basearch/
enabled=1
gpgcheck=1
gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql
[mysql-8.0-community]
name=MySQL 8.0 Community Server
baseurl=https://mirrors.163.com/mysql/yum/mysql-8.0-community-el7-$basearch/
enabled=1
gpgcheck=1
gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql
安装mysql:yum install mysql-community-server
1.2 CentOS 8 或 6 安装 MySQL
只需将上面repo脚本中所有 el7 替换成 el8 或 el6 即可。
sed -i 's#el7#el8#g' /etc/yum.repos.d/mysql-community.repo # Centos 8
sed -i 's#el7#el6#g' /etc/yum.repos.d/mysql-community.repo # Centos 6
1.3 安装指定版本
以v5.7为例:将上面脚本中除 ‘mysql-5.7-community’ 标签下 enabled = 1,其他标签下全部置为0即可。
二、【使用其他国内源】
2.1 使用清华源
直接将上面脚本域名换成 mirrors.tuna.tsinghua.edu.cn 即可。
sed -i 's#mirrors.163.com#mirrors.tuna.tsinghua.edu.cn#g' /etc/yum.repos.d/mysql-community.repo
2.2 使用腾讯源
替换域名为 mirrors.cloud.tencent.com
sed -i 's#mirrors.163.com#mirrors.cloud.tencent.com#g' /etc/yum.repos.d/mysql-community.repo
三、安装后初始化
3.1 启动服务
systemctl start mysqld
systemclt enable mysqld
systemctl daemon-reload
3.2 修改初始密码
- 从日志中获取系统初始密码
grep 'generated' /var/log/mysqld.log | awk -F ' ' '{print $13}'
- 使用初始密码登录并修改初始密码
mysql -uroot -p
alter user 'root'@'localhost' identified by 'Admin@123';
- 修改密码安全级别策略
在配置文件 /etc/my.cnf中添加:
validate_password.length = 0
validate_password.mixed_case_count = 0
validate_password.number_count = 0
validate_password.policy = LOW
validate_password.special_char_count = 0
现在可以重新登录mysql修改简单密码或空密码了。
3.3 开启远程连接
有些远程客户端不支持 caching_sha2_password 密码加密方式,改成native 方式:
// 允许root任意ip登录
update user set host='%' where user='root';
flush privileges;
// 修改密码加密方式
alter user 'root'@'%' identified with mysql_native_password by '123456';
// 设置root 密码永久有效
alter user 'root'@'%' identified by '123456' password expire never;
flush privileges;