当前位置: 移动技术网 > 网络运营>服务器>Linux > 在 CentOS 7 中安装 MySQL 8 的教程详解

在 CentOS 7 中安装 MySQL 8 的教程详解

2019年04月18日  | 移动技术网网络运营  | 我要评论

准备

本文环境信息:

软件 版本
centos centos 7.4
mysql 8.0.x

安装前先更新系统所有包

sudo yum update

安装

1. 添加 yum 包

wget https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm
# 或者 wget http://repo.mysql.com/mysql80-community-release-el7-1.noarch.rpm
sudo yum update
sudo rpm -ivh mysql80-community-release-el7-1.noarch.rpm

注:在 官网 可以找到最新的 rpm 包名。

2. 安装 mysql

# 安装
sudo yum -y install mysql-community-server
# 启动守护进程
sudo systemctl start mysqld
# 查看状态
sudo systemctl status mysqld
# 查看版本
mysql -v

安装完后,mysql 会在系统启动时自动启动,如果不想让它自动启动,可以使用 systemctl disable mysqld 关闭它。

3. 修改密码

mysql 安装过程中会为 root 用户生成一个临时密码,保存在 /var/log/mysqld.log 中。通过以下命令查看:
sudo grep 'temporary password' /var/log/mysqld.log

进入 mysql 客户端修改:

mysql -u root -p
alter user 'root'@'localhost' identified by 'your passowrd';
# alter user 'root'@ identified by 'your passowrd';

密码强度要求是:不少于12字符,必须包含大写字母、小写字母、数字和特殊字符。

3. mysql 安全配置

mysql 包含一个安全设置向导脚本,可以用它来修改安全选项。

sudo mysql_secure_installation

运行后依次设置以下几项:

1.修改root账号密码
2.密码强度验证插件(建议使用)
3.移除匿名用户(建议移除)
4.禁用root账户远程登录
5.移除测试数据库(test)

根据个人情况设置。

用户权限

1. 赋予权限

# 创建本地用户
create user 'user'@'localhost' identified by 'password';
# 新建远程用户
create user 'user'@'%' identified by 'password';
# 新建数据库
create database test_db;
# 查看用户权限
show grants for 'user'@'%';
# 赋予用户指定数据库远程访问权限
grant all privileges on test_db.* to 'user'@'%';
# 赋予用户对所有数据库远程访问权限
grant all privileges on *.* to 'user'@'%';
# 赋予用户对所有数据库本地访问权限
grant all privileges on *.* to 'user'@'localhost';
# 刷新权限
flush privileges;

2. 收回权限

# 收回权限
revoke all privileges on *.* from 'test'@'%';
# 删除本地用户
drop user 'user'@'localhost';
# 删除远程用户
drop user 'user'@'%';
# 刷新权限
flush privileges;

3. 远程登录

在 mysql 数据库查看 user 表信息 :

use mysql;
select host, user, authentication_string, plugin from user;

表格中 root 用户的 host 默认是 localhost,只允许本地访问。授权 root 用户的所有权限并设置远程访问:

# 授权
grant all on *.* to 'root'@'%';
# 刷新
flush privileges;

root 用户默认的密码加密方式是:caching_sha2_password;而很多图形客户端工具可能还不支持这种加密认证方式,连接的时候就会报错 。通过以下命令重新修改密码:

alter user 'root'@'%' identified with mysql_native_password by 'your password';

这里指定了 root 的密码加密方式为 mysql_native_password,如果想改变默认密码加密方式都是,可以在 /etc/my.cnf 文件加上一行:

default-authentication-plugin=mysql_native_password

如果服务器开启了防火墙,则需要打开 3306 端口。

firewall-cmd --add-port=3306/tcp --permanent
firewall-cmd --reload

注意:如果是云服务器,有的服务商(如阿里云)需要到控制台去开放端口的。

修改字符编码

字符集是一套符号和编码,查看字符集配置:

mysql> show variables like 'charac%';
+--------------------------+--------------------------------+
| variable_name  | value    |
+--------------------------+--------------------------------+
| character_set_client | utf8mb4   |
| character_set_connection | utf8mb4   |
| character_set_database | utf8mb4   |
| character_set_filesystem | binary    |
| character_set_results | utf8mb4   |
| character_set_server | utf8mb4   |
| character_set_system | utf8    |
| character_sets_dir | /usr/share/mysql-8.0/charsets/ |
+--------------------------+--------------------------------+

字符集生效规则为:table 继承于 database,database 继承于 server。就是说,可只设置 character_set_server。

校对规则是在字符集内用于比较字符的一套规则,查看校对规则:

mysql> show character set like 'utf8%';
+---------+---------------+--------------------+--------+
| charset | description | default collation | maxlen |
+---------+---------------+--------------------+--------+
| utf8 | utf-8 unicode | utf8_general_ci | 3 |
| utf8mb4 | utf-8 unicode | utf8mb4_0900_ai_ci | 4 |
+---------+---------------+--------------------+--------+

校对规则生效规则:如果没有设置校对规则,字符集取默认校对规则,例如 utf8mb4 的校对规则是utf8mb4_0900_ai_ci。

mysql 8 默认字符集改成了 utf8mb4。之前的 mysql 版本如果默认字符集不是 utf8mb4,建议改成 utf8mb4。

mb4 即 most bytes 4。为什么是 utf8mb4,而不是 utf8?mysql 支持的 utf8 编码最大字符长度为 3 字节,如果遇到 4 字节的宽字符就会插入异常。

下面是 老版mysql 修改字符集为 utf8mb4 的步骤,mysql 8.0+ 无需修改。

# 查看配置文件位置
whereis my.cnf

# 打开文件
vi /etc/my.cnf

增加字符编码配置项:

[client]
default-character-set=utf8mb4

[mysqld]
character-set-server=utf8mb4
collation-server=utf8mb4_general_ci

重启 mysql 服务

sudo systemctl restart mysqld

使用 mysql 命令检查字符集配置:

show variables like 'charac%';

参考


推荐:

感兴趣的朋友可以关注小编的微信公众号【码农那点事儿】,更多网页制作特效源码及学习干货哦!!!

总结

以上所述是小编给大家介绍的在 centos 7 中安装 mysql 8 的教程详解,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网