您的位置:首页 > 运维架构 > Linux

linux 数据库主从同步配置

2015-03-11 14:09 274 查看
一、配置MySQL主服务器(192.168.1.128)的my.cnf文件

vi /etc/my.cnf #编辑配置文件,在[mysqld]部分添加下面内容

server-id=1 #设置服务器id,为1表示主服务器,注意:如果原来的配置文件中已经有这一行,就不用再添加了。

log-bin=mysql-bin #启动MySQ二进制日志系统,注意:如果原来的配置文件中已经有这一行,就不用再添加了。

binlog-do-db=osyunweidb #需要同步的数据库名,如果有多个数据库,可重复此参数,每个数据库一行

binlog-ignore-db=mysql #不同步mysql系统数据库

:wq! #保存退出

service mysqld restart #重启MySQL

mysql -u root -p #进入mysql控制台

show variables like 'server_id'; #查看server-id的值是否为1

mysql> show variables like 'server_id';

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

| Variable_name | Value |

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

| server_id | 1 |

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

1 row in set (0.00 sec)

show master status; #查看主服务器,出现以下类似信息

mysql> show master status;

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

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |

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

| mysql-bin.000011 | 107 | osyunweidb | mysql |

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

1 row in set (0.00 sec)

注意:这里记住File的值:mysql-bin.000011和Position的值:107,后面会用到。

二、配置MySQL从服务器(192.168.21.129)的my.cnf文件

vi /etc/my.cnf #编辑配置文件,在[mysqld]部分添加下面内容

server-id=2 #设置服务器id,修改其值为2,表示为从数据库

log-bin=mysql-bin #启动MySQ二进制日志系统,注意:如果原来的配置文件中已经有这一行,就不用再添加了。

replicate-do-db=osyunweidb #需要同步的数据库名,如果有多个数据库,可重复此参数,每个数据库一行

replicate-ignore-db=mysql #不同步mysql系统数据库

read_only #设置数据库只读

:wq! #保存退出

service mysqld restart #重启MySQL

mysql -u root -p #进入MySQL控制台

show variables like 'server_id'; #查看server-id的值,必须为上面设置的2,否则请返回修改配置文件

mysql> show variables like 'server_id';

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

| Variable_name | Value |

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

| server_id | 2 |

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

1 row in set (0.01 sec)

slave stop; #停止slave同步进程

change master to master_host='192.168.1.128',master_user='osyunweidbbak',master_password='123456',master_log_file='mysql-bin.000011' ,master_log_pos=107; #执行同步语句

slave start; #开启slave同步进程

SHOW SLAVE STATUS\G #查看slave同步信息,出现以下内容

mysql> SHOW SLAVE STATUS\G

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

Slave_IO_State: Waiting for master to send event

Master_Host: 192.168.1.128

Master_User: osyunweidbbak

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000011

Read_Master_Log_Pos: 107

Relay_Log_File: mysqlslave-relay-bin.000004

Relay_Log_Pos: 253

Relay_Master_Log_File: mysql-bin.000011

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

Replicate_Do_DB: osyunweidb

Replicate_Ignore_DB: mysql

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: 107

Relay_Log_Space: 560

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

1 row in set (0.00 sec)

mysql>

注意查看:

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

以上这两个参数的值为Yes,即说明配置成功!

测试篇

测试MySQL主从服务器是否正常运行

1、进入MySQL主服务器(192.168.21.128)

mysql -u root -p #进入MySQL控制台

use osyunweidb #进入数据库

CREATE TABLE test ( id int not null primary key,name char(20) ); #创建test表

2、进入MySQL从服务器

mysql -u root -p #进入MySQL控制台

use osyunweidb #进入数据库

show tables; #查看osyunweidb表结构,会看到有一个新建的表test,表示数据库同步成功

mysql> show tables;

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

| Tables_in_osyunweidb |

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

| test |

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

1 row in set (0.00 sec)

错误处理

mysql 主从集群做好了后,这时候一不小心从主库上删掉一个数据库数据,从库没有这个数据库,这时候 Slave_SQL_Running: NO,会变成NO,解决方法有两种如下

记录File和Position对应的值。

mysql> show master status;

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

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |

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

| mysql-bin.000013 | 330748356| | |

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

1 row in set (0.00 sec)

到slave服务器上执行手动同步:

mysql> slave stop;

mysql> change master to master_log_file='mysql-bin.000013', master_log_pos=330748356;

1 row in set (0.00 sec)

mysql> slave start;

1 row in set (0.00 sec)

再次查看slave状态发现:

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

...

Seconds_Behind_Master: 0

解决办法二:

mysql> slave stop;

mysql> set GLOBAL SQL_SLAVE_SKIP_COUNTER=1;

mysql> slave start;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: