Mysql的复制过程是异步复制,即从Mysql的一个实例复制到另一个实例。复制操作由三个进程完成,其中两个进程(SQL进程和I/O进程)在Slave上,另外一个进程在Master(binlogdump)上。

要实现复制,必须打开Master端的二进制日志的功能。这是因为整个复制的过程实际上就是SlaveMaster端获取该更新操作日志,将其传输到本地并写入本地文件,然后再读取本地文件内容中可执行的操作记录。


A:192.168.24.138

B:192.168.24.144

A:

vim/etc/my.cnf

server-id=1

log-bin=mysql-bin

grantreplicationslaveon*.*to'slave'@'192.168.24.144'identifiedby'123';

flushtableswithreadlock;#注意不要推出这个终端,不然锁就不生效了

resetmaster;#这里要重启下mysql才能执行,此步是将Masterpos设置为98

再开一个终端

mysqldump--all-databases>all.sql

scpall.sql192.168.24.144:/root

unlocktables;

showmasterstatus;

B:

vim/etc/my.cnf

server-id=2

log-bin=mysql-bin

grantreplicationslaveon*.*to'slave'@'192.168.24.138'identifiedby'123';

mysql<all.sql

changemastertomaster_host='192.168.24.138',master_user='slave',master_password='123',master_log_file='mysql-bin.000001',master_log_pos=98;

slavestart;

mysql>showslavestatus\G

***************************1.row***************************

Slave_IO_State:Waitingformastertosendevent

Master_Host:192.168.24.138

Master_User:slave

Master_Port:3306

Connect_Retry:60

Master_Log_File:mysql-bin.000001

Read_Master_Log_Pos:98

Relay_Log_File:mysqld-relay-bin.000002

Relay_Log_Pos:235

Relay_Master_Log_File:mysql-bin.000001

Slave_IO_Running:Yes

Slave_SQL_Running: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:98

Relay_Log_Space:235

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

1rowinset(0.00sec)

A:

changemastertomaster_host='192.168.24.144',master_user='slave',master_password='123',master_log_file='mysql-bin.000001',master_log_pos=(Bshowmasterstatus得到的pos值);

slavestart;

mysql>showslavestatus\G

***************************1.row***************************

Slave_IO_State:Waitingformastertosendevent

Master_Host:192.168.24.144

Master_User:slave

Master_Port:3306

Connect_Retry:60

Master_Log_File:mysql-bin.000001

Read_Master_Log_Pos:98

Relay_Log_File:mysqld-relay-bin.000002

Relay_Log_Pos:235

Relay_Master_Log_File:mysql-bin.000001

Slave_IO_Running:Yes

Slave_SQL_Running: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:98

Relay_Log_Space:235

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

1rowinset(0.00sec)

如果Master端口不是默认端口则通过master_port=xxx指定其端口即可