准备工作
Mysql 单机多实例详解
What is the GTID of the replication
实验环境
Os: CentOS 6.X
Mysql: 5.6 单机多实例 [3306,3307]
Hostname: lab.suzf.net
场景一:新机器 无数据
对于GTID的配置,主要修改配置文件中与GTID特性相关的几个重要参数
[mysqld_multi] mysqld = /usr/local/mysql/bin/mysqld_safe mysqladmin = /usr/local/mysql/bin/mysqladmin log = /usr/local/mysql/data/log/mysqld_multi.log user = root ## Used for stopping the server via mysqladmin # master [mysqld3306] socket = /usr/local/mysql/data/run/mysqld_3306.sock port = 3306 pid-file = /usr/local/mysql/data/run/mysqld_3306.pid datadir = /usr/local/mysql/data/mysql_3306 # set for GTID Replication server_id = 3232237894 gtid_mode = on enforce_gtid_consistency = on #强制gtid一致性,开启后对于特定create table不被支持 # binlog log-bin = /usr/local/mysql/data/binlogs/master-binlog log-slave-updates = 1 binlog_format = row #强烈建议,其他格式可能造成数据不一致 # rely log skip_slave_start=1 # slave [mysqld3307] socket = /usr/local/mysql/data/run/mysqld_3307.sock port = 3307 pid-file = /usr/local/mysql/data/run/mysqld_3307.pid datadir = /usr/local/mysql/data/mysql_3307 # set for GTID Replication server_id = 3232237866 gtid_mode = on enforce_gtid_consistency = on # bin log log-bin = /usr/local/mysql/data/binlogs/slave-binlog log-slave-updates = 1 binlog_format = row # rely log skip_slave_start=1 [client] default-character-set = utf8
重启mysql
/etc/init.d/mysqld_multi.server restart
Mysql 多实例启动脚本
cat /etc/init.d/mysqld_multi.server #!/bin/sh # # A simple startup script for mysqld_multi by Tim Smith and Jani Tolonen. # This script assumes that my.cnf file exists either in /etc/my.cnf or # /root/.my.cnf and has groups [mysqld_multi] and [mysqldN]. See the # mysqld_multi documentation for detailed instructions. # # This script can be used as /etc/init.d/mysql.server # # Comments to support chkconfig on RedHat Linux # chkconfig: 2345 64 36 # description: A very fast and reliable SQL database engine. # # Version 1.0 # basedir=/usr/local/mysql bindir=/usr/local/mysql/bin conf=$basedir/data/etc/my.cnf export PATH=$PATH:$bindir if test -x $bindir/mysqld_multi then mysqld_multi="$bindir/mysqld_multi"; else echo "Can‘t execute $bindir/mysqld_multi from dir $basedir"; exit; fi case "$1" in ‘start‘ ) "$mysqld_multi" --defaults-extra-file=$conf start $2 ;; ‘stop‘ ) "$mysqld_multi" --defaults-extra-file=$conf stop $2 ;; ‘report‘ ) "$mysqld_multi" --defaults-extra-file=$conf report $2 ;; ‘restart‘ ) "$mysqld_multi" --defaults-extra-file=$conf stop $2 "$mysqld_multi" --defaults-extra-file=$conf start $2 ;; *) echo "Usage: $0 {start|stop|report|restart}" >&2 ;; esac
Master OPS
mysql -uroot -S /usr/local/mysql/data/run/mysqld_3306.sock mysql> GRANT REPLICATION SLAVE ON *.* TO ‘repluser‘@‘localhost‘ IDENTIFIED BY ‘suzf.net666‘; Query OK, 0 rows affected (0.00 sec)
Slave OPS
mysql -uroot -S /usr/local/mysql/data/run/mysqld_3307.sock mysql> CHANGE MASTER TO -> MASTER_HOST=‘localhost‘, -> MASTER_USER=‘repluser‘, -> MASTER_PASSWORD=‘suzf.net666‘, -> MASTER_PORT=3306, -> MASTER_AUTO_POSITION = 1; Query OK, 0 rows affected, 2 warnings (0.04 sec) mysql> start slave; Query OK, 0 rows affected (0.19 sec)
验证
场景二:移除 Replication
首先关闭 SLAVE
mysql> STOP SLAVE; Query OK, 0 rows affected (0.00 sec)
使用ALL 参数 重置 SLAVE (MySQL >= 5.6.7):
mysql> RESET SLAVE ALL; Query OK, 0 rows affected (0.02 sec)
此时 mysql replication 已经消失了
mysql> SHOW SLAVE STATUS\G Empty set (0.00 sec)
如果你希望这个SLAVE节点继续做一个新的MASTER的slave 节点,那么请设置 gtid_purged,你应该发出一下命令(即使 gtid_puerged 看起来是空的,你也应该重置一下MASTER.)
mysql> reset master; Query OK, 0 rows affected (0.11 sec) mysql> show variables like ‘%gtid%‘; +--------------------------+-----------+ | Variable_name | Value | +--------------------------+-----------+ | enforce_gtid_consistency | ON | | gtid_executed | | | gtid_mode | ON | | gtid_next | AUTOMATIC | | gtid_owned | | | gtid_purged | | +--------------------------+-----------+ 6 rows in set (0.00 sec)
备注
我很确定我已经删除了 rely-binary-logs. (relay_log_basename variable)
场景三:拷贝数据 新建 GTID slave
Using mysqldump
在 slave 几点初始化数据库
$ ./scripts/mysql_install_db --datadir=/usr/local/mysql/data/mysql_3307
为了更简单使用 mysqldump, 是将下列参数加入配置文件
[mysqldump] quick max_allowed_packet = 16M port = 3306 socket = /usr/local/mysql/data/run/mysqld_3306.sock
之后使用下面命令导出 Master 节点的 Cherry 数据库
$ mysqldump --defaults-file=/usr/local/mysql/data/etc/my.cnf --user=root --password=`cat ~mysql/.root_password` --single-transaction --databases Cherry > backup.sql Warning: Using a password on the command line interface can be insecure. Warning: A partial dump from a server that has GTIDs will by default include the GTIDs of all transactions, even those that changed suppressed parts of the database. If you don‘t want to restore GTIDs, pass --set-gtid-purged=OFF. To make a complete dump, pass --all-databases --triggers --routines --events.
备注
–defaults-file 参数必须放在第一个位置, 否则你将会入到一个我认为已经修复的bug.
GTID 的诀窍在这里,当然它是通过 mysqldump 来实现的
$grep -i gtid 、/tmp/backup.sql -- GTID state at the beginning of the backup SET @@GLOBAL.GTID_PURGED=‘770d3753-c6e4-11e2-8e78-080027d93e15:1-8‘;
之后将dump 文件传送到 slave 节点,然后导入它;
mysql> source /tmp/backup.sql;
我最初得到了下面的错误:
ERROR 1840 (HY000): @@GLOBAL.GTID_PURGED can only be set when @@GLOBAL.GTID_EXECUTED is empty.
但是我的 gtid_executed 参数是空的:
mysql> show variables like ‘%gtid%‘; +--------------------------+------------------------------------------+ | Variable_name | Value | +--------------------------+------------------------------------------+ | enforce_gtid_consistency | ON | | gtid_executed | | | gtid_mode | ON | | gtid_next | AUTOMATIC | | gtid_owned | | | gtid_purged | 770d3753-c6e4-11e2-8e78-080027d93e15:1-6 | +--------------------------+------------------------------------------+ 6 rows in set (0.00 sec)
错误消息显示冲突 一个bug已经打开。实际上,gtid_purged也必须是空的,以便能够设置它。官方文档中引用的唯一解决方法是使用下面的命令:
mysql> DROP DATABASE replicationdb; Query OK, 1 row affected (0.07 sec) mysql> RESET MASTER; Query OK, 0 rows affected (0.06 sec) mysql> SHOW VARIABLES LIKE ‘%gtid%‘; +--------------------------+-----------+ | Variable_name | Value | +--------------------------+-----------+ | enforce_gtid_consistency | ON | | gtid_executed | | | gtid_mode | ON | | gtid_next | AUTOMATIC | | gtid_owned | | | gtid_purged | | +--------------------------+-----------+ 6 rows in set (0.01 sec)
一旦完成你可以从新导入备份文件,如果最后没有错误出现,那么现在可以设置 replication 了。
mysql> change master to master_host=‘lab.suzf.net‘, master_port=3306, master_user=‘repluser‘, master_password=‘suzf.net666‘, master_auto_position=1; Query OK, 0 rows affected, 2 warnings (0.16 sec) mysql> start slave; Query OK, 0 rows affected (0.03 sec) mysql> SHOW SLAVE STATUS\G *************************** 1. row *************************** Slave_IO_Running: Yes Slave_SQL_Running: Yes ... ... Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
如果使用的是mysql 5.5 可能会用到
change master to master_host=‘lab.suzf.net‘, master_port=3306, master_user=‘repluser‘, master_password=‘suzf.net666‘, master_log_file=‘mysql-bin.000006‘, master_log_pos=1026;
从哪里可以获得 master_log_file 和 log_position(在你导入数据时请不要将 master 设置为 read-only )
mysql> FLUSH TABLES WITH READ LOCK; mysql> SHOW MASTER STATUS;
Using XtraBackup
场景四:经典复制 –> GTID Replication
a. 按场景一中描述配置参数文件
b. 所有服务器设置global.read_only参数,等待主从服务器同步完毕;
mysql> SET @@global.read_only = ON;
c. 依次重启主从服务器;
d. 使用change master 更新主从配置;
mysql> CHANGE MASTER TO
> MASTER_HOST = host,
> MASTER_PORT = port,
> MASTER_USER = user,
> MASTER_PASSWORD = password,
> MASTER_AUTO_POSITION = 1;
e. 从库开启复制
mysql> START SLAVE;
f. 验证主从复制
FAQ
Reference : Mysql manual
未完待续 … …
License:Attribution-NonCommercial-NoDerivatives 4.0 International
本文出自 Suzf Blog。 如未注明,均为 SUZF.NET 原创。
转载请注明出处:http://suzf.net/thread-0401-696.html