煮酒品茶-Mysql主从复制
我们来做这个复制,怎么做?
command:
show processlist; show slave status; show master status; SHOW MASTER LOGS SHOW BINARY LOGS SHOW SLAVE HOSTS show binlog events\G
A-A1,A2 > B-A1,A2
A:
A1,A2 -- time
A1,A2,A3 > A1,A2 update
A-A1,A2 - time
-----------end
---start
A-A1(edit),A3 - time
A-A1(edit),A3(edit) - time
A-A1(edit) > B-A1?
md5
{
table
}
1、从服务器 start slave,创建I/O线程。
2、主服务器创建I/O线程,用来传送binlog日志。(SHOW PROCESSLIST)
3、从服务器接收binlog日志,并把它保存到本地中继日志)。
4、从服务器创建 SQL线程用来执行刚保存的binlog日志。
1、实现主从数据相同的过程
1、锁表 FLUSH TABLES WITH READ LOCK;
2、备份数据并在从服务器上导入数据,使之数据相同。mysqldump....
3、记录binlog日志文件以及偏移量 show master status;
4、增加同步的权限用户,密码,权限,同步主机。)
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%.mydomain.com' IDENTIFIED BY 'slavepass';
2、修改配置文件
主:
server_id
log-bin
从:
server_id
3、在从服务器上设置同步选项
mysql> CHANGE MASTER TO
-> MASTER_HOST='master_host_name',
-> MASTER_USER='replication_user_name',
-> MASTER_PASSWORD='replication_password',
-> MASTER_LOG_FILE='recorded_log_file_name',
-> MASTER_LOG_POS=recorded_log_position4、启动从服务器同步 start slave;
5、开启主服务器上的锁表unlock table;
二进制日志文件\索引\最后一次成功更新的位置\封锁并等待主服务器通知新的更新
主服务器将更新写入二进制日志文件,并维护文件的一个索引以跟踪日志循环。这些日志可以记录发送到从服务器的更新。当一个从服务器连接主服务器时,它通知主 服务器从服务器在日志中读取的最后一次成功更新的位置。从服务器接收从那时起发生的任何更新,然后封锁并等待主服务器通知新的更新。
LVS>mysql1
mysql2
通过在主服务器和从服务器之间切分处理客户查询的负荷,可以得到更好的客户响应时间。SELECT查询可以发送到从服务器以降低主服务器的查询处理负荷。但修改数据的语句仍然应发送到主服务器,以便主服务器和从服务器保持同步。如果非更新查询为主,该负载均衡策略很有效,但一般是更新查询。
主从复制执行过程:
MySQL使用3个线程来执行复制功能(其中1个在主服务器上,另两个在从服务器上。当发出START SLAVE时,从服务器创建一个I/O线程,以连接主服务器并让它发送记录在其二进制日志中的语句。主服务器创建一个线程将二进制日志中的内容发送到从服务器。该线程可以识别为主服务器上SHOW PROCESSLIST的输出中的Binlog Dump线程。从服务器I/O线程读取主服务器Binlog Dump线程发送的内容并将该数据拷贝到从服务器数据目录中的本地文件中,即中继日志。第3个线程是SQL线程,是从服务器创建用于读取中继日志并执行日志中包含的更新。
MASTER:
mysql> SHOW PROCESSLIST\G
*************************** 1. row ***************************
Id: 2
User: root
Host: localhost:32931
db: NULL
Command: Binlog Dump
Time: 94
State: Has sent all binlog to slave; waiting for binlog to
be updated
Info: NULLSLAVE:
mysql> SHOW PROCESSLIST\G
*************************** 1. row ***************************
Id: 10
User: system user
Host:
db: NULL
Command: Connect
Time: 11
State: Waiting for master to send event
Info: NULL
*************************** 2. row ***************************
Id: 11
User: system user
Host:
db: NULL
Command: Connect
Time: 11
State: Has read all relay log; waiting for the slave I/O
thread to update it
Info: NULLmysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.100.220
Master_User: rep1
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000006
Read_Master_Log_Pos: 240
Relay_Log_File: Web2-221-relay-bin.000006
Relay_Log_Pos: 253
Relay_Master_Log_File: mysql-bin.000006
Slave_IO_Running: Yes
Slave_SQL_Running: No
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 1050
Last_Error: Error 'Table '100w' already exists' on query. Default database: 'test'. Query: 'create table 100w(
`id` int(11) not null,
`number` int(11) not null
)'
Skip_Counter: 0
Exec_Master_Log_Pos: 107
Relay_Log_Space: 691
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: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 1050
Last_SQL_Error: Error 'Table '100w' already exists' on query. Default database: 'test'. Query: 'create table 100w(
`id` int(11) not null,
`number` int(11) not null
)'
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
1 row in set (0.00 sec)Master info:
--master-host · --master-user · --master-password · --master-port · --master-connect-retry · --master-ssl · --master-ssl-ca · --master-ssl-capath · --master-ssl-cert · --master-ssl-cipher · --master-ssl-key
SHOW PROCESSLIST输出的State字段的拷贝。SHOW PROCESSLIST用于从属I/O线程。如果线程正在试图连接到主服务器,正在等待来自主服务器的时间或正在连接到主服务器等,本语句会通知您。在6.3节,“复制实施细节”中列出了可能的状态。旧版本的MySQL在连接主服务器不成功时,允许线程继续运行。对于旧版本的MySQL,观看此字段是必须的。如果它正在运行,则无问题;如果它没有运行,则您会在Last_Error字段中发现错误后面有说明)。
· Master_Host
当前的主服务器主机。
· Master_User
被用于连接主服务器的当前用户。
· Master_Port
当前的主服务器接口。
· Connect_Retry
--master-connect-retry选项的当前值
· Master_Log_File
I/O线程当前正在读取的主服务器二进制日志文件的名称。
<

