MySQL主从配置 联系客服

发布时间 : 星期日 文章MySQL主从配置更新完毕开始阅读40db04bc2f60ddccdb38a02f

一.需求问题

假设目前有两台 MySQL 数据库服务器,如何实现这两台机器的数据同步问题?即在一台机器上修改数据库后,另一台机器会同步更新所修改的信息。

二.解决方案

查资料发现 MySQL 支持单向,异步复制,复制过程中一个服务器充当主服务器,而另一个或多个其他服务器充当从服务器。

主服务器将更新写入二进制日志文件,并维护文件的一个索引来跟踪日志循环。这些日志可以记录发送到从服务器的更新。当一个从服务器连接主服务器时,它通知主服务器从服务器在日志中读取的最后一次成功更新的位置。从服务器接受从那时起发生的任何更新,然后封锁并等待主服务器通知新的更新。

2.1 测试环境

1. Master : 10.176.190.111 (CentOS 6.5 x86_64 ) MySQL Version : 5.1

2. Slave: 10.176.190.113 (CentOS 6.5 x86_64 ) MySQL Version : 5.1

备注:Master 和 slave 端的 MySQL 版本最好要一样的,或者 Master 端的版本高于 Slave 端

2.2 配置过程 2.2.1 Master 端设置

开启 MySQL 服务并新建一个测试数据库 abc:

1. root@camlit ~: /etc/init.d/mysqld start 2. jian.ma@camlit ~: mysql -u root -p 3. Enter password: xxxx

4. Welcome to the MySQL monitor. Commands end with ; or \\g. 5. mysql> create database abc; (新建库名) 6. Query OK, 1 row affected (0.31 sec)

7. ###创建一个用来同步的用户,指定只能在 192.168.56.103 登录

8. ###REPLICATION SLAVE: Enable replication slaves to read binary log ev

ents from the master

9. mysql>grant replication slave on *.* to 'test1'@'10.176.190.113'

identified by 'test1';

10. Query OK, 0 rows affected (0.16 sec)

修改配置文件:

1. root@camlit ~: vim /etc/my.cnf

备注:在修改配置文件之前做好该文件的备份工作。

1. [mysqld]

2. datadir=/var/lib/mysql

3. socket=/var/lib/mysql/mysql.sock 4. user=mysql

5. old_passwords=1 (密码不变) 6. bind-address=10.176.190.111 7. ##增加下面内容

8. server_id=1###1 表示 master, 2 表示 slave binlog-do-db=abc ###需要同

步的数据库,如果有多个数据库,每个数据库一行 binlog-ignore-db=mysql###不需要同步的数据库 log-bin=mysql-bin 9.

10. [mysqld_safe]

11. log-error=/var/log/mysqld.log 12. pid-file=/var/run/mysqld/mysqld.pid

重启服务:

1. root@camlit ~: /etc/init.d/mysqld restart

2.2.2 Slave 端设置

和 master 端一样创建一个相同的数据库: abc

1. Enter password:

2. Welcome to the MySQL monitor. Commands end with ; or \\g.

3. Your MySQL connection id is 5

4. Server version: 5.0.45-log Source distribution 5.

6. Type 'help;' or '\\h' for help. Type '\\c' to clear the buffer. 7.

8. mysql> create database abc; (新建库名) 9. Query OK, 1 row affected (0.31 sec)

修改配置文件:

1. root@test2 ~: vim /etc/my.cnf 1. [mysqld]

2. datadir=/var/lib/mysql

3. socket=/var/lib/mysql/mysql.sock 4. user=mysql 5. old_passwords=1 6.

7. ###增加下面内容

8. server_id=2 log-bin=mysql-bin master-host=10.176.190.111 master

-user=test1 master-password=test1 master-port=3306 master-connect-retry=10 ###连接次数 replicate-do-db=abc ###接受的数据库名 replicate-ignore-db=mysql ###不要接受的数据库 9.

10. [mysqld_safe]

11. log-error=/var/log/mysqld.log 12. pid-file=/var/run/mysqld/mysqld.pid

重启服务:

1. root@test2~: /etc/init.d/mysqld restart

备注:

配置成功 后会在 mysql 目录(/var/lib/mysql/)下生成 master.info 文件,如果要更改 slave 设置,要先将该文件删除才会起作用。

进入 mysql,输入下面命令:

1. root@test2~: mysql -u root -p 2. Enter password:

3. Welcome to the MySQL monitor. Commands end with ; or \\g. 4. Your MySQL connection id is 4

5. Server version: 5.0.45-log Source distribution 6.

7. Type 'help;' or '\\h' for help. Type '\\c' to clear the buffer. 8.

9. mysql> slave start;

10. Query OK, 0 rows affected, 1 warning (0.00 sec) 11. ###查看同步情况

12. mysql > show slave status; 或 show master status;

2.3 结果测试

在 Master 端进行数据库 abc 的一些操作,如下所示:

1. jian.ma@camlit ~: mysql -u root -p 2. Enter password:

3. Welcome to the MySQL monitor. Commands end with ; or \\g. 4. Your MySQL connection id is 3

5. Server version: 5.0.77-log Source distribution 6.

7. Type 'help;' or '\\h' for help. Type '\\c' to clear the buffer. 8.

9. mysql> use abc; 10. Database changed

11. mysql> create table test1 (IP VARCHAR(20),USER VARCHAR(100), MAIL VA

RCHAR(100));

12. Query OK, 0 rows affected (1.20 sec)