MySQL8二进制方式部署主从模式
系统及MySQL版本:
- Centos7.5
- MySQL 8.0.35
下载安装包
先去官网下载二进制安装包,进入官网下载页面 mysql下载页面,点击下载 5.7.34二进制64位版本
环境准备
1. 安装linux依赖包
查看安装文档
在下载页面上面有安装指南
翻译大概是,mysql依赖libaio这个library。如果没有安装,那么安装会失败。
安装命令:yum install libaio numactl ncurses-devel autoconf
2.删除自带的mariadb
#查找是否安装mariadb
rpm -qa|grep mariadb
#卸载mariadb
rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64
3.创建用户以及用户组
groupadd mysql
useradd -g mysql -s /sbin/nologin mysql
4.配置 ulimit
我们一般使用 mysql 用户启动数据库,如果不设置文件打开数和进程数,后期会有问题
打开 /etc/security/limits.conf
文件,在文件末尾写上
mysql soft nproc 65536
mysql hard nproc 65536
mysql soft nofile 131072
mysql hard nofile 131072
打开 /etc/security/limits.d/20-nproc.conf
把参数调整为
* soft nofile 65536
* hard nofile 65536
* soft nproc 131072
* hard nproc 131072
5.关闭 selinux
selinux 可能会造成无法写入数据,如果对它不熟悉,建议关掉
查看 selinux 状态
getenforce
临时关闭
setenforce 0
永久关闭
打开 /etc/sysconfig/selinux
SELINUX=enforcing` 改为 `SELINUX=disabled
安装配置
配置mysql
上传文件,在当前目录解压
tar -xf mysql-8.0.35-linux-glibc2.17-x86_64.tar.xz
mv mysql-8.0.35-linux-glibc2.17-x86_64 mysql
将配置文件复制到 /etc/init.d 目录下
cp ./mysql/support-files/mysql.server /etc/init.d/mysql8
编辑移动后的配置文件
vi /etc/init.d/mysqld8
在配置文件中修改参数路径
basedir=/AppHome/mysql
datadir=/AppHome/mysql/data
创建my.cnf文件
/AppHome/mysql/my.cnf
master配置
[client]
port = 8306
socket = /tmp/mysql.sock
[mysql]
prompt="\u@MySQL-01 \R:\m:\s [\d]> "
no-auto-rehash
[mysqld]
user = mysql
port = 8306
basedir = /AppHome/mysql
datadir = /AppHome/mysql/data
socket = /tmp/mysql.sock
pid-file = /tmp/mysql-8306.pid
character-set-server = utf8mb4
default-time_zone = '+8:00'
server-id = 002
lower_case_table_names = 1
# 继续使用Mysql5.x的认证方式
default_authentication_plugin = mysql_native_password
# 禁止域名解析
skip_name_resolve = 1
# 事务隔离级别
transaction_isolation = REPEATABLE-READ
# 性能参数 #
open_files_limit = 65535
back_log = 1024
max_connections = 3000
max_connect_errors = 1000000
table_open_cache = 20000
table_definition_cache = 20000
table_open_cache_instances = 64
# 每个连接线程被创建时,MySQL给它分配的内存大小
thread_stack = 512K
# 外部锁定
external-locking = FALSE
# 数据包的过程当中最大允许的数据包大小
max_allowed_packet = 32M
sort_buffer_size = 4M
join_buffer_size = 4M
#线程缓存中有多少空闲线程
thread_cache_size = 768
#内部内存临时表的最大值
tmp_table_size = 32M
max_heap_table_size = 32M
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
lock_wait_timeout = 3600
explicit_defaults_for_timestamp = 1
# log settings #
slow_query_log = 1
slow_query_log_file = /AppHome/mysql/logs/slow.log
log-error = /AppHome/mysql/logs/error.log
long_query_time = 5
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 = /AppHome/mysql/logbin/mybinlog
#log_bin_index = /AppHome/mysql/logbin/mybinlog.index
# innodb settings #
innodb_thread_concurrency = 0
innodb_sync_spin_loops = 100
innodb_spin_wait_delay = 30
innodb_buffer_pool_size = 2687M
innodb_buffer_pool_instances = 8
innodb_buffer_pool_load_at_startup = 1
innodb_buffer_pool_dump_at_shutdown = 1
innodb_data_file_path = ibdata1:12M: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_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
innodb_stats_on_metadata = 0
innodb_status_file = 1
innodb_status_output = 0
innodb_status_output_locks = 0
# replication settings #
sync_binlog = 1
binlog_cache_size = 4M
max_binlog_cache_size = 2G
max_binlog_size = 1G
binlog_expire_logs_seconds = 43200
master_info_repository = TABLE
relay_log_info_repository = TABLE
gtid_mode = on
enforce_gtid_consistency = on
log_slave_updates = 1
binlog_format = row
binlog_checksum = 1
relay_log_recovery = 1
relay-log-purge = 1
#不同步相关的库
replicate-ignore-db = mysql
replicate-ignore-db = information_schema
replicate-ignore-db = performance_schema
replicate-ignore-db = sys
binlog-ignore-db=information_schema
binlog-ignore-db=mysql
binlog-ignore-db=performance_schema
binlog-ignore-db=sys
# other #
performance_schema = 1
performance_schema_instrument = '%=on'
[mysqldump]
quick
max_allowed_packet = 512M
salve配置
上面配置的server-id = 001
改为002
创建目录授权
mkdir -p /AppHome/mysql/data
mkdir -p /AppHome/mysql/logs
mkdir -p /AppHome/mysql/logbin
chown -R mysql:mysql /AppHome/mysql/my.cnf
chown -R mysql:mysql /AppHome/mysql/data/
chown -R mysql:mysql /AppHome/mysql/logs
chown -R mysql:mysql /AppHome/mysql/logbin
chown -R mysql:mysql /AppHome/mysql
初始化MySQL
/AppHome/mysql/bin/mysqld --initialize-insecure --basedir=/AppHome/mysql --datadir=/AppHome/mysql/data/ --user=mysql --log-error=/AppHome/mysql/logs/error.log --lower-case-table-names=1
初始化SSL
可忽略执行
/AppHome/mysql/bin/mysql_ssl_rsa_setup --datadir=/AppHome/mysql/data/ -S /tmp/mysql.sock
启动MySQL
/etc/init.d/mysqld8 start
命令行方式启动
/AppHome/mysql/bin/mysqld_safe --defaults-file=/AppHome/mysql/my.cnf --user=mysql &
查看MySQL进程
ps axu|grep mysql
部署MySQL主从
登录主节点,创建主从同步账号
CREATE USER 'repl'@'%' IDENTIFIED BY '11111^';
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';
flush privileges;
登录主节点,查询binlog文件名与偏移量
show master status;
登录从节点,配置主节点信息
STOP SLAVE;
CHANGE MASTER TO
MASTER_HOST = '10.196.69.28',
MASTER_USER = 'repl',
MASTER_PORT = 8306,
MASTER_PASSWORD = '11111^',
MASTER_LOG_FILE = 'mybinlog.000002',
MASTER_LOG_POS = 2173;
在从服务器启动同步并查看状态
#开启同步
start slave;
#查看同步状态
SHOW SLAVE STATUS\G;
检查输出中的 Slave_IO_Running 和 Slave_SQL_Running 字段,确保两个字段的值都是 Yes,表示主从复制已经成功配置。
修改root密码
默认root密码是空的
alter user 'root'@'localhost' identified by '123456';
flush privileges;
设置从库只读
确保主从复制已经正常配置并运行,主库和从库之间可以正常同步数据。在从库的配置文件中,添加以下配置项:
read_only = 1
super_read_only = 1
重启从库的 MySQL 服务,使配置项生效。/etc/init.d/mysql8 restart