配置MySQL主从复制
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了配置MySQL主从复制相关的知识,希望对你有一定的参考价值。
说明
mysql版本为5.6.26
安装MySQL步骤就省略了。
MySQL主从复制注意点
1、在master端要启用binlog日志。slave端不用启用binlog日志
2、master和slave的server-id要不一样
主从my.cnf配置的区别
不列出server-id了,设置不同即可。主要是[mysqld]区域的 不同
master
log-bin =/data/mysql_log/mysql-bin
expire-logs-days = 14
sync-binlog = 1
binlog_format = row
slave
relay-log =/data/mysql_log/mysql-relay.log
relay-log-index =/data/mysql_log/mysql-relay.index
架构图
系统环境
# cat /etc/redhat-release
CentOS release 6.6 (Final)
# uname -rm
2.6.32-504.el6.x86_64 x86_64
在主库上创建数据库并插入数据
mysql> create databasekevin;
Query OK, 1 row affected(0.00 sec)
mysql> use kevin
Database changed
mysql> create tablestudent(
-> id int(4) not null auto_increment,
-> name char(20) not null,
-> primary key(id),
-> key index_name(name)
-> )AUTO_INCREMENT=1;
Query OK, 0 rows affected(0.10 sec)
mysql> insert intostudent values(1,‘Kevin‘),(2,‘Dave‘),(3,‘Danny‘),(4,‘Jenny‘),(5,‘Jerry‘);
Query OK, 5 rows affected(0.02 sec)
Records: 5 Duplicates: 0 Warnings: 0
创建主从复制
在master上操作
建立同步用户
mysql> GRANT REPLICATIONSLAVE ON *.* TO ‘repuser‘@‘192.168.56.%‘ IDENTIFIED BY ‘123456‘;
Query OK, 0 rows affected(0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected(0.00 sec)
mysql> FLUSH LOGS;
Query OK, 0 rows affected(0.00 sec)
mysql> show binary logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 2189 |
| mysql-bin.000002 | 323 |
+------------------+-----------+
2 rows in set (0.00 sec)
mysql> \q
Bye
备份数据库
[[email protected] ~]#innobackupex --user=root --password=123456 /backup/
准备备份
[[email protected] ~]#innobackupex --apply-log /backup/2016-05-18_19-19-38/
在插入一条数据
mysql> use kevin
Database changed
mysql> insert intostudent values(6,‘Lucy‘);
Query OK, 1 row affected(0.14 sec)
mysql> select * fromstudent\G
***************************1. row ***************************
id: 3
name: Danny
***************************2. row ***************************
id: 2
name: Dave
***************************3. row ***************************
id: 4
name: Jenny
***************************4. row ***************************
id: 5
name: Jerry
***************************5. row ***************************
id: 1
name: Kevin
***************************6. row ***************************
id: 6
name: Lucy
6 rows in set (0.00 sec)
mysql> show masterstatus\G
将备份拷贝到从库上
[[email protected] ~]# scp-r /backup/2016-05-18_19-19-38 [email protected]:/backup/
slave上操作
删除原始数据
[[email protected] ~]# cd/data/mysql_data/
[[email protected]_data]# rm -fr *
[[email protected]_data]# cd
恢复数据
[[email protected] ~]#innobackupex --copy-back /backup/2016-05-18_19-19-38/
[[email protected] ~]#chown -R mysql.mysql /data/mysql_data/
启动MySQL
[[email protected] ~]#/etc/init.d/mysqld start
验证数据
[[email protected] ~]#mysql -uroot –p
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| kevin |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.04 sec)
mysql> use kevin;
Database changed
mysql> select * fromstudent\g
+----+-------+
| id | name |
+----+-------+
| 3 | Danny |
| 2 | Dave |
| 4 | Jenny |
| 5 | Jerry |
| 1 | Kevin |
+----+-------+
5 rows in set (0.07 sec)
从从库上读主库的binlog
mysql> change master tomaster_host=‘192.168.56.12‘,master_port=3306,master_user=‘repuser‘,master_password=‘123456‘,master_log_file=‘mysql-bin.000001‘,master_log_pos=2189;
启动slave
mysql> start slave;或者执行START SLAVEIO_Thread;和START SLAVE SQL_Thread;
mysql> select * fromstudent;
+----+-------+
| id | name |
+----+-------+
| 3 | Danny |
| 2 | Dave |
| 4 | Jenny |
| 5 | Jerry |
| 1 | Kevin |
| 6 | Lucy |
+----+-------+
6 rows in set (0.00 sec)
现在数据完全同步
在slave2上进行与slave1的完全一样的操作即可。
由于个人技术所限有不足之处还请各位指出。可以通过以下两个群找到笔者。
北京linux运维求职招聘群:153677549
Linux运维开发群:298324302
本文出自 “长街听风人” 博客,请务必保留此出处http://kevinhao.blog.51cto.com/5204735/1775132
以上是关于配置MySQL主从复制的主要内容,如果未能解决你的问题,请参考以下文章