Centos7 使用 RPM 源安装 MySQL

系统环境

1
2
CentOS Linux release 7.6.1810 (Core)
Linux centos7 3.10.0-957.5.1.el7.x86_64 #1 SMP Fri Feb 1 14:54:57 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

卸载 Mariadb

1
2
3
4
5
# 查找mariadb模块
# rpm -qa | grep mariadb

# 删除查找到的mariadb模块
# rpm -e --nodeps xxxx

RPM 源安装 MySQL

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 下载repository
# wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm

# 安装repository
# rpm -ivh mysql57-community-release-el7-11.noarch.rpm

# 查看repository是否安装成功
# yum repolist enabled | grep "mysql.*-community.*"

# 安装mysql
# yum install -y mysql-community-libs-compat mysql-community-server

# 启动mysql
# systemctl start mysqld

# 查看mysql启动状态
# systemctl status mysqld

开机自启动 MySQL

1
2
3
4
5
# 自启动
# systemctl enable mysqld

# 重载配置
# systemctl daemon-reload

更改 Root 本地登录密码、允许 Root 远程登录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 查看mysql安装时默认创建的密码
# grep 'temporary password' /var/log/mysqld.log

# 登录mysql
# mysql -h localhost -u root -p

# 更改root本地登录密码(由于mysql自身默认的密码检查策略,密码必须包含:大小写字母、数字和特殊符号,并且长度不能少于8位)
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'yourPassword';

# 允许root远程登录
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'yourPassword' WITH GRANT OPTION;

# 刷新mysql的系统权限相关表
mysql> FLUSH PRIVILEGES;

MySQL 基础配置、性能优化配置

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 备份默认的配置文件
# cp /etc/my.cnf /etc/my.cnf.default

# 编辑配置文件,添加以下内容
# vim /etc/my.cnf

[client]
default-character-set=utf8

[mysql]
default-character-set=utf8

[mysqld]

character-set-server=utf8
default-storage-engine=INNODB

default-time-zone="+8:00"
explicit_defaults_for_timestamp=true

########################################

max_allowed_packet=64M

back_log=800 #重点优化
max_connections=5000 #重点优化
table_open_cache=614 #重点优化,其值与max_connections相关
sort_buffer_size=2M #重点优化,其值与max_connections相关
join_buffer_size=2M #重点优化,其值与max_connections相关

thread_cache_size=300 #重点优化
query_cache_size=64M #重点优化
query_cache_limit=4M
query_cache_min_res_unit=2k

tmp_table_size=256M
key_buffer_size=2048M #重点优化
read_buffer_size=1M #其值与max_connections相关
read_rnd_buffer_size=16M #其值与max_connections相关
bulk_insert_buffer_size=64M

innodb_buffer_pool_size=2048M #重点优化
innodb_thread_concurrency=0 #重点优化
innodb_flush_log_at_trx_commit=1 #重点优化
innodb_log_buffer_size=8M
innodb_log_file_size=128M
innodb_log_files_in_group=3

配置防火墙

1
2
3
4
5
6
7
8
# 开放端口
# firewall-cmd --zone=public --permanent --add-port=3306/tcp

# 保存防火墙配置
# firewall-cmd --reload

# 查看已开放的端口
# firewall-cmd --list-ports

管理 MySQL 服务

1
2
3
4
5
6
7
8
9
10
11
# 关闭
# systemctl stop mysqld

# 启动
# systemctl start mysqld

# 重启
# systemctl restart mysqld

# 查看状态
# systemctl status mysqld

更改系统的最大打开文件描述符数

配置概述

1
2
3
4
5
6
配置文件:/etc/my.cnf
数据目录:/var/lib/mysql
日志文件:/var/log/mysqld.log
pid文件:/var/run/mysqld/mysqld.pid
socket文件:/var/lib/mysql/mysql.sock
服务启动脚本:/usr/lib/systemd/system/mysqld.service