CentOS7安装MySQL

1.先查看是否已经安装MySQL

rpm -qa | grep mysql

若返回为空,则未安装。

2.下载安装wget

yum -y install wget

3.下载MySQL的repo源

wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm

4.安装mysql-community-release-el7-5.noarch.rpm包

sudo rpm -ivh mysql-community-release-el7-5.noarch.rpm

5.安装MySQL

sudo yum install mysql-server

6.修改/var/lib/mysql的访问权限

sudo chown -R openscanner:openscanner /var/lib/mysql

7.启动MySQL

service mysqld restart

8.修改root密码

  • 登录mysql
mysql -u root
  • 修改密码
use mysql;
set password for 'root'@'localhost' = password('123456'); # 123456为你的新密码,可根据需要自行修改

9.使用iptables防火墙; 开放3306端口

  • 关闭防火墙服务:
systemctl stop firewalld
  • 设置开机禁用
systemctl disable firewalld
  • 查看是否安装iptables
systemctl status iptables.service
  • 安装iptables
yum install -y iptables
  • 安装iptables-service
yum install iptables-services
  • 编辑防火墙配置文件
sudo vi /etc/sysconfig/iptables
  • 添加以下内容
-A INPUT -p tcp -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
  • 保存配置
service iptables save
  • 重启防火墙
systemctl restart iptables.service
  • 设置开机启动
systemctl enable iptables.servcie

10.允许root远程登录

  • 登录mysql
mysql -u root -p
use mysql;
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION; # 123456换成你设置的密码
flush privileges;

CentOS7安装MySQL
https://www.lihaibao.cn/2023/12/29/CentOS7安装MySQL/
Author
Seal Li
Posted on
December 29, 2023
Licensed under