实例解读mysqldump参数--master-data
先来看看官方文档的解释
Use this option to dump a master replication server to produce a dump file that can be used to set up another server as a slave of the master. It causes the dump output to include a CHANGE MASTER TO statement that indicates the binary log coordinates (file name and position) of the dumped server. These are the master server coordinates from which the slave should start replicating.
If the option value is 2, the CHANGE MASTER TO statement is written as an SQL comment, and thus is informative only; it has no effect when the dump file is reloaded. If the option value is 1, the statement takes effect when the dump file is reloaded. If the option value is not specified, the default value is 1.
大概就这么个意思:这个参数在建立slave数据库的时候会用到,当这个参数的值为1的时候,mysqldump出来的文件就会包括CHANGE MASTER TO这个语句,CHANGE MASTER TO后面紧接着就是file和position的记录,file和position记录的位置就是slave从master端复制文件的起始位置。默认情况下这个值是1
当这个值是2的时候,chang master to也是会写到dump文件里面去的,但是不会有上面那个作用了(thus is information only)
翻译过来真感觉拗口,呵呵,凑活看看吧。上实例!
一、先dump一个库
[root@HI0-OA-nagios ~]# mysqldump -uroot -pdfs123 --master-data=1 nagios > dumpfile
二、观察file和position的值,此时的table是被lock住不能写入的
mysql> show master status;
+------------------+-----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+-----------+--------------+------------------+
| mysql-bin.000016 | 760292258 | | |
+------------------+-----------+--------------+------------------+
1 row in set (0.00 sec)
mysql> show master status;
+------------------+-----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+-----------+--------------+------------------+
| mysql-bin.000016 | 760292258 | | |
+------------------+-----------+--------------+------------------+
1 row in set (0.00 sec)
mysql> show master status;
+------------------+-----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+-----------+--------------+------------------+
| mysql-bin.000016 | 760292258 | | |
+------------------+-----------+--------------+------------------+
1 row in set (0.00 sec)
三、观察dump出来的文件,file和position的值和上面是相同的
[root@HI0-OA-nagios ~]# grep -i "CHANGE MASTER TO" dumpfile
CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000016', MASTER_LOG_POS=760292258;
四、编辑slave端配置文件如下
[mysqld]
port = 3306
socket = /usr/local/mysql/var/mysql.sock
skip-locking
key_buffer = 64M
max_allowed_packet = 16M
table_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
log-bin=mysql-bin
server-id = 3
master-host=10.83.200.153
master-port=3306
master-user=repl
master-password=slavepass
master-connect-retry=60
五、重启slave端mysql
Service mysql restart
六、此时netstat -an|grep 3306 看到slave和master的连接已经建立。现在不需要它们之间连接,登陆slave端,执行stop slave,连接成功断开
七、Mysqldump 导入master 导出的文件dumpfile
八、开启slave端同步
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
在这一步骤,如果--master-data 参数为二,此时你会发现你还需要输入
CHANGE MASTER TO
MASTER_HOST='master2.mycompany.com',
MASTER_USER='replication',
MASTER_PASSWORD='bigs3cret',
MASTER_PORT=3306,
MASTER_LOG_FILE='master2-bin.001',
MASTER_LOG_POS=4,
MASTER_CONNECT_RETRY=10;
类似的参数后,才能继续执行start slave。当然这里你也可以写到配置文件中去
九、查看slave端状态,大功告成
mysql> show processlist\G;
*************************** 1. row ***************************
Id: 6
User: root
Host: localhost
db: nagios
Command: Query
Time: 0
State: NULL
Info: show processlist
*************************** 2. row ***************************
Id: 7
User: system user
Host:
db: NULL
Command: Connect
Time: 27
State: Waiting for master to send event
Info: NULL
*************************** 3. row ***************************
Id: 8
User: system user
Host:
db: nagios
Command: Connect
Time: 2958
State: updating
Info: DELETE FROM nagios_servicechecks WHERE instance_id='1' AND start_time
3 rows in set (0.01 sec)