centos7安装mysql,并配置远程访问

1. 下载并安装MySQL官方的 Yum Repository

[root@iZuf62qojdpdfmeohedn2jZ software]# wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm 
# 下载完成之后安装
[root@iZuf62qojdpdfmeohedn2jZ software]# yum -y install mysql57-community-release-el7-10.noarch.rpm

2. 安装MySQL服务器

[root@iZuf62qojdpdfmeohedn2jZ software]# yum -y install mysql-community-server

3. 启动mysql服务

[root@iZuf62qojdpdfmeohedn2jZ software]# systemctl start mysqld  # 启动
[root@iZuf62qojdpdfmeohedn2jZ software]# systemctl restart mysqld # 重启
[root@iZuf62qojdpdfmeohedn2jZ software]# systemctl status mysqld # 查看mysql状态

centos7安装mysql,并配置远程访问_centos

4. 获取初始密码

[root@iZuf62qojdpdfmeohedn2jZ software]# grep "password" /var/log/mysqld.log
2020-11-13T09:21:09.143361Z 1 [Note] A temporary password is generated for root@localhost: 初始密码
[root@iZuf62qojdpdfmeohedn2jZ software]#

5. 登录mysql,并修改初始密码

# 输入两次获取到的初始密码,注意:输入的时候不显示

[root@iZuf62qojdpdfmeohedn2jZ software]# mysql -u root -p
password:
Enter password:

依次执行:

mysql> set global validate_password_policy=0;  # 修改安全等级
mysql> set global validate_password_length=1; # 修改密码长度
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'your password'; # 设置新密码
mysql> flush privileges; # 刷新权限,使修改生效

6. 配置远程访问

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'your password' WITH GRANT OPTION;  # 配置所有的IP都可以通过账号密码访问
mysql> flush privileges; # 使之生效
mysql> quit; # 退出mysql控制台

7. 开放3306端口 firewalld 防火墙(centos-7)运行命令,并重启:

[root@iZuf62qojdpdfmeohedn2jZ software]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
success
[root@iZuf62qojdpdfmeohedn2jZ software]# firewall-cmd --reload
success
[root@iZuf62qojdpdfmeohedn2jZ software]# systemctl restart mysqld
[root@iZuf62qojdpdfmeohedn2jZ software]#

8. 此时本地的服务器就可以连接centos上的mysql数据库了