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

MYSQL的主从和主主复制模式

2013-11-14 10:40 260 查看
一、复制介绍

MySQL支持单向、异步复制,复制过程中一个服务器充当主服务器,而一个或多个其它服务器充当从服务器。主服务器将更新写入二进制日志文件,并维护文件的一个索引以跟踪日志循环。这些日志可以记录发送到从服务器的更新。当一个从服务器连接主服务器时,它通知主服务器从服务器的日志中读取的最后一次成功更新的位置。从服务器接收从那时起发生的任何更新,然后封锁并等待主服务器通知新的更新。

请注意当你进行复制时,所有对复制中的表的更新必须在主服务器上进行。否则,你必须要小心,以避免用户对主服务器上的表进行的更新与对从服务器上的表所进行的更新之间的冲突。

单向复制有利于健壮性、速度和系统管理:

•主服务器/从服务器设置增加了健壮性。主服务器出现问题时,你可以切换到从服务器作备份。

•通过在主服务器和从服务器之间切分处理客户查询的负荷,可以得到更好的客户响应时间。SELECT查询可以发送到从服务器以降低主服务器的查询处理负荷。但修改数据的语句仍然应发送到主服务器,以便主服务器和从服务器保持同步。如果非更新查询为主,该负载均衡策略很有效,但一般是更新查询。

•使用复制的另一个好处是可以使用一个从服务器执行备份,而不会干扰主服务器。在备份过程中主服务器可以继续处理更新。

MySQL提供了数据库的同步功能,这对我们事先数据库的容灾、备份、恢复、负载均衡等都是有极大帮助的。

二、实验环境

操作系统:RHEL 5.4 X86

mysql:5.5.22版本

master机器名:node2 IP:192.168.1.152

slave机器名:node1 IP:192.168.1.151

三、MySQL主从模式

注意Mysql数据库的版本,两个数据库版本要相同,或者slave比master版本低!

3.1、通过源码安装mysql

此处过程略,可以参考<Mysql 源码安装>!

3.2、Master端:

3.2.1、创建目录

[root@node2 ~]# mkdir -p /var/log/mysql
[root@node2 ~]# chown -R mysql:mysql /var/log/mysql   //创建更新目录并赋予mysql用户权限


3.2.2、修改配置文件:

[root@node2 mysql]# vi my.cnf               //编辑配置文件增加以下内容
log-bin=mysql-bin                                  //启动二进制日志系统
binlog-do-db=node1
binlog-do-db=node2                                //二进制需要同步的数据库名 如果有多个数据库,每个数据库一行
server-id = 1                                    //本机数据库ID
log-bin=/var/log/mysql/updatelog          //设定生成log文件名,这里的路径没有mysql目录要手动创建并给于它mysql用户的权限
binlog-ignore-db=mysql                   //避免同步mysql用户配置,以免不必要的麻烦


3.2.3、创建用于同步的用户:

mysql> GRANT REPLICATION SLAVE,REPLICATION CLIENT,RELOAD,SUPER ON *.* TO 'replication'@'192.168.1.151' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)      //给从服务器用户replication 的同步权限

mysql> Grant ALL PRIVILEGES ON node1.* TO replication@'%' IDENTIFIED BY '123456';           //创建用户replication,密码123456,允许所有用户访问数据库node1,并刷新权限
Query OK, 0 rows affected (0.00 sec)

mysql> Grant ALL PRIVILEGES ON node2.* TO replication@'%' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)


3.2.4、复制数据到从库:

mysql> create database node1;
Query OK, 1 row affected (0.00 sec)

mysql> create database node2;
Query OK, 1 row affected (0.00 sec)

mysql> use node1;
Database changed
mysql> create table node1 (id char) engine=myisam;
Query OK, 0 rows affected (0.01 sec)

mysql> insert into node1 values(1);
Query OK, 1 row affected (0.00 sec)

mysql> commit;
Query OK, 0 rows affected (0.00 sec)

mysql> exit;
Bye
[root@node2 app]# service mysql stop;
Shutting down MySQL...                                     [  OK  ]

[root@node2 data]# tar -cvf db.tar node1 node2
node1/
node1/node1.MYI
node1/db.opt
node1/node1.frm
node1/node1.MYD
node2/
node2/db.opt
[root@node2 data]# ll
total 30140
-rw-r--r-- 1 root  root     20480 Nov 13 19:29 db.tar

[root@node2 data]# scp db.tar node1:/app/mysql/data/
db.tar                                                                 100%   20KB  20.0KB/s   00:00

[root@node2 data]# service mysql start
Starting MySQL..                                           [  OK  ]

[root@node2 data]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.22-log Source distribution

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| updatelog.000001 |      107 | node1,node2  | mysql            |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)


3.3、Slave端:

3.3.1、修改配置文件:

[root@node1 data]# vi ../my.cnf
server-id       = 2                           //从服务器ID号
#master_host=192.168.1.152        //主服务器地址
#master_user=replication              //指定在主服务器上可以进行同步的用户名
#master_password=123456           //密码
#master-port=3306                        //端口号
#master-connect-retry=60               //断点重连时间
replicate-ignore-db=mysql              //屏蔽对mysql库的同步,以免有麻烦
replicate-do-db=node1                         //同步数据库名称
replicate-do-db=node2
----由于5.3以后不支持master_host参数等,因此下面采用change master to的方式


3.3.2、装载主服务器数据库:

[root@node1 data]# tar -xvf db.tar
node1/
node1/node1.MYI
node1/db.opt
node1/node1.frm
node1/node1.MYD
node2/
node2/db.opt
[root@node1 data]# chown -R mysql:mysql node1
[root@node1 data]# chown -R mysql:mysql node2
[root@node1 data]# service mysql restart
Starting MySQL                                             [  OK  ]


3.3.3、同步数据:

mysql> slave stop
-> ;
Query OK, 0 rows affected (0.01 sec)

mysql> change master to master_host='192.168.1.152', master_user='replication', master_password='123456';
Query OK, 0 rows affected (0.01 sec)

mysql> slave start;
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.1.152
Master_User: replication
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: updatelog.000001
Read_Master_Log_Pos: 107
Relay_Log_File: node1-relay-bin.000002
Relay_Log_Pos: 253
Relay_Master_Log_File: updatelog.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: node1,node2
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: 409
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)

ERROR:
No query specified

mysql> use node1;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+-----------------+
| Tables_in_node1 |
+-----------------+
| node1           |
+-----------------+
1 row in set (0.00 sec)

mysql> select * from node1;
+------+
| id   |
+------+
| 1    |
+------+
1 row in set (0.00 sec)


3.4、测试数据

----主库端操作
mysql> use node1;
Database changed
mysql> insert into node1 values(2);
Query OK, 1 row affected (0.01 sec)

mysql> commit;
Query OK, 0 rows affected (0.00 sec)

mysql> use node2;
Database changed
mysql> create table node2 (id char);
Query OK, 0 rows affected (0.01 sec)

----备库端查询
mysql> select * from node1;
+------+
| id   |
+------+
| 1    |
| 2    |
+------+
2 rows in set (0.00 sec)

mysql> use node2;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+-----------------+
| Tables_in_node2 |
+-----------------+
| node2           |
+-----------------+
1 row in set (0.00 sec)


四、MySQL主主模式:

思路,互为对方的从服务器,每台服务器即是对方的主服务器,又是对方的从服务器。

在这里就省略了!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: