Centos7 RPM安装MYSQL8

前言

都说MySql8.x5.7快两倍,可以参考一下。
但不支持从 MySQL 8.0 降级到 MySQL 5.7(或从某个 MySQL 8.0 版本降级到任意一个更早的 MySQL 8.0 版本)。数据备份方式还是可以的。

  • 注意MySql8 的用户安全策略的改变
  • 注意MySql8 编码格式
  • 支撑内网服务器安装

准备

下载

1
2
3
4
5
6
7
8
# 打包下载
wget https://cdn.mysql.com//Downloads/MySQL-8.2/mysql-8.2.0-1.el7.x86_64.rpm-bundle.tar
tar -xvf mysql-8.2.0-1.el7.x86_64.rpm-bundle.tar
# 单独下载

wget https://cdn.mysql.com//Downloads/MySQL-8.2/mysql-community-server-8.2.0-1.el7.x86_64.rpm
wget https://cdn.mysql.com//Downloads/MySQL-8.2/mysql-community-libs-8.2.0-1.el7.x86_64.rpm
wget https://cdn.mysql.com//Downloads/MySQL-8.2/mysql-community-client-8.2.0-1.el7.x86_64.rpm

安装

先查看有没有 mariadb

1
rpm -qa | grep mariadb

有就强制卸载

1
rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64

强制安装

1
2
3
4
5
6
rpm -ivh mysql-community-common-8.2.0-1.el7.x86_64.rpm --nodeps --force
rpm -ivh mysql-community-server-8.2.0-1.el7.x86_64.rpm --nodeps --force
rpm -ivh mysql-community-libs-8.2.0-1.el7.x86_64.rpm --nodeps --force
# 可选安装
rpm -ivh mysql-community-client-8.2.0-1.el7.x86_64.rpm --nodeps --force
rpm -ivh mysql-community-devel-8.2.0-1.el7.x86_64.rpm --nodeps --force

这时候查看安装

1
2
3
4
rpm -qa|grep mysql
mysql-community-server-8.2.0-1.el7.x86_64
mysql-community-client-8.2.0-1.el7.x86_64
mysql-community-libs-8.2.0-1.el7.x86_64

初始化配置

1
vim /etc/my.cnf

配置路径 /opt/db/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8mb4
socket=/opt/db/mysql_data/mysql.sock
[mysqld]
skip-name-resolve
#设置3306端口
port=3331
datadir=/opt/db/mysql_data
socket=/opt/db/mysql_data/mysql.sock
log-error=/opt/db/mysql_log/mysqld.log
pid-file=/opt/db/mysqld/mysqld.pid
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8mb4
#排序规则
collation-server=utf8mb4_0900_ai_ci
#utf8mb4_0900_ai_ci 排序规则:ai 口音不敏感 ci 不区分大小写 ,默认支持表情符号
#utf8mb4_0900_ai_ci 属于 utf8mb4_unicode_ci 中的一种
#utf8mb4_general_ci 没有实现utf8mb4_unicode_ci 的排序规则。没有utf8mb4_unicode_ci 准备。但是比较和排序的时候更快
# 创建新表时将使用的默认存储引擎,innodb支持事物
default-storage-engine=INNODB
lower_case_table_names=1
max_allowed_packet=16M
lc-messages-dir=/usr/share/MySQL-8.2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
rm -rf /opt/db/mysql/
rm -rf /opt/db/mysqld/
rm -rf /opt/db/mysql_data
rm -rf /opt/db/mysql_log
mkdir -p /opt/db/mysql/
mkdir -p /opt/db/mysqld/
mkdir -p /opt/db/mysql_data
mkdir -p /opt/db/mysql_log
chown mysql:mysql /opt/db/ -R
chmod 777 -R /opt/db/mysqld

sudo mysqld --initialize --user=mysql --basedir=/usr
setenforce 0
systemctl start mysqld.service
systemctl status mysqld.service
# 权限
# chmod 777 /opt/db/mysqld/mysqld.pid
# chmod 777 /opt/db/mysql_log/mysqld.log
# 开机自起
systemctl enable mysqld
cat /opt/db/mysql_log/mysqld.log

报错解决

mysqld: error while loading shared libraries: libaio.so.1

1
yum install libaio 

Can't find error-message file '/usr/share/MySQL-8.2/errmsg.sys'. Check error-message file location and 'lc-messages-dir' configuration directive.

1
lc-messages-dir=/usr/share/MySQL-8.2

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

1
2
rm -rf /var/lib/mysql/mysql.sock
systemctl restart mysqld

查看安装日志中的密码

1
cat /opt/db/mysql_log/mysqld.log | grep password

登录

1
mysql -uroot -p

比如这个uQ3sw%Cu;IKk复制粘贴就可以了。

修改密码

1
2
3
4
5
6
7
flush privileges;
alter user 'root'@'localhost' identified by "123456";
# alter user 'root'@'localhost' identified WITH caching_sha2_password by "123456";
#创建远程连接
create user root@'%' identified by '123456';
grant all privileges on *.* to root@'%';
flush privileges;

无密码的方式,可用于找回密码。

输入一百次也输入不对默认密码 配置文件 MySQL 免密码登录 编辑 MySQL 的配置文件

1
vim /etc/my.cnf

[mysqld]下添加

1
2
#在 [mysqld] 开头的下面一行加入下面这句
skip-grant-tables

修改密码(记得删除my.cnf文件的东西)

1
2
3
4
5
6
7
8
# 这里需要先刷新权限,不然会报错。
flush privileges;
alter user 'root'@'localhost' identified by "123456";
# alter user 'root'@'localhost' identified WITH caching_sha2_password by "123456";
#创建远程连接
create user root@'%' identified by '123456';
grant all privileges on *.* to root@'%';
flush privileges;

或者

1
2
3
bin/mysql 
use mysql;
update user SET Password = 'new-password' WHERE User = 'root';

基本配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8mb4
socket=/opt/db/mysql_data/mysql.sock
[mysqld]
skip-name-resolve
#设置3306端口
port = 3331
datadir=/opt/db/mysql_data
socket=/opt/db/mysql_data/mysql.sock
log-error=/opt/db/mysql_log/mysqld.log
pid-file=/opt/db/mysqld/mysqld.pid
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8mb4
#排序规则
collation-server=utf8mb4_0900_ai_ci
#utf8mb4_0900_ai_ci 排序规则:ai 口音不敏感 ci 不区分大小写 ,默认支持表情符号
#utf8mb4_0900_ai_ci 属于 utf8mb4_unicode_ci 中的一种
#utf8mb4_general_ci 没有实现utf8mb4_unicode_ci 的排序规则。没有utf8mb4_unicode_ci 准备。但是比较和排序的时候更快
# 创建新表时将使用的默认存储引擎,innodb支持事物
default-storage-engine=INNODB
lower_case_table_names=1
max_allowed_packet=16M
lc-messages-dir=/usr/share/MySQL-8.2
# 不使用密码进入
# skip-grant-tables

推荐

MySQL8.0创建用户及其配置
MySQL8.0新特性-新的索引方式
MySQL8.0新特性-通用表表达式(CTE)
MySQL8.0新特性-窗口函数
MySQL8.0新特性-InnoDB增强
MySQL8.0新特性-JSON增强
[官方介绍]([https://dev.mysql.com/doc/refman/8.0/en/create-index.html#create-index-functional-key-parts

本文地址: https://github.com/maxzhao-it/blog/post/26543/