您的位置:首页 > 数据库 > MySQL

mysql主从同步配置

2014-09-25 17:26 501 查看
一、环境
master:192.168.124.51
MYSQL版本:5.1.48-community-log
slave: 192.168.124.52
MYSQL版本:5.1.48-community-log

二、主从数据库
将主机上现有的数据库备份,然后在从机上建立同名数据库并还原。
(这次做的是51上的两个数据库database1和database2)

三、master和 slave上的相关配置
在/etc目录下可能无my.cnf文件,
从/user/share/mysql目录中拷贝my-medium.cnf 到/etc并修改成my.cnf(master和slave上一样)。

如 # cp /user/share/mysql/my-medium.cnf /etc/my.cnf

1.修改master上的配置文件my.cnf。
在[mysqld]下添加如下字段:
server-id=1
log-bin=log
binlog-do-db=database1 //需要同步的数据库
binlog-do-db=database2
binlog-ignore-db=mysql //被忽略的数据库

在master上为slave添加一个同步账号

grant replication slave on *.* to 'AffairLog'@'192.168.124.52' identified by 'password';
//在slave上登陆成功
重启master的mysql服务:
service mysql restart;

用show master status命令查看日志情况
mysql> show master status\\G;

*************************** 1. row ***************************
File: log.000027
Position: 3151
Binlog_Do_DB: database1,database2
Binlog_Ignore_DB:
1 row in set (0.00 sec)

2.修改slave上的配置文件my.cnf。
在[mysqld]下添加如下字段:
server-id=2
master-host=192.168.124.51
master-user= AffairLog
master-password= password
master-port=3306
master-connect-retry=60
replicate-do-db=database1 //同步的数据库
replicate-do-db=database2
replicate-ignore-db=mysql //被忽略的数据库

重启slave的mysql服务:
service mysql restart;

在进入slave机中的mysql。
mysql>start slave;
mysql>show slave status\\G;

*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.124.51
Master_User: AffairLog
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: log.000027
Read_Master_Log_Pos: 3151
Relay_Log_File: localhost-relay-bin.000379
Relay_Log_Pos: 245
Relay_Master_Log_File: log.000027
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: database1,database2
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: 3151
Relay_Log_Space: 543
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:
1 row in set (0.00 sec)

如果Slave_IO_Running、Slave_SQL_Running状态为Yes则表明设置成功。

四、出现问题
Slave_IO_Running: No或者Slave_SQL_Running: No
1.停掉slave服务
mysql> slave stop;
Query OK, 0 rows affected (2.01 sec)

2.解决办法
解决办法1
a.在master上查看。
mysql> show master status\\G;
*************************** 1. row ***************************
File: log.000027
Position: 3151
Binlog_Do_DB: database1,database2
Binlog_Ignore_DB:
1 row in set (0.00 sec)
b.到slave上手动同步。
mysql>change master to
>master_host='192.168.124.51',
>master_user='AffairLog',
>master_password='password',
>master_log_file='log.000027',
>master_log_pos=3151;

Query OK, 0 rows affected (0.00 sec)

解决方法2
mysql> slave stop;
mysql> SET GLOBAL SQL_SLAVE_SKIP_COUNTER=1;
mysql> slave start;

3.启动slave服务
mysql> slave start;

4.再次查看Slave_IO_Running、Slave_SQL_Running状态,为Yes则表明设置成功。

PS:
Slave_IO_Running:连接到主库,并读取主库的日志到本地,生成本地日志文件
Slave_SQL_Running:读取本地日志文件,并执行日志里的SQL命令。
Mysql主主循环同步
Mysql主主循环同步区别于Mysql主从同步。

在主主循环同步中,两台服务器的任何一台上的数据库发生了改变都会同步到另一台服务器上,这样两台服务器互为主从,并且都能向外提供服务。
一、环境
masterA:192.168.2.182
MySQL版本:5.1.48-community-log
masterB: 192.168.2.183
MySQL版本:5.1.48-community-log

二、数据库初始化。
首先保证两台服务器上的数据库数据相同,可以用还原同一备份文件或备份其中一台在另一台上还原来实现。
(涉及两个数据库10086_Personal_Stereo和10086_personal_stereo_issue)

三、创建用户并授权。
在masterA上为masterB添加一个同步账号:用户名ATest,密码123
grant replication slave on *.* to 'ATest'@'192.168.2.183' identified by '123';

在masterB上位masterA添加一个同步账号:用户名ATest2,密码123
grant replication slave on *.* to 'ATest2'@'192.168.2.182' identified by '123';

四、修改masterA和 masterB上的相关配置文件。
在/etc目录下可能无my.cnf文件,从/user/share/mysql目录中拷贝my-medium.cnf 到/etc并修改成my.cnf。
如 # cp /user/share/mysql/my-medium.cnf /etc/my.cnf
1.修改masterA(182)上的配置文件my.cnf。
在[mysqld]下添加如下字段:
server-id=1
log-bin=mysql-bin
replicate-do-db=10086_Personal_Stereo
replicate-do-db=10086_personal_stereo_issue
auto-increment-increment=2
auto-increment-offset=1

2.修改masterB(183)上的配置文件my.cnf。
在[mysqld]下添加如下字段:
server-id=2
log-bin=mysql-bin
replicate-do-db=10086_Personal_Stereo
replicate-do-db=10086_personal_stereo_issue
auto-increment-increment=2
auto-increment-offset=2

PS:两者只有server-id不同和auto-increment-offset不同
replicate-do-db指定同步的数据库,我们只在两台服务器间同步10086_Personal_Stereo和10086_personal_stereo_issue。
auto-increment-offset是用来设定数据库中自动增长的起点的,会为这两台服务器都设定一个自动增长值2,

所以它们的起点必须得不同,这样才能避免两台服务器数据同步时出现主键冲突。

auto-increment-increment的值应设为整个结构中服务器的总数,本方案中涉及了两台服务器,所以值设置为2。

五、重启mysql服务。
在masterA和masterB上分别重启mysql服务:
service mysql restart;

六、用show master status命令查看日志情况。
1.在masterA上:
用show master status命令查看日志情况
mysql> show master status\\G;
*************************** 1. row ***************************
File: mysql-bin.000026
Position: 106
Binlog_Do_DB:
Binlog_Ignore_DB:
1 row in set (0.00 sec)

2.在masterB上:
用show master status命令查看日志情况
mysql> show master status\\G;
*************************** 1. row ***************************
File: mysql-bin.000042
Position: 106
Binlog_Do_DB:
Binlog_Ignore_DB:
1 row in set (0.00 sec)

七、关闭slave功能。
1.在masterA上
关闭slave功能:
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)

1.在masterB上
关闭slave功能:
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)

八、互相通告二进制日志位置。
在masterA上:
mysql> change master to
-> master_host='192.168.2.183',
-> master_user='ATest2',
-> master_password='123',
-> master_log_file='mysql-bin.000042',
-> master_log_pos=106;
Query OK, 0 rows affected (0.00 sec)

在masterB上:
mysql> change master to
-> master_host='192.168.2.182',
-> master_user='ATest',
-> master_password='123',
-> master_log_file='mysql-bin.000026',
-> master_log_pos=106;
Query OK, 0 rows affected (0.00 sec)

九、启动并确认主主同步功能。
1.在masterA上
开启slave功能:
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

查看同步情况:
mysql> show slave status\\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.2.183
Master_User: ATest2
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000042
Read_Master_Log_Pos: 338
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 483
Relay_Master_Log_File: mysql-bin.000042
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: 10086_Personal_Stereo,10086_personal_stereo_issue
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: 338
Relay_Log_Space: 642
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:
1 row in set (0.00 sec)
2.在masterB上
开启slave功能:
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

查看同步情况:
mysql> show slave status\\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.2.182
Master_User: ATest
Master_Port: 3306
Connect_Retry: 300
Master_Log_File: mysql-bin.000026
Read_Master_Log_Pos: 325
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 470
Relay_Master_Log_File: mysql-bin.000026
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: 10086_Personal_Stereo,10086_personal_stereo_issue
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: 325
Relay_Log_Space: 629
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:
1 row in set (0.00 sec)
如果masterA和masterB上Slave_IO_Running、Slave_SQL_Running状态为Yes则表明设置成功。

PS:
Slave_IO_Running:连接到主库,并读取主库的日志到本地,生成本地日志文件
Slave_SQL_Running:读取本地日志文件,并执行日志里的SQL命令。
在实际企业应用环境当中,单台mysql数据库是不足以满足日后业务需求的。譬如服务器发生故障,没有备份服务器来提供服务的话,业务就得停止。介于这种情况,我们来学习一下mysql主从复制。

使用mysql主从复制的好处有:

1、采用主从服务器这种架构,稳定性得以提升。如果主服务器发生故障,我们可以使用从服务器来提供服务。

2、在主从服务器上分开处理用户的请求,可以提升数据处理效率。

3、将主服务器上的数据复制到从服务器上,保护数据免受意外的损失。

环境描述:

新企业要搭建架构为主从复制的mysql数据库。

主服务器(mysql-master):IP地址:192.168.48.128,mysql已安装,没有用户数据。

从服务器(mysql-slave):IP地址:192.168.48.130,mysql已安装,没有用户数据。

主从服务器均可正常提供服务。

01

主从复制配置如下:

在主服务器上操作:

1)、确保/etc/my.cnf中有如下参数,没有的话需手工添加,并重启mysql服务。

[mysqld]

log-bin=mysql-bin 启动二进制文件

server-id=1 服务器ID

2)、登录mysql,在mysql中添加一个backup的账号,并授权给从服务器。

[root@localhost ~]# mysql -uroot –p123456 登录mysql

mysql> grant replication slave on *.* to 'backup'@'192.168.48.130' identified by 'backup';

创建backup用户,并授权给192.168.48.130使用。

02

3)、查询主数据库状态,并记下FILE及Position的值,这个在后面配置从服务器的时候要用到。

mysql> show master status;

03

在从服务器上操作:

1)、确保/etc/my.cnf中有
log-bin=mysql-bin和server-id=1参数,并把server-id=1修改为server-id=10。修改之后如下所示:

[mysqld]

log-bin=mysql-bin 启动二进制文件

server-id=10 服务器ID

2)、重启mysql服务。

[root@localhost ~]# mysqladmin -p123456 shutdown

[root@localhost ~]# mysqld_safe --user=mysql &

04

3)、登录mysql,执行如下语句

[root@localhost ~]# mysql -uroot –p123456

mysql> change master to master_host='192.168.48.128',master_user='backup',master_password='backup',master_log_file='mysql-bin.000003',master_log_pos=401;

05

4)、启动slave同步。

mysql> start slave;

06

5)、检查主从同步,如果您看到Slave_IO_Running和Slave_SQL_Running均为Yes,则主从复制连接正常。

mysql> show slave status\\G

07

验证配置是否正常,mysql主从能否正常复制。

环境如下:

单台数据库存有数据,要升级为主从复制的架构。

主数据库:mysql-master:IP192.168.48.128,数据库正常提供服务,有数据。

从数据库:mysql-slave:IP192.168.48.130,数据库正常提供服务,无数据。

01

升级大致步骤如下:

1、修改主数据库配置文件,开启总从复制必要的功能。

2、在主数据库中创建一个账号并授权给从数据库使用。

3、导出主数据库中的数据作。

4、修改从数据库配置文件,开启总从复制必要的功能。

5、把刚才主数据库中导出的数据导入到从数据库。

6、在从数据库中配置连接主数据库要用到的账户、密码等。

7、启动主从复制功能,并检查是否正常复制。

开始升级:

在主数据库上操作:

1)、确保/etc/my.cnf中有如下参数,没有的话需手工添加,并重启mysql服务。

[mysqld]

log-bin=mysql-bin 启动二进制文件

server-id=1 服务器ID

2)、登录mysql,在mysql中添加一个backup的账号,并授权给从服务器。

[root@localhost ~]# mysql -uroot –p123456 登录mysql

mysql> grant replication slave on *.* to 'backup'@'192.168.48.130' identified by 'backup';

创建backup用户,并授权给192.168.48.130使用。

3)、查看已有的数据库有哪些;

mysql> show databases;

4)、进行锁表操作,不让数据进行写入动作,这么做事为了防止从数据库的原始数据和主数据库的原始数据不一致。

mysql> flush tables with read lock;

5)、查询主数据库状态,并记下FILE及Position的值,这个在后面配置从服务器的时候要用到。

mysql> show master status;

6)、切换一个终端,使用mysqldump命令将刚才查询到的两个库导出来。

mysql> mysqldump –uroot –p123456 mysqltest > mysqltest.sql

mysql> mysqldump –uroot –p123456 test1234 > test1234.sql

7)、将导出来的库文件传送到从数据库的/root目录下。

[root@localhost ~]#scp mysqltest.sql test1234.sql root@192.168.48.130:/root/

8)、切换回之前的终端,进行表解锁操作。

mysql> unlock tables;

07

主数据库服务器上的操作告一段落。

在从数据库上操作:

1)、确保/etc/my.cnf中有log-bin=mysql-bin和server-id=1参数,并把server-id=1修改为server-id=10。修改之后如下所示:

[mysqld]

log-bin=mysql-bin 启动二进制文件

server-id=10 服务器ID

2)、重启mysql服务。

[root@localhost ~]# mysqladmin -p123456 shutdown
[root@localhost ~]# mysqld_safe --user=mysql &

08

3)、登录数据库,确认要同步的库名不存在。

[root@localhost ~]# mysql -uroot –p123456

mysql> show databases;

09

4)、创建名为mysqltest和test1234的库。

mysql> create database mysqltest;

mysql> create database test1234;

10

4)、切换一个终端,将传过来的两个数据文件分别导入对应的数据库下。

[root@localhost ~]# mysql -uroot –p123456 mysqltest < mysqltest.sql

[root@localhost ~]# mysql -uroot –p123456 test1234 < test1234.sql

5)、切换回之前的终端,执行如下语句

mysql> change master to master_host='192.168.48.128',master_user='backup',master_password='backup',master_log_file='mysql-bin.000003',master_log_pos=1650;

5)、切换回之前的终端,执行如下语句

mysql> change master to master_host='192.168.48.128',master_user='backup',master_password='backup',master_log_file='mysql-bin.000003',master_log_pos=1650;

6)、启动主从复制功能。
mysql> start slave;

7)、
检查主从同步,如果您看到Slave_IO_Running和Slave_SQL_Running均为Yes,则主从复制连接正常。

mysql> show slave status\\G
使用如下命令查看了一下server_id

show variables like 'server_id';

发现,mysql并没有从my.cnf文件中更新server_id,既然这样就只能手动修改了

mysql> set global server_id=10; #此处的数值和my.cnf里设置的一样就行
mysql> slave start;

看到mysql在启动的时候会查找/etc/my.cnf、DATADIR/my.cnf,USER_HOME/my.cnf。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息