码迷,mamicode.com
首页 > 数据库 > 详细

MariaDB、MySQL双主互备模型同步数据

时间:2015-01-21 20:19:56      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:mysql双主互备

    MySQL双主互备即两台MySQL服务器同时是主服务器,而同时又是对方的从服务器,这样可以分担主服务器的一部分写请求,因为如果是单主模型的话数据写入只能写入在一台MySQL服务器,而双主模型则可同时写在两台服务器上,有效加快了写入的速度,而且还能提供数据的备份功能。


环境如下:

server1:CentOS 6.5 x86_64 mariadb 10.0.10 IP:192.168.10.204

server2:CentOS 6.5 x86_64 mariadb 10.0.10  IP:192.168.10.205


一:服务器的my.cnf配置:

Server1:my.cnf全部配置如下:

[root@node4 ~]# grep -v "#"  /etc/mysql/my.cnf 

[client]

port = 3306

socket = /tmp/mysql.sock


[mysqld]

port = 3306

socket = /tmp/mysql.sock

skip-external-locking

key_buffer_size = 256M

max_allowed_packet = 1M

table_open_cache = 256

sort_buffer_size = 1M

read_buffer_size = 1M

read_rnd_buffer_size = 4M

myisam_sort_buffer_size = 64M

thread_cache_size = 8

query_cache_size= 16M

thread_concurrency = 4

datadir = /data/mydata

innodb_file_per_table = ON  #每一条事物完成后就写入二进制

log-bin=/data/binlogs/master-bin #二进制日志目录

binlog_format=mixed

server-id = 100 #两台服务器的server-id绝不可以相同

auto_increment_increment=2 # 定义每次增长间隔为2即步长,起始值为1步长为2表示每次增长就加2,1、3、5、7、9,用于和从服务器区分值

auto_increment_offset=1 # 定义主键自动增长起始值

skip_slave_start  #跳过slave备份线程,即服务启东时候不自动打开

relay_log = /data/relaylogs/relay-bin  #中继日志路径


[mysqldump]

quick

max_allowed_packet = 16M


[mysql]

no-auto-rehash


[myisamchk]

key_buffer_size = 128M

sort_buffer_size = 128M

read_buffer = 2M

write_buffer = 2M


[mysqlhotcopy]

interactive-timeou


server2 my.cnf全部配置:

[root@node5 ~]# grep -v "#" /etc/mysql/my.cnf 

[client]

port = 3306

socket = /tmp/mysql.sock


[mysqld]

port = 3306

socket = /tmp/mysql.sock

skip-external-locking

key_buffer_size = 256M

max_allowed_packet = 1M

table_open_cache = 256

sort_buffer_size = 1M

read_buffer_size = 1M

read_rnd_buffer_size = 4M

myisam_sort_buffer_size = 64M

thread_cache_size = 8

query_cache_size= 16M

thread_concurrency = 4

innodb_file_per_table = ON

datadir = /data/mydata

log-bin=/data/binlogs/master-bin

binlog_format=mixed

server-id = 4

auto_increment_increment=2

auto_increment_offset=2

skip_slave_start

relay_log = /data/relaylogs/relay-bin


[mysqldump]

quick

max_allowed_packet = 16M


[mysql]

no-auto-rehash


[myisamchk]

key_buffer_size = 128M

sort_buffer_size = 128M

read_buffer = 2M

write_buffer = 2M


[mysqlhotcopy]

interactive-timeout


然后启动mysqld服务


二:授权复制并启动复制:

1、server1:

mysql> grant replication slave,file on *.* to ‘tom‘@‘192.168.%.%‘ identified by ‘123456‘;

mysql> flush privileges;


2、server2:

mysql> grant replication slave,file on *.* to ‘tom‘@‘192.168.%.%‘ identified by ‘123456‘;

mysql> flush privileges;


3、在各个server指向对方当主服务器:

server1:

mysql> CHANGE MASTER TO     MASTER_HOST=‘192.168.10.205‘,MASTER_USER=‘tom‘,MASTER_PASSWORD=‘123456‘,MASTER_LOG_FILE=‘master-bin.000004‘,MASTER_LOG_POS=1160;

   注:MASTER_LOG_FILE=‘master-bin.000004‘,MASTER_LOG_POS=1160; 是对方主服务器192.168.10.205查看show master status得知


server2:

mysql>  CHANGE MASTER TO     MASTER_HOST=‘192.168.10.204‘,MASTER_USER=‘tom‘,MASTER_PASSWORD=‘123456‘,MASTER_LOG_FILE=‘master-bin.000003‘,MASTER_LOG_POS=526;

     注:MASTER_LOG_FILE=‘master-bin.000004‘,MASTER_LOG_POS=1160; 是对方主服务器192.168.10.204查看show master status得知


4、启动slave线程:

server1:

mysql> start slave;

    Query OK, 0 rows affected, 1 warning (0.00 sec)


server2:

mysql> start slave;

    Query OK, 0 rows affected, 1 warning (0.00 sec)


5、查看状态:

serever1:

mysql> show slave status\G

    *************************** 1. row ***************************

    Slave_IO_State: Waiting for master to send event

    Master_Host: 192.168.10.205

    Master_User: tom

    Master_Port: 3306

    Connect_Retry: 60

    Master_Log_File: master-bin.000004

    Read_Master_Log_Pos: 1160

    Relay_Log_File: relay-bin.000002

    Relay_Log_Pos: 536

    Relay_Master_Log_File: master-bin.000004

    Slave_IO_Running: Yes     #

    Slave_SQL_Running: Yes  #此两个线程一定要启动


server2:

mysql>show slave status\G

    ***************************1. row ***************************

    Slave_IO_State:Waiting for master to send event

    Master_Host:192.168.10.204

    Master_User:tom

    Master_Port:3306

    Connect_Retry:60

    Master_Log_File:master-bin.000003

    Read_Master_Log_Pos:843

    Relay_Log_File:relay-bin.000002

    Relay_Log_Pos:853

    Relay_Master_Log_File:master-bin.000003

    Slave_IO_Running:Yes

    Slave_SQL_Running:Yes



6、在任意一个server创建库和表并插入内容差可能能否同步:

server1:

mysql> create database linux;

    Query OK, 1 row affected (0.00 sec)

server2:

mysql> show databases;

    +--------------------+

    | Database           |

    +--------------------+

    | information_schema |

    | linux              |

    | mysql              |

    | performance_schema |

    | test               |

    +--------------------+

    5 rows in set (0.04 sec)

mysql> use linux;

    Database changed

mysql> create table t1 (id int);

    Query OK, 0 rows affected (0.16 sec)

mysql> insert into t1 values (1),(2),(3);

    Query OK, 3 rows affected (0.08 sec)

    Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from t1;

    +------+

    | id   |

    +------+

    |    1 |

    |    2 |

    |    3 |

    +------+

    3 rows in set (0.00 sec)

server1:

mysql>  insert into t1 values (4),(5),(6);

    Query OK, 3 rows affected (0.00 sec)

    Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from t1;

    +------+

    | id   |

    +------+

    |    1 |

    |    2 |

    |    3 |

    |    4 |

    |    5 |

    |    6 |

    +------+

    6 rows in set (0.00 sec)



本文出自 “Linux” 博客,请务必保留此出处http://zhangshijie.blog.51cto.com/806066/1606725

MariaDB、MySQL双主互备模型同步数据

标签:mysql双主互备

原文地址:http://zhangshijie.blog.51cto.com/806066/1606725

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!