Skip to content

Latest commit

 

History

History
80 lines (51 loc) · 2.22 KB

centos-7-uses-yum-way-to-install-and-configure-mysql.md

File metadata and controls

80 lines (51 loc) · 2.22 KB

使用yum方式安装和配置MySQL

安装MySQL

安装 8.0

rpm -Uvh https://repo.mysql.com/mysql80-community-release-el7-3.noarch.rpm
sed -i 's/enabled=1/enabled=0/' /etc/yum.repos.d/mysql-community.repo # 启用MySQL 8社区版
sudo yum --enablerepo=mysql80-community install -y mysql-community-server

如果在安装的过程中遇到MySQL Upgrade process failed - The GPG keys listed for the "MySQL 8.0 Community Server" repository are already installed but they are not correct for this repository. 错误可以执行:rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022 命令,具体查看这里

安装MySQL 5.7

使用yum命令之前,需要安装MySQL5.7的rpm仓库。执行下面的命令安装MySQL

wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
rpm -ivh mysql57-community-release-el7-11.noarch.rpm
sudo yum install -y mysql-server

启动MySQL

守护进程方式MySQL

执行下面的命令启动mysql并使其在CentOS系统启动时运行。

sudo systemctl start mysqld && sudo systemctl enable mysqld

配置MySQL

  • 获取安装时初始化密码

    sudo grep 'temporary password' /var/log/mysqld.log
  • 登录并重设root账户密码

    mysql -uroot -p # 回车输入上面获取到的密码
    
    ALTER USER 'root'@'localhost' IDENTIFIED BY 'pId%Mm!2vs~qnM@LFf^Bm';
  • 授权新用户

    CREATE USER 'new_user'@'localhost' IDENTIFIED BY 'CKHbYiEpZokM9yB7ojqPUikSLX4P!8Y';
    GRANT ALL PRIVILEGES ON new_user_database.* TO 'new_user'@'localhost';

检查MySQL

检查端口

MySQL已经启动并在3306端口上运行,可以使用netstat命令检查。

sudo netstat -plntu |grep 3306

检查进程

检查系统进程使用ps命令。

sudo ps aux |grep mysql

至此,CentOS下安装mysql就安装好了。

参考链接