MySQL复制搭建
1. 原理
参考《涂抹MySQL 跟着三思一步一步学MySQL 》这本书。
2.环境背景
操作系统 :CentOS 6.5 数据库版本:MySQL 5.6 主库A:192.168.1.202 备库B:192.168.1.203
3.mysql主从复制配置[二进制日志复制]
场景:机器上已经完成Mysql数据库安装配置可以查考《MySQL on Linux 部署手册》+Innobackupex备份环境配置;主库192.168.1.202是一台运行中的服务器。现需要为主库添加一台备库
- master端操作
3.1 master端设置server_id;启动二进制日志
[mysqld] server_id=1 log-bin=/data/mysqldata/3306/binlog/mysql-bin
3.2 创建复制帐号
(system@localhost) [mysql]> grant replication slave on *.* to 'repl'@'192.168.1.%' identified by 'li0924';
3.3 master端数据库创建数据库备份
#在备份的过程中;Master不能有写操作; (system@localhost) [mysql]> flush tables with read lock; #采用innobackupex备份方式;将备份集传到slave端 [mysql@es_mysql1 ~]$ innobackupex --defaults-file=/data/mysqldata/3306/my.cnf -u system -pli0924 --port=3606 --socket=/data/mysqldata/3306/mysql.sock /data/mysqldata/backup --stream=tar --tmpdir=/data/mysqldata/backup |gzip - > /data/mysqldata/backup/20171225.tar.gz [mysql@es_mysql1 ~]$ rsync /data/mysqldata/backup/20171225.tar.gz mysql@192.168.1.203:/data/mysqldata/backup/ #备份完成之后;释放锁 (system@localhost) [mysql]> unlock tables;
slave端复制环境
3.4 配置my.cnf参数
#将master端参数文件复制到slave端;设置server_id;启动二进制日志 [mysqld] server_id=3 log-bin=/data/mysqldata/3306/binlog/mysql-bin
3.5 slave复制mysql
[mysql@localhost ~]$ tar -zxvf /data/mysqldata/backup/20171225.tar.gz [mysql@localhost ~]$ innobackupex --apply-log /data/mysqldata/backup/ [mysql@localhost ~]$ innobackupex --defaults-file=/data/mysqldata/3306/my.cnf -u system -pli0924 --port=3606 --copy-back --rsync /data/mysqldata/backup/
3.6 配置slave节点环境
# 启动数据库 [mysql@localhost ~]$ mysqld_safe --defaults-file=/data/mysqldata/3306/my.cnf & # 查看备份集信息 [mysql@localhost ~]$ cat /data/mysqldata/backup/xtrabackup_binlog_info mysql-bin.000010 120 # 配置到master端的连接 (system@localhost) [(none)]>CHANGE MASTER TO -> MASTER_HOST='192.168.1.202', -> MASTER_PORT=3306, -> MASTER_USER='repl', -> MASTER_PASSWORD='li0924', -> MASTER_LOG_FILE='mysql-bin.000010', -> MASTER_LOG_POS=120; Query OK, 0 rows affected, 2 warnings (0.06 sec)
3.7启动slave进程
(system@localhost) [(none)]>use mysql Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed (system@localhost) [mysql]>start slave; Query OK, 0 rows affected (0.09 sec)
3.8检查状态
(system@localhost) [mysql]>show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.1.202 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000010 Read_Master_Log_Pos: 315 Relay_Log_File: es_mysql1-relay-bin.000002 Relay_Log_Pos: 478 Relay_Master_Log_File: mysql-bin.000010 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 315 Relay_Log_Space: 655 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1 Master_UUID: 39d7694d-e57d-11e7-b0a6-000c29157f7a Master_Info_File: /data/mysqldata/3306/data/master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0 1 row in set (0.00 sec)