云服务器CentOS7.5安装MySQL5.7

时间:2022-09-19 20:12:45

CentOS 7的默认yum仓库中并没有MySQL5.7,需要手动添加.

本文我们将介绍CentOS 7下MySQL5.7的安装。

  1. 添加Mysql5.7仓库,可去官网https://dev.mysql.com/downloads/repo/yum/ 找到 yum 源 rpm 安装包
sudo rpm -ivh https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
  1. 确认Mysql仓库成功添加
sudo yum repolist all | grep mysql | grep enabled

         如果展示像下面,则表示成功添加仓库:

mysql-connectors-community/x86_64  MySQL Connectors Community    enabled:     51
mysql-tools-community/x86_64       MySQL Tools Community         enabled:     63
mysql57-community/x86_64           MySQL 5.7 Community Server    enabled:    267
  1. 开始安装Mysql5.7
sudo yum -y install mysql-community-server
  1. 启动Mysql

#启动
sudo systemctl start mysqld #设置系统启动时自动启动 sudo systemctl enable mysqld #查看启动状态 sudo systemctl status mysqld

5. 设置数据库编码为utf8

#打开配置文件
sudo vim /etc/my.cnf
#添加如下代码

[client]
default-character-set=utf8

[mysql]
default-character-set=utf8

[mysqld]
collation-server = utf8_unicode_ci
init-connect='SET NAMES utf8'
character-set-server = utf8
#重启Mysql
sudo systemctl restart mysqld

6. 设置防火墙

sudo firewall-cmd --zone=public --permanent --add-service=mysql
sudo firewall-cmd --reload

7. 添加密码

sudo systemctl stop mysqld

sudo mysqld --user=root --skip-grant-tables
mysql -uroot

UPDATE mysql.user SET authentication_string=PASSWORD('#password') where USER='root';
flush privilege;
sudo systemctl restart mysqld

8. 添加用户用于远程访问

 #创建用户并授权 
GRANT ALL PRIVILEGES ON *.* TO 用户名@”%” IDENTIFIED BY ‘密码’ WITH GRANT OPTION; 

flush privileges;

或者修改root为可远程访问

use mysql;
UPDATE user SET Host='%' WHERE User='root';
flush privileges;

 

WRANING

  • ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

Solution:修改密码。如

SET PASSWORD FOR 'root'@'localhost' = PASSWORD('123');
  •  ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

Solution:密码安全等级不够,MySQL5.6以后加强了安全等级,密码可按如下方式查看

SHOW VARIABLES LIKE 'validate_password%';

+--------------------------------------+--------+
| Variable_name                        | Value  |
+--------------------------------------+--------+
| validate_password_check_user_name    | OFF    |
| validate_password_dictionary_file    |        |
| validate_password_length             | 8      |
| validate_password_mixed_case_count   | 1      |
| validate_password_number_count       | 1      |
| validate_password_policy             | MEDIUM |
| validate_password_special_char_count | 1      |
+--------------------------------------+--------+