MySQL主从同步设置和同步错误处理(三)

2014-11-24 17:02:51 · 作者: · 浏览: 2
sqld_safe --user=mysql &
不同服务器的配置可能不一样,一般会在/etc/rc.d/rc.local中有写入.
如果前面已经复制了,用--skip-slave-start选项启动从服务器,以便它不立即尝试连接主服务器。你也可能想要用--logs-warnings选项启动从服务器(默认设置启用),以便在错误日志中显示更多的问题相关的信息(例如,网络或连接问题)。放弃的连接将不会记入错误日志,除非这个option的值大于1。
8.如果使用mysqldump备份主服务器的数据,将转储文件装载到从服务器:
shell> mysql -u root -p < dump_file.sql
我没有采用mysqldump这种方式备份,所以这步跳过.
9.在从服务器上执行下面的语句:
mysql> CHANGE MASTER TO
-> MASTER_HOST='192.168.0.111',
//写主服务器的名称或者IP
-> MASTER_USER='root',
//写用来复制的帐号,前面说明过我直接用的root
-> MASTER_PASSWORD='xxx',
//复制帐号的密码,这里就是指root的密码
-> MASTER_LOG_FILE='mysql-bin.000045',
//之前记录的日志名
-> MASTER_LOG_POS=947;
//之前记录的偏移量
返回:
Query OK, 0 rows affected (0.01 sec)
下面的表显示了字符串选项的最大长度:
Master_Host
60
Master_USER
16
Master_PASSWORD
32
Master_Log_File
255
10.启动从服务器线程:
mysql> START SLAVE;
执行这些程序后,从服务器应连接主服务器,并补充自从快照以来发生的任何更新。
测试:
在从服务器上查看
mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.111
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000045
Read_Master_Log_Pos: 1064
Relay_Log_File: dbpi-relay-bin.000002
Relay_Log_Pos: 352
Relay_Master_Log_File: mysql-bin.000045
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: 1064
Relay_Log_Space: 352
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
红色标示显示出从服务器的两个线程已经启动.
在从服务器上可以查看线程运行状态
mysql> SHOW PROCESSLIST\G
*************************** 1. row ***************************
Id: 3
User: root
Host: localhost
db: test
Command: Query
Time: 0
State: NULL
Info: SHOW PROCESSLIST
*************************** 2. row ***************************
Id: 18
User: system user
Host:
db: NULL
Command: Connect
Time: 43
State: Waiting for master to send event
Info: NULL
*************************** 3. row ***************************
Id: 19
User: system user
Host:
db: NULL
Command: Connect
Time: 4294966771
State: Has read all relay log; waiting for the slave I/O thread to update it
Info: NULL
3 rows in set (0.00 sec)
在主服务器上做一个update的语句,从服务器立刻就会同步更新.配置完成.
说明:由于从服务器是通过读主服务器的二进制日志来实现自我更新的,所以对于对数据库进行修改的操作都要放在主服务器上执行,而从服务器只用来进行查询.(也就是只读不写的数据库操作).