Heim  >  Artikel  >  Datenbank  >  mysql5.5.27主从复制_MySQL

mysql5.5.27主从复制_MySQL

WBOY
WBOYOriginal
2016-06-01 12:58:51931Durchsuche

主从复制原理:

1 复制架构图

\

2 初始化图

\

3 复制原理

mysql使用3个线程来执行复制功能(其中1个在主服务器上,另两个在从服务器上)。当发出start slave时,从服务器创建一个I/O线程,以连接主服务器并让它发送记录在其二进制日志中的语句。主服务器创建一个线程将二进制日志中的内容发送到从服务器。该线程可以即为主服务器上show processlist输出中的Binlog Dump线程。从服务器I/O线程读取主服务器Binlog Dump线程发送的内容并将该数据拷贝到从服务器数据目录中的本地文件中,即中继日志。第3个线程是sql线程,由从服务器创建,用于读取中继日志并执行日志中包含的更新。在从服务器上,读取和执行更新语句被分成两个独立的任务。当从服务器启动时,其I/O线程可以很快地从主服务器索取所有二进制日志内容。

1、主从服务器分别作以下操作:
1.1、版本一致。(mysql-5.5.27)
1.2、初始化表,并在后台启动mysql
1.3、修改root的密码
1.4、主服务器master:192.168.142.131
1.4、从服务器slave:192.168.142.133



2、修改主服务器master:
#vi /etc/my.cnf
[mysqld]
#
log-bin=mysql-bin #[必须]启用二进制日志
server-id=131 #[必须]服务器唯一ID,默认是1,一般取IP最后一段
#binlog-do-db =test #需要备份数据,多个写多行
#binlog-ignore-db =mysql #不需要备份的数据库,多个写多行


3、修改从服务器slave:
#vi /etc/my.cnf
[mysqld]
log-bin=mysql-bin #[不是必须]启用二进制日志
server-id=133 #[必须]服务器唯一ID,默认是1,一般取IP最后一段
#master-connect-retry=60 #如果从服务器发现主服务器断掉,重新连接的时间差(秒)
#replicate-do-db =test #只复制某个库
#replicate-ignore-db=mysql #不复制某个库


4、重启两台服务器的mysql
service mysqld restart


5、在主服务器上建立帐户并授权slave:
# mysql -uroot -p123456
mysql>GRANT REPLICATION SLAVE ON *.* to 'clevercode'@'%' identified by 'q123456'; //一般不用root帐号,“%”表示所有客户端都可能连,只要帐号,密码正确,此处可用具体客户端IP代替,如192.168.145.226,加强安全。


6、登录主服务器的mysql,查询master的状态
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000011 | 248 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
注:执行完此步骤后不要再操作主服务器MYSQL,防止主服务器状态值变化


7、配置从服务器Slave:
mysql>stop slave;

mysql>change master to master_host='192.168.142.131',master_user='clevercode',master_password='q123456',
master_log_file='mysql-bin.000011',master_log_pos=248; //注意不要断开,248数字前后无单引号。


Mysql>start slave; //启动从服务器复制功能,如果slave是启动状态,需要先stop slave;


8、检查从服务器复制功能状态:


mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.142.131 //主服务器地址
Master_User: clevercode //授权帐户名,尽量避免使用root
Master_Port: 3306 //数据库端口,部分版本没有此行
Connect_Retry: 60
Master_Log_File: mysql-bin.000012
Read_Master_Log_Pos: 107 //#同步读取二进制日志的位置,大于等于Exec_Master_Log_Pos
Relay_Log_File: centos64idx4-relay-bin.000002
Relay_Log_Pos: 253
Relay_Master_Log_File: mysql-bin.000012
Slave_IO_Running: Yes //此状态必须YES
Slave_SQL_Running: Yes //此状态必须YES
Replicate_Do_DB:
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: 107
Relay_Log_Space: 416
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: 131
1 row in set (0.00 sec)


注:Slave_IO及Slave_SQL进程必须正常运行,即YES状态,否则都是错误的状态(如:其中一个NO均属错误)。


9、主从服务器测试:


1、主服务器Mysql,建立数据库,并在这个库中建表插入一条数据:
mysql> create database db_clevercode;
Query OK, 1 row affected (1.77 sec)


mysql> use db_clevercode;
Database changed


mysql> create table tb_test(id int(3),name char(10));
Query OK, 0 rows affected (14.17 sec)


mysql> insert into tb_test values(1,'clevercode');
Query OK, 1 row affected (1.60 sec)


2、从服务器Mysql查询:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| db_clevercode |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)


mysql> use db_clevercode;
Database changed


mysql> select * from tb_test;
+------+------------+
| id | name |
+------+------------+
| 1 | clevercode |
+------+------------+
1 row in set (0.01 sec)


10、主从同步延时:

通过监控show slave status\G命令输出的Seconds_Behind_Master参数的值来判断,是否有发生主从延时。其值有这么几种:


NULL — 表示io_thread或是sql_thread有任何一个发生故障,也就是该线程的Running状态是No,而非Yes。


0 — 该值为零,是我们极为渴望看到的情况,表示主从复制良好,可以认为lag不存在。


正值 — 表示主从已经出现延时,数字越大表示从库落后主库越多。


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn