一.安装(5.7版本)
由于CentOS 的yum源中没有mysql,需要到mysql的官网下载yum repo配置文件。

wget https://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm

rpm -ivh mysql57-community-release-el7-10.noarch.rpm

sudo yum install mysql-server

如果此时报有以下错误,则需要把 mariadb组件 移除,再进行安装

Removing mariadb-libs.x86_64 1:5.5.56-2.el7 – u due to obsoletes from mysql-community-libs.x86_64 0:5.6.38-2.el7 – u
–> Restarting Dependency Resolution with new changes.

yum -y remove mariadb-libs

 

如果此时报有以下错误

Public key for mysql-community-server-5.7.37-1.el7.x86_64.rpm is not installed
Failing package is: mysql-community-server-5.7.37-1.el7.x86_64
GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

在yum install 版本后面加上 –nogpgcheck,即可绕过GPG验证成功安装。比如 sudo yum install mysql-server –nogpgcheck

如果此时报有以下错误

warning: /var/cache/yum/x86_64/7/mysql57-community/packages/mysql-community-libs-compat-5.7.37-1.el7.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID 3a79bd29: NOKEY
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

The GPG keys listed for the "MySQL 5.7 Community Server" repository are already installed but they are not correct for this package.
Check that the correct key URLs are configured for this repository.

Failing package is: mysql-community-libs-compat-5.7.37-1.el7.x86_64
GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

大致意思就是MySQL的GPG升级了,需要更新

执行 rpm –import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022

二.设置配置文件
首先,我们要知道,mysql 安装在哪里

which mysql

/usr/bin/mysql

然后,查看配置文件的位置

/usr/bin/mysql --verbose --help | grep -A 1 'Default options'
返回:
Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf

这个信息的意思是:
服务器首先读取的是 /etc/my.cnf 文件,如果前一个文件不存在则继续读 /etc/mysql/my.cnf 文件,依此类推,如若还不存在便会去读~/.my.cnf文件。

我们的配置文件如下:

 

三.启动msyql

systemctl start mysqld

获取安装时的临时密码

grep 'temporary password' /var/log/mysqld.log

登入MySQL

mysql -u root -p

剩下的操作可参考MySQL 8.0.11安装遇到的问题 即可完成

 

四.开启远程控制
1.1、连接服务器: mysql -u root -p
1.2、看当前所有数据库:show databases;
1.3、进入mysql数据库:use mysql;
1.4、查看mysql数据库中所有的表:show tables;
1.5、查看user表中的数据:select Host, User,Password from user;
1.6、修改user表中的Host:update user set Host=’%’ where User=’root’;
说明: % 代表任意的客户端,可替换成具体IP地址。
1.7、最后刷新一下:flush privileges;

 

五.其他命令
关闭MySQL

systemctl stop mysqld 

重启MySQL

systemctl stop mysqld 

设置开机启动

systemctl enable mysqld 

关闭开机启动

systemctl disablemysqld 

查看MySQL运行状态

systemctl status mysqld 

 

六.开启防火墙端口

/sbin/iptables -I INPUT -p tcp --dport 6379 -j ACCEPT