主从复制语法
CHANGE MASTER TO option [, option] ... [ channel_option ] option: { MASTER_BIND = 'interface_name' | MASTER_HOST = 'host_name' | MASTER_USER = 'user_name' | MASTER_PASSWORD = 'password' | MASTER_PORT = port_num | PRIVILEGE_CHECKS_USER = {'account' | NULL} | REQUIRE_ROW_FORMAT = {0|1} | REQUIRE_TABLE_PRIMARY_KEY_CHECK = {STREAM | ON | OFF} | MASTER_CONNECT_RETRY = interval | MASTER_RETRY_COUNT = count | MASTER_DELAY = interval | MASTER_HEARTBEAT_PERIOD = interval | MASTER_LOG_FILE = 'source_log_name' | MASTER_LOG_POS = source_log_pos | MASTER_AUTO_POSITION = {0|1} | RELAY_LOG_FILE = 'relay_log_name' | RELAY_LOG_POS = relay_log_pos | MASTER_COMPRESSION_ALGORITHMS = 'value' | MASTER_ZSTD_COMPRESSION_LEVEL = level | MASTER_SSL = {0|1} | MASTER_SSL_CA = 'ca_file_name' | MASTER_SSL_CAPATH = 'ca_directory_name' | MASTER_SSL_CERT = 'cert_file_name' | MASTER_SSL_CRL = 'crl_file_name' | MASTER_SSL_CRLPATH = 'crl_directory_name' | MASTER_SSL_KEY = 'key_file_name' | MASTER_SSL_CIPHER = 'cipher_list' | MASTER_SSL_VERIFY_SERVER_CERT = {0|1} | MASTER_TLS_VERSION = 'protocol_list' | MASTER_TLS_CIPHERSUITES = 'ciphersuite_list' | MASTER_PUBLIC_KEY_PATH = 'key_file_name' | GET_MASTER_PUBLIC_KEY = {0|1} | IGNORE_SERVER_IDS = (server_id_list) } channel_option: FOR CHANNEL channel server_id_list: [server_id [, server_id] ... ]
假如使用binlog+position的方式:
change master to MASTER_HOST = 'host_name' ,MASTER_USER = 'user_name' , MASTER_PASSWORD = 'password' , MASTER_PORT = port_num , MASTER_LOG_FILE = 'source_log_name' ,MASTER_LOG_POS = source_log_pos
对应的主机名,用户,密码,端口号,二进制日志文件名,二进制日志位置。这些有了就OK了。
GTID模式
change master to MASTER_HOST = 'host_name' ,MASTER_USER = 'user_name' , MASTER_PASSWORD = 'password' , MASTER_PORT = port_num ,MASTER_AUTO_POSITION = {0|1}
假如开启了GTID模式(变量gtid_mode=on),这是就可以用GTID去替代binlog和pos了,GTID是全局事务ID,GTID=server_uuid:transaction_id.
其中server_uuid是在数据库启动过程中自动生成的,每台机器的server——uuid不一样,保存在数据目录的auto.cnf文件中。transaction_id就是事务提交时服务器分配的一个不会重复的序列号。
如果用mysqldump备份数据,那在备份后的数据中会记录一行set @@global.GTID_PURGED=***;如果是用xtrabackup备份,备份后的文件中会直接记录需要跳过的GTID。
mysql5.7版本之后新增了一张表gitd_executed,该表会记录已经执行的GTID的信息。这个时候就不用像5.6版本中必须开始log_slave_updates参数了,此参数是开启从库复制的二进制log.
binlog不可能永久停留在服务器上,需要进行定期清理(如通过expire_logs_days),否则迟早它会把磁盘空间用完。gtid_purged用于记录已经被清除了的binlog事务集合,它是gtid_executed的子集。只有gtid_executed为空时才能手动设置该变量,此时会同时更新gtid_executed为和gtid_purged相同的值。gtid_executed为空意味着要么之前没有启动过基于GTID的复制,要么执行过reset master。执行reset master时同样也会把gtid_purged置空,即始终保持gtid_purged是gtid_executed的子集。
主从管理语句
show slave status :在从库上查看主从复制状态;
show master status :查看主库的binlog和position 位置,以及开启GTID模式下记录的gtid;
change master to :在从库上配置主从过程
start slave:开启主从同步;
stop slave:关闭主从同步;
reset slave all:清空从库的所有配置信息。