- 操作系统:Rocky Linux 8.5
- Vcpu:2
- 内存:4G
- 网络:互联网
wget https://dev.MYSQL.com/get/Downloads/MySQL-8.0/mysql-8.0.29-linux-glibc2.17-x86_64-minimal.tar.xz
#或者
wget https://dev.MySQL.com/get/Downloads/MySQL-8.0/mysql-8.0.29-linux-glibc2.12-x86_64-minimal.tar.xz
或者进入Mysql官网下载,点击以下链接直达下载地址:
https://dev.mysql.com/downloads/mysql/,根据对应信息,下载版本进行安装。
环境配置- 配置 selinux
# enforcing 开启selinux
# permissive 宽松模式,记录但不操作
# disabled 关闭selinux
sed -i 's/SELINUX\=enforcing/SELINUX\=permissive/g' /etc/selinux/config
- 配置防火墙 生产环境防火墙需开启
# 关闭防火墙
systemctl stop firewalld.service
# 关闭开机自动启动
systemctl disabled firewalld.service
- 配置 NUMA
查看 numa
[root@mysqldb_master mysql]# numactl --hardware
available: 1 nodes (0)
node 0 cpus: 0 1
node 0 size: 3736 MB
node 0 free: 2267 MB
node distances:
node 0
0: 10
available: 1 nodes (0) #如果是 2 或多个 nodes 就说明 numa 没关掉
或者
[root@mysqldb_master local]# dmesg | grep -i numa
[ 0.000000] No NUMA configuration found
如果输出结果为:
No NUMA configuration found
说明 numa 为 disable,如果不是上面的内容说明 numa 为 enable
关闭 numa
vim /etc/default/grub
或者在 mysql 的 my.cnf 中,配置 innodb_numa_interleave 参数,将其设置为 ON
mysql> show variables like '%NUMA%';
ERROR 4031 (HY000): The client was disconnected by the server because of inactivity. See wait_timeout and interactive_timeout for configuring this behavior.
No connection. Trying to reconnect...
Connection id: 22
Current database: *** NONE ***
------------------------ -------
| Variable_name | Value |
------------------------ -------
| innodb_numa_interleave | OFF |
------------------------ -------
1 row in set (0.00 sec)
还有其他方式,如想知道请自行查询资料
- 操作系统资源限制
echo "
* soft nproc 65535
* hard nproc 65535
* soft noFile 65536
* hard nofile 65536" >>/etc/security/limits.conf
- 配置用户组及用户
如无特殊说明,都是使用 root 用户操作
groupadd -g 999 mysql
useradd -g mysql -s /sbin/nologin -d /usr/local/mysql -MN mysql
这里 bash 是 nologin,也就是没有 shell,用户不能登录 bash 中,提高安全性
- 解压包
cd /soft
tar -xvf mysql-8.0.27-linux-glibc2.17-x86_64.tar.xz
- 软件包统一放在/usr/local 下面,做个软链接,方便以后升级
cd /usr/local
ls -s /soft/mysql-8.0.27-linux-glibc2.12-x86_64 mysql
- 解绑
cd /usr/local
unllink mysql
- 创建数据目录并配置权限
mkdir -p /data/mysql/{data,logs,tmp}
chow -R mysql:mysql /usr/local/mysql
chow -R mysql:mysql /data/mysql
- 创建 my.cnf 文件
去网站上自动生成,根据自己的需要再进行修改方便靠谱的 my.cnf 生成工具
[client]
port = 3306
socket = /data/mysql/mysql.sock
[mysql]
prompt = "\u@mysqldb_master \R:\m:\s [\d]> "
no_auto_rehash
[mysqld]
user = mysql
port = 3306
#主从复制或MGR集群中,server_id记得要不同
#另外,实例启动时会生成 auto.cnf,里面的 server_uuid 值也要不同
#server_uuid的值还可以自己手动指定,只要符合uuid的格式标准就可以
server_id = 3306
basedir = /app/mysql
datadir = /data/mysql/data
socket = /tmp/mysql.sock
pid_file = mysqldb_master.pid
character_set_server = UTF8MB4
skip_name_resolve = 1
#若你的MySQL数据库主要运行在境外,请务必根据实际情况调整本参数
default_time_zone = " 8:00"
#启用admin_port,连接数爆满等紧急情况下给管理员留个后门
admin_address = '127.0.0.1'
admin_port = 33062
#performance setttings
lock_wait_timeout = 3600
open_files_limit = 65535
back_log = 1024
max_connections = 10
max_connect_errors = 1000000
table_open_cache = 200
table_definition_cache = 200
thread_stack = 512K
sort_buffer_size = 4M
join_buffer_size = 4M
read_buffer_size = 8M
read_rnd_buffer_size = 4M
bulk_insert_buffer_size = 64M
thread_cache_size = 15
interactive_timeout = 600
wait_timeout = 600
tmp_table_size = 32M
max_heap_table_size = 32M
#log settings
log_timestamps = SYSTEM
log_error = /data/mysql/data/error.log
log_error_verbosity = 3
slow_query_log = 1
log_slow_extra = 1
slow_query_log_file = /data/mysql/data/slow.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
log_bin = /data/mysql/data/mybinlog
binlog_format = ROW
sync_binlog = 1 #MGR环境中由其他节点提供容错性,可不设置双1以提高本地节点性能
binlog_cache_size = 4M
max_binlog_cache_size = 2G
max_binlog_size = 1G
binlog_rows_query_log_events = 1
binlog_expire_logs_seconds = 604800
#MySQL 8.0.22前,想启用MGR的话,需要设置binlog_checksum=NONE才行
binlog_checksum = CRC32
gtid_mode = ON
enforce_gtid_consistency = TRUE
#myisam settings
key_buffer_size = 32M
myisam_sort_buffer_size = 128M
#replication settings
relay_log_recovery = 1
slave_parallel_type = LOGICAL_CLOCK
slave_parallel_workers = 64 #可以设置为逻辑CPU数量的2倍
binlog_transaction_dependency_tracking = WRITESET
slave_preserve_commit_order = 1
slave_checkpoint_period = 2
#mgr settings
loose-plugin_load_add = 'mysql_clone.so'
loose-plugin_load_add = 'group_replication.so'
loose-group_replication_group_name = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaa1"
#MGR本地节点IP:PORT,请自行替换
loose-group_replication_local_address = "172.16.16.10:33061"
#MGR集群所有节点IP:PORT,请自行替换
loose-group_replication_group_seeds = "172.16.16.10:33061,172.16.16.11:33061,172.16.16.12:33061"
loose-group_replication_start_on_boot = OFF
loose-group_replication_bootstrap_group = OFF
loose-group_replication_exit_state_action = READ_ONLY
loose-group_replication_flow_control_mode = "DISABLED"
loose-group_replication_single_primary_mode = ON
loose-group_replication_communication_max_message_size = 10M
loose-group_replication_unreachable_majority_timeout = 30
loose-group_replication_member_expel_timeout = 5
loose-group_replication_autorejoin_tries = 288
#innodb settings
transaction_isolation = REPEATABLE-READ
#系统内存的0.7
innodb_buffer_pool_size = 2867M
innodb_buffer_pool_instances = 4
innodb_data_file_path = ibdata1:12M:autoextend
innodb_flush_log_at_trx_commit = 1 #MGR环境中由其他节点提供容错性,可不设置双1以提高本地节点性能
innodb_log_buffer_size = 32M
innodb_log_file_size = 1G #如果线上环境的TPS较高,建议加大至1G以上,如果压力不大可以调小
innodb_log_files_in_group = 3
innodb_max_undo_log_size = 4G
# 根据您的服务器IOPS能力适当调整
# 一般配普通SSD盘的话,可以调整到 10000 - 20000
# 配置高端PCIe SSD卡的话,则可以调整的更高,比如 50000 - 80000
innodb_io_capacity = 4000
innodb_io_capacity_max = 8000
innodb_open_files = 65535
innodb_flush_method = O_DIRECT
innodb_lru_scan_depth = 4000
innodb_lock_wait_timeout = 10
innodb_rollback_on_timeout = 1
innodb_print_all_deadlocks = 1
innodb_online_alter_log_max_size = 4G
innodb_print_ddl_logs = 1
innodb_status_file = 1
#注意: 开启 innodb_status_output & innodb_status_output_locks 后, 可能会导致log_error文件增长较快
innodb_status_output = 0
innodb_status_output_locks = 1
innodb_sort_buffer_size = 67108864
innodb_adaptive_hash_index = OFF
#提高索引统计信息精确度
innodb_stats_persistent_sample_pages = 500
#innodb monitor settings
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"
#pfs settings
performance_schema = 1
#performance_schema_instrument = '%memory%=on'
performance_schema_instrument = '%lock%=on'
[mysqldump]
quick - 初始化数据库
初始化时可以有密码也可以没有
有密码:
/usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/my.cnf --initialize
无密码:
/usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/my.cnf --initialize-insecure
如果有问题,会马上返回,没问题,等一会就会返回。
日志如下:
[root@mysqldb_master logs]# cat error.log
2022-07-22T10:30:28.758075 08:00 0 [Warning] [MY-000081] [Server] option 'table_definition_cache': unsigned value 200 adjusted to 400.
2022-07-22T10:30:28.759983 08:00 0 [Warning] [MY-011068] [Server] The syntax 'log_slow_slave_statements' is deprecated and will be removed in a future release. Please use log_slow_replica_statements instead.
2022-07-22T10:30:28.760210 08:00 0 [Note] [MY-010096] [Server] Ignoring --secure-file-priv value as server is running with --initialize(-insecure).
2022-07-22T10:30:28.760238 08:00 0 [Note] [MY-010949] [Server] Basedir set to /soft/mysql-8.0.29-linux-glibc2.17-x86_64-minimal/.
......
- 配置 mysql 环境
vim /etc/profile
#MYSQL_HOME
export MYSQL_HOME=/usr/local/mysql
export PATH=$PATH:$MYSQL_HOME/bin
- 更新环境变量
source /etc/profile
- 启动数据库
- mysqld_safe 启动(rpm 安装没有对应的文件)
mysqld_safe --defaults-file=/data/mysql/my.cnf &
- mysqld 启动
mysqld --defaults-file=/data/mysql/my.cnf &
启动成功会有对应的日志。
连接数据库- 修改 root 密码
[root@mysqldb_master logs]# mysql -S /data/mysql/mysql.sock -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.29 MySQL Community Server - GPL
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
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.
mysql> alter user user() identified by 'mysql';
Query OK, 0 rows affected (0.02 sec)
- 初始化过程提示少 so 文件
[root@mysqldb_salve mysql]# /usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/my.cnf --initialize
/usr/local/mysql/bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
[root@mysqldb_salve mysql]# dnf provides '*/libaio.so.1'
Last metadata expiration check: 1:41:20 ago on Fri 22 Jul 2022 09:13:49 AM CST.
libaio-0.3.112-1.el8.i686 : Linux-native asynchronous I/O access library
Repo : baseos
Matched from:
Filename : /usr/lib/libaio.so.1
libaio-0.3.112-1.el8.x86_64 : Linux-native asynchronous I/O access library
Repo : baseos
Matched from:
Filename : /usr/lib64/libaio.so.1
rpm -qf /lib64/libaio.so.1
- 连接过程提示缺少 so 文件
[root@mysqldb_salve mysql]# /usr/local/mysql/bin/mysqld -S /tmp/mysql.sock -p
/usr/local/mysql/bin/mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory
[root@mysqldb_salve mysql]# dnf provides '*/libncurses.so.5'
Last metadata expiration check: 1:44:42 ago on Fri 22 Jul 2022 09:13:49 AM CST.
ncurses-compat-libs-6.1-9.20180224.el8.i686 : Ncurses compatibility libraries
Repo : baseos
Matched from:
Filename : /usr/lib/libncurses.so.5
ncurses-compat-libs-6.1-9.20180224.el8.x86_64 : Ncurses compatibility libraries
Repo : baseos
Matched from:
Filename : /usr/lib64/libncurses.so.5
[root@mysqldb_salve mysql]# dnf install ncurses-compat-libs-6.1-9.20180224.el8.x86_64 -y