主从复制1

第一章: xtrabackup安装包下载

第二章: 全量备份主库数据+在从库还原主库的数据

第三章: 主从复制关系配置

  • 3.1、 my.cnf配置文件(标准文档生产可直接用)

第一章:xtrabackup安装包下载

  • 使用背景:生产mysql数据库大小在80g左右,这种情况下,使用mysqldump备份+恢复数据需要的时间太久;故使用xtrabackup这种热备份,首先需要安装配置环境;生产环境mysql5.6、mysql5.7包科学上网吧,不会的下载的私信作者。
percona-xtrabackup-2.4.24-Linux-x86_64.glibc2.12-minimal.tar.gz
1、解压到指定目录:
tar -xzvf percona-xtrabackup-2.4.24-Linux-x86_64.glibc2.12-minimal.tar.gz -C /usr/local

2、做一个软连接:
ln -s percona-xtrabackup-2.4.24-Linux-x86_64.glibc2.12-minimal.tar.gz xtrabackup

3、配置环境变量:
/etc/profile文件中配置下环境变量source下就能使用了:
export PATH=$PATH:/usr/local/percona-xtrabackup/bin

4、验证下:
[root@k8s-master01 ~]# xtrabackup -v
xtrabackup: recognized server arguments: --server-id=1740 --datadir=/var/lib/mysql/data --log_bin=/var/lib/mysql/arch/mysql-bin --innodb_write_io_threads=16 --innodb_data_home_dir=/var/lib/mysql/data/ --innodb_data_file_path=ibdata1:1G;ibdata2:1G:autoextend --innodb_log_group_home_dir=/var/lib/mysql/arch --innodb_log_files_in_group=4 --innodb_log_file_size=1G --innodb_log_buffer_size=200M --innodb_buffer_pool_size=2G --tmpdir=/var/lib/mysql/tmp --innodb_flush_log_at_trx_commit=2 --innodb_read_io_threads=4 --innodb_write_io_threads=4 --innodb_io_capacity=200 --innodb_use_native_aio=1 --innodb_file_per_table=1 
xtrabackup version 2.4.24 based on MySQL server 5.7.35 Linux (x86_64) (revision id: b4ee263)

第二章:全量备份主库数据+在从库还原主库的数据

1、在主库上创建备份目录,目录自己选择:
mkdir /root/backup

2、注意下磁盘空间,开始全量备份,提示complete就表示完成:
innobackupex --defaults-file=/etc/my.cnf --user=root --password='123456' /root/backup

3、备份完成后的目录结构:
[root@idcserver2 backup]# pwd
/home/apps/backup
[root@idcserver2 backup]# ll
total 8
drwxr-x---. 15 root root 4096 Feb 19 21:56 2023-02-19_21-32-37

4、因为是文件夹可以选择打包传输到从库,因为作者内网传输速度很快,所以直接scp -r 2023-02-19_21-32-37 root@ip:/root/backup/

5、传输过去后需要在从库上恢复数据:
- 回滚事务
[root@idcmemdb1 backup]# innobackupex  --apply-log /root/backup/2023-02-19_21-32-37
- 复制数据
[root@idcmemdb1 backup]# innobackupex --defaults-file=/etc/my.cnf --copy-back /root/backup/2023-02-19_21-32-37
  • 实操中遇到的问题:
  1. 在从库上恢复数据的时候,需要先把mysql服务停止,service mysql stop
  2. 因为主库的datadir和二进制文件存放目录和从库的可能不一致,这点在使用xtrabackup恢复的时候不受影响,不需要两者目录保持一致,因为笔者生产数据库是rpm安装的,而从库是使用二进制安装的
  3. 在恢复的时候需要把datadir目录和bin_log二进制文件存放目录删除
  4. copy-back完成后,因为使用的是root用户,重新使用chown为整个目录赋一下全新啊
  5. 此时全量备份完成后可以启动从数据库

第三章:主从复制关系配置

  • 注意事项:
  • 前提主库的bin_log肯定是开启了的,才做的xtrabackup备份恢复
1、bin_log参考配置,配置完后需重启数据库
log-bin=/var/lib/mysql/bin_log/mysql-bin
expire_logs_days=7
max_binlog_size=200M
binlog_cache_size=32M
binlog_format=row
sync_binlog=1
innodb_support_xa=1
innodb_flush_log_at_trx_commit=1
log_slave_updates=1
innodb_buffer_pool_size=50G 根据内存大小自行设置
max_allowed_packet=8388608
1、在主库上创建复制用户,创建repluser用户只允许从库ip进行访问:
grant replication slave on *.* to 'repluser'@'从库ip' identified by 'password';

2、从库上操作连接主库:
CHANGE MASTER TO
MASTER_HOST='主库ip',
MASTER_USER='repluser',
MASTER_PASSWORD='主库密码',
MASTER_PORT=3306,
master_log_file='mysql-bin.019091',
master_log_pos=897809;

master_log_file和master_log_pos需要去到xtrabackup备份的文件夹中看
3、进入到文件夹查看:
[root@idcserver2 2023-02-19_21-32-37]# cat xtrabackup_binlog_info
mysql-bin.019091        897809  2dd5e4ee-9b4a-11e6-877a-384c4fcc6bf2:1-44723029

4、所以2的master_log_file就是mysql-bin.019091 ,master_log_pos就是897809

5、启动从节点
strat slave;

6、查看丛节点状态,这两个状态是yes就是没问题的:
show slave status \G;

Slave_IO_Running: Yes
Slave_SQL_Running: Yes
  • 作者此处遇到的问题,生产环境使用的是mysql5.6,从库环境笔者使用的是二进制安装的mysql5.7,从库同步时出现了问题:Reason:The master event is logically timestamped incorrectly
  • 参考解决博客地址:https://www.modb.pro/db/54335
  • 设置完后就一直同步没有问题产生

3.1、my.cnf配置文件(标准文档生产可直接用)

[root@hadoop39 local]# vi /etc/my.cnf
[client]
port            = 3306
socket          = /usr/local/mysql/data/mysql.sock
default-character-set=utf8mb4

[mysqld]
port            = 3306
socket          = /usr/local/mysql/data/mysql.sock

skip-slave-start

skip-external-locking
key_buffer_size = 256M
sort_buffer_size = 2M
read_buffer_size = 2M
read_rnd_buffer_size = 4M
query_cache_size= 32M
max_allowed_packet = 16M
myisam_sort_buffer_size=128M
tmp_table_size=32M

table_open_cache = 512
thread_cache_size = 8
wait_timeout = 86400
interactive_timeout = 86400
max_connections = 600

# Try number of CPU's*2 for thread_concurrency
#thread_concurrency = 32 

#isolation level and default engine 
default-storage-engine = INNODB
transaction-isolation = READ-COMMITTED

server-id  = 1739
basedir     = /usr/local/mysql
datadir     = /usr/local/mysql/data
pid-file     = /usr/local/mysql/data/hostname.pid

#open performance schema
log-warnings
sysdate-is-now

binlog_format = ROW
log_bin_trust_function_creators=1
log-error  = /usr/local/mysql/data/hostname.err
log-bin = /usr/local/mysql/arch/mysql-bin
expire_logs_days = 7

innodb_write_io_threads=16

relay-log  = /usr/local/mysql/relay_log/relay-log
relay-log-index = /usr/local/mysql/relay_log/relay-log.index
relay_log_info_file= /usr/local/mysql/relay_log/relay-log.info

log_slave_updates=1
gtid_mode=OFF
enforce_gtid_consistency=OFF

# slave
slave-parallel-type=LOGICAL_CLOCK
slave-parallel-workers=4
master_info_repository=TABLE
relay_log_info_repository=TABLE
relay_log_recovery=ON

#other logs
#general_log =1
#general_log_file  = /usr/local/mysql/data/general_log.err
#slow_query_log=1
#slow_query_log_file=/usr/local/mysql/data/slow_log.err

#for replication slave
sync_binlog = 500


#for innodb options 
innodb_data_home_dir = /usr/local/mysql/data/
innodb_data_file_path = ibdata1:1G;ibdata2:1G:autoextend

innodb_log_group_home_dir = /usr/local/mysql/arch
innodb_log_files_in_group = 4
innodb_log_file_size = 1G
innodb_log_buffer_size = 200M

#根据生产需要,调整pool size 
innodb_buffer_pool_size = 2G
#innodb_additional_mem_pool_size = 50M #deprecated in 5.6
tmpdir = /usr/local/mysql/tmp

innodb_lock_wait_timeout = 1000
#innodb_thread_concurrency = 0
innodb_flush_log_at_trx_commit = 2

innodb_locks_unsafe_for_binlog=1

#innodb io features: add for mysql5.5.8
performance_schema
innodb_read_io_threads=4
innodb-write-io-threads=4
innodb-io-capacity=200
#purge threads change default(0) to 1 for purge
innodb_purge_threads=1
innodb_use_native_aio=on

#case-sensitive file names and separate tablespace
innodb_file_per_table = 1
lower_case_table_names=1

[mysqldump]
quick
max_allowed_packet = 128M

[mysql]
no-auto-rehash
default-character-set=utf8mb4

[mysqlhotcopy]
interactive-timeout

[myisamchk]
key_buffer_size = 256M
sort_buffer_size = 256M
read_buffer = 2M
write_buffer = 2M

你可能感兴趣的:(mysql)