Installing MySQL 5.7.23 on CentOS 7

1. 安装前检查

1.1 检查NUMA是否开启

  • NUMA为什么要咋MySQL中禁用?
    MySQL是单进程多线程架构数据库,当numa采用默认内存分配策略时,MySQL进程会被并且仅仅会被分配到numa的一个节点上去。假设这个节点的本地内存为10GB,而MySQL配置20GB内存,超出节点本地内存部分(20GB-10GB)Linux会使用swap而不是使用其他节点的物理内存。在这种情况下,能观察到虽然系统总的可用内存还未用完,但是MySQL进程已经开始在使用swap了。如果单机只运行一个MySQL实例,可以选择关闭numa.
  • 检查NUMA
    Redhat或者Centos系统中可以通过命令判断bios层是否开启numa
# grep -i numa /var/log/dmesg

如果输出结果为: No NUMA configuration found
  • 关闭nuam方法
    修改/etc/default/grub文件,在kernel那行追加numa=off;
# vi /etc/default/grub


GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet numa=off"
GRUB_DISABLE_RECOVERY="true"

请注意,在RHEL 7或CentOS 7上,必须重建grub配置才能使更改生效

grub2-mkconfig -o /etc/grub2.cfg

1.2 限制/etc/security/limits.conf文件

mysql hard nproc 65535
mysql soft nproc 65535
mysql hard nofile 65535
mysql soft nofile 65535

或者

* hard nproc 65535
* soft nproc 65535
* hard nofile 65535
* soft nofile 65535

* 代表所有用户

1.3 swap设置

修改/etc/sysctl.conf 文件,根据以下策略增加对应的命令:


查看当前swap设置

# sysctl -a|grep swap
vm.swappiness = 30

1.4 禁用selinux

把SELINUX值修改成disabled,以禁用selinux

# vi /etc/sysconfig/selinux 

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

1.5 安装libaio 库

MySQL依赖于libaio 库。如果未在本地安装此库,则数据目录初始化和后续服务器启动步骤将失败。

# yum search all libaio
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
======================================================================= Matched: libaio ========================================================================
libaio.x86_64 : Linux-native asynchronous I/O access library
libaio-devel.x86_64 : Development files for Linux-native asynchronous I/O access

如果未安装,用如下命令:

# yum install libaio

1.6 关闭防火墙(可选)

//临时关闭
systemctl stop firewalld

//禁止开机启动
systemctl disable firewalld

Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

2. 安装MySQL

2.1 创建mysql用户和组

创建用户mysql,指定home目录/usr/local/mysql ,并且是不能登陆,-M代表不创建home目录

# groupadd mysql
# useradd -M -g mysql -s /sbin/nologin -d /usr/local/mysql mysql

2.2 解压mysql二进制包

# cd /opt

# tar -xvf mysql-5.7.23-linux-glibc2.12-x86_64.tar

# ls -l
total 1703176
drwxr-xr-x  2 root root          6 Jul 31 15:02 mysql
-rw-r--r--. 1 root root  673873920 Jul 31 11:23 mysql-5.7.23-linux-glibc2.12-x86_64.tar
-rw-r--r--  1 7161 31415 644399365 Jun  8 19:10 mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
-rw-r--r--. 1 root root  396308480 Jul 31 11:23 mysql-8.0.12-linux-glibc2.12-x86_64.tar
-rw-r--r--  1 7161 31415  29463116 Jun  8 19:08 mysql-test-5.7.23-linux-glibc2.12-x86_64.tar.gz
drwxr-xr-x. 2 root root          6 Sep  7  2017 rh
drwxr-xr-x. 9 root root        147 Jul 31 10:55 VBoxGuestAdditions-5.2.16

# mkdir mysql

# tar -zxvf /opt/mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz

2.3 创建软连接

ln命令创建指向安装目录的符号链接,这样可以访问/usr/local/mysql

# cd /usr/local
# ln -s /opt/mysql/mysql-5.7.23-linux-glibc2.12-x86_64 mysql

为了避免在使用MySQL时必须键入客户端程序的路径名,可以将/usr/local/mysql/bin目录添加到PATH变量中:

# echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
# source /etc/profile

3. 安装后配置

3.1 创建基本目录

# mkdir /data/mysql
# mkdir /data/mysql/mysql3306
# cd /data/mysql/mysql3306/
# mkdir {data,logs,tmp}
# chown -R mysql:mysql /data/mysql/mysql3306

3.1 创建/etc/my.cnf文件

[client]
port    = 3306
socket  = /tmp/mysql3306.sock

[mysql]
prompt="\\u@\\h [\\d]>" 
no-auto-rehash

[mysqld]
user    = mysql
port    = 3306
basedir = /usr/local/mysql
datadir = /data/mysql/mysql3306/data
tmpdir  = /data/mysql/mysql3306/tmp
socket  = /tmp/mysql3306.sock
pid-file = mysqldb1.pid
character-set-server = utf8mb4
skip_name_resolve = 1
open_files_limit    = 65535
back_log = 1024
max_connections = 512
max_connect_errors = 1000000
table_open_cache = 1024
table_definition_cache = 1024
table_open_cache_instances = 64
thread_stack = 512K
external-locking = FALSE
max_allowed_packet = 32M
sort_buffer_size = 4M
join_buffer_size = 4M
thread_cache_size = 768
interactive_timeout = 600
wait_timeout = 600
tmp_table_size = 32M
max_heap_table_size = 32M
slow_query_log = 1
slow_query_log_file = /data/mysql/mysql3306/slow.log
log-error = /data/mysql/mysql3306/error.log
long_query_time = 0.1
log_queries_not_using_indexes =1
log_throttle_queries_not_using_indexes = 60
min_examined_row_limit = 100
log_slow_admin_statements = 1
log_slow_slave_statements = 1
server-id = 1003306
log-bin = /data/mysql/mysql3306/logs/my3306_binlog
sync_binlog = 1
binlog_cache_size = 4M
max_binlog_cache_size = 2G
max_binlog_size = 1G
expire_logs_days = 7
master_info_repository = TABLE
relay_log_info_repository = TABLE
gtid_mode = on
enforce_gtid_consistency = 1
log_slave_updates
binlog_format = row
binlog_checksum = 1
relay_log_recovery = 1
relay-log-purge = 1
key_buffer_size = 32M
read_buffer_size = 8M
read_rnd_buffer_size = 4M
bulk_insert_buffer_size = 64M
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
lock_wait_timeout = 3600
explicit_defaults_for_timestamp = 1
innodb_thread_concurrency = 0
innodb_sync_spin_loops = 100
innodb_spin_wait_delay = 30

transaction_isolation = REPEATABLE-READ
#innodb_additional_mem_pool_size = 16M
innodb_buffer_pool_size = 2867M
innodb_buffer_pool_instances = 8
innodb_buffer_pool_load_at_startup = 1
innodb_buffer_pool_dump_at_shutdown = 1
innodb_data_file_path = ibdata1:1G:autoextend
innodb_flush_log_at_trx_commit = 1
innodb_log_buffer_size = 32M
innodb_log_file_size = 2G
innodb_log_files_in_group = 2
innodb_max_undo_log_size = 4G
innodb_undo_directory = undolog
innodb_undo_tablespaces = 95

# 根据您的服务器IOPS能力适当调整
# 一般配普通SSD盘的话,可以调整到 10000 - 20000
# 配置高端PCIe SSD卡的话,则可以调整的更高,比如 50000 - 80000
innodb_io_capacity = 4000
innodb_io_capacity_max = 8000
innodb_flush_neighbors = 0
innodb_write_io_threads = 8
innodb_read_io_threads = 8
innodb_purge_threads = 4
innodb_page_cleaners = 4
innodb_open_files = 65535
innodb_max_dirty_pages_pct = 50
innodb_flush_method = O_DIRECT
innodb_lru_scan_depth = 4000
innodb_checksum_algorithm = crc32
innodb_lock_wait_timeout = 10
innodb_rollback_on_timeout = 1
innodb_print_all_deadlocks = 1
innodb_file_per_table = 1
innodb_online_alter_log_max_size = 4G
internal_tmp_disk_storage_engine = InnoDB
innodb_stats_on_metadata = 0

# some var for MySQL 5.7
innodb_checksums = 1
#innodb_file_format = Barracuda
#innodb_file_format_max = Barracuda
query_cache_size = 0
query_cache_type = 0
innodb_undo_logs = 128

innodb_status_file = 1
# 注意: 开启 innodb_status_output & innodb_status_output_locks 后, 可能会导致log-error文件增长较快
innodb_status_output = 0
innodb_status_output_locks = 0

#performance_schema
performance_schema = 1
performance_schema_instrument = '%=on'

#innodb monitor
innodb_monitor_enable="module_innodb"
innodb_monitor_enable="module_server"
innodb_monitor_enable="module_dml"
innodb_monitor_enable="module_ddl"
innodb_monitor_enable="module_trx"
innodb_monitor_enable="module_os"
innodb_monitor_enable="module_purge"
innodb_monitor_enable="module_log"
innodb_monitor_enable="module_lock"
innodb_monitor_enable="module_buffer"
innodb_monitor_enable="module_index"
innodb_monitor_enable="module_ibuf_system"
innodb_monitor_enable="module_buffer_page"
innodb_monitor_enable="module_adaptive_hash"

[mysqldump]
quick
max_allowed_packet = 32M

3.2 初始化MySQL

# /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --initialize

3.3 查看临时密码

# cat error.log|grep password
2018-07-31T08:44:09.458906Z 1 [Note] A temporary password is generated for root@localhost: 0;u+it(H.mYL

3.4 创建启动脚本

# cd /usr/local/mysql/

# cp support-files/mysql.server /etc/init.d/mysql

3.5 启动和关闭

/etc/init.d/mysql start | stop | restart

3.6 以临时密码登陆mysql客户端

# /usr/local/mysql/bin/mysql -S /tmp/mysql3306.sock -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.23-log

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

(unknown)@localhost [(none)]>show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

3.7 更改密码

(unknown)@localhost [(none)]>alter user user() identified by 'mysql';
Query OK, 0 rows affected (0.01 sec)

root@localhost [(none)]>show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| undolog            |
+--------------------+
5 rows in set (0.00 sec)

4. mysql启动的一些方法

  • 系统自启
/etc/init.d/mysql start | stop | restart
  • 手工方式启动
#mysqld 启动


# /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf &



#mysqld_safe启动


# /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf &