1.切换到root用户
su -
2.下载并安装MySQL官方的 Yum Repository
wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
3.开始安装
yum -y install mysql57-community-release-el7-10.noarch.rpm
4.安装mysql服务器
yum -y install mysql-community-server
PS:
1.如果报错All matches were filtered out by modular filtering for argument: mysql-community-server,执行下面命令
yum module disable mysql
yum -y install mysql-community-server
2.如果提示密钥问题,执行下面命令之后再执行 yum -y install mysql-community-server
rm /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
5.启动数据库
systemctl start mysqld.service
systemctl status mysqld.service #查看运行状态
6.修改数据库root密码
此时MySQL已经开始正常运行,不过要想进入MySQL还得先找出此时root用户的密码,通过如下命令可以在日志文件中找出密码:
grep “password” /var/log/mysqld.log
1.登录数据库
mysql -uroot -p
输入上图的初始密码
2.修改密码为: 123456
提示密码太简单,不符合密码策略!
ALTER USER ‘root’ @‘localhost’ IDENTIFIED BY ‘新密码’;
3.修改密码策略
SHOW VARIABLES LIKE ‘validate_password%’; #查看 mysql 初始的密码策略,
set global validate_password_policy=LOW; #设置密码的验证强度等级
set global validate_password_length=6; #设置为 6 位的密码长度
4.修改root帐号外网登录
下面命令开启的IP是 192.168.1.2,如要开启所有的,用%代替IP
GRANT ALL PRIVILEGES ON . TO ‘root’@‘192.168.1.2’ IDENTIFIED BY ‘你的密码’ WITH GRANT OPTION;
6.开放端口号
退出mysql命令模式:exit
添加mysql端口3306
firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --reload
7.更改编码格式及不区分大小写首先重新登录mysql,然后输入status:
vi /etc/my.cnf
[client]
default-character-set=utf8
default-storage-engine=INNODB
character-set-server=utf8
collation-server=utf8_general_ci
lower_case_table_names = 1
更改完成,重启mysql服务:
systemctl restart mysqld.service