@zhangyy
2021-10-28T15:26:55.000000Z
字数 3290
阅读 155
MySQL系列
配置启动主从复制:
主库从库都要创建复制用户:
mysql -uroot -proot
create user 'repuser'@'%' identified by 'repuser123';
grant replication slave on *.* to 'repuser'@'%';
GRANT ALL PRIVILEGES ON *.* TO 'repuser'@'%'IDENTIFIED BY 'repuser123' WITH GRANT OPTION;
flush privileges;
select user,host from mysql.user;
192.168.100.11
#master add parameter
vi /mysql/data/3306/my.cnf
-----
log_bin=/mysql/log/3306/binlog/mysql-binlog
log_bin_index=/mysql/log/3306/binlog/mysql-binlog.index
binlog_format=row
binlog_rows_query_log_events=on
#master modify parameter
bind-address=0.0.0.0
server_id=113306
expire_logs_days = 7
innodb_support_xa =1
binlog_cache_size = 32M
max_binlog_size = 2048M
log_bin_trust_function_creators = 1
innodb_flush_log_at_trx_commit =1
sync_binlog = 1
transaction-isolation = read-committed
----
#slave parameter
#relay_log = /mysql/log/3306/relaylog/itpuxdb-relay.log
#log-slave-updates = 1
#read_only=1
#slave-parallel-type=LOGICAL_CLOCK
#slave-parallel-workers=4
#master_info_repository=TABLE
#relay_log_info_repository=TABLE
#relay_log_recovery=1
#slave_skip_errors = ddl_exist_errors
#slave_preserve_commit_order=1
## 192.168.1.12(slave)
vi /mysql/data/3306/my.cnf
----
#slave add parameter
log_bin=/mysql/log/3306/binlog/itpuxdb-binlog
log_bin_index=/mysql/log/3306/binlog/itpuxdb-binlog.index
binlog_format=row
binlog_rows_query_log_events=on
#slave modify parameter
bind-address=0.0.0.0
server_id=123306
skip_name_resolve = on
expire_logs_days = 7
innodb_support_xa =1
binlog_cache_size = 1M
max_binlog_size = 2048M
log_bin_trust_function_creators = 1
innodb_flush_log_at_trx_commit =1
sync_binlog = 1
transaction-isolation = read-committed
#slave parameter
relay_log = /mysql/log/3306/relaylog/mysql-relay.log
log-slave-updates = 1
read_only=1
slave-parallel-type=LOGICAL_CLOCK
slave-parallel-workers=4
master_info_repository=table
relay_log_info_repository=table
relay_log_recovery=1
slave_skip_errors = ddl_exist_errors
slave_preserve_commit_order=1
------
从新启动MySQL:
systemctl restart mysqld
从主库上看binlog 日志:
主库: 192.168.100.11:
mysql -uroot -proot
show master status;
从库上面 复制主库
从库:192.168.100.12:
mysql -uroot -proot
stop slave;
change master to master_host='192.168.100.11', master_user='repuser', master_password='repuser123', master_log_file='mysql-binlog.000001', master_log_pos=154;
start slave;
show slave status \G
show processlist\G ;
在主库上创建库表测试:
create database itpuxdb1;
use itpuxdb1;
create table itpuxbak11 (id int primary key,name varchar(40));
insert into itpuxbak11 values (1,'itpux111'),(2,'itpux112'),(3,'itpux113'),(4,'itpux114'),(5,'itpux1 15');
commit;
select * from itpuxbak11;
备库检查:
select * from itpuxdb1.itpuxbak11;
涉及相关参数介绍与总结
show slave status 参数介绍:
mysql> show slave status \G
*************************** 1. row *************************** Slave_IO_State: Waiting for master to send event -- IO 线程 的状态
master_host: 192.168.1.51 -- 主库 IP
master_user: repuser -- 主库同步的用户
master_port: 3306 -- 主库同步的端口
Connect_Retry: 60 -- 连接尝试次数,使用 change master 时可以使用master-connect-retry 选项指定当前值。
master_log_file: itpuxdb-binlog.000005 -- IO 线程读取到主服务器二进制的文件
Read_Master_Log_Pos: 4117 --IO 线程读取主服务器二进制文件中的位置
relay_log_file: itpuxdb-relay.000002 --Slave 的 SQL
线程当前正在读取和执行的中继日志文件的名称。
relay_log_pos: 4288 线程读取 Slave 服务器中继日志文件中的位置
Relay_Master_Log_File: itpuxdb-binlog.000005 -- SQL 线程执行到的文 件
Slave_IO_Running: Yes -- io thread 启动成功
Slave_SQL_Running: Yes -- sql thread 启动成功
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table: