【三】从库的操作
与主库一样,需要先复制一个配置文件到/etc下并进行编辑,复制操作可参考主库的,这里跳过
【1】修改my.cnf
#vi /etc/my.cnf
server-id = 2 将这个ID号改为2
log-bin=MySQL-bin 此项如果开启建议关闭加上#就可以了
master-host = 192.168.1.41 配置主库的IP
master-user = testuser 同步用的账户
master-password = 123456 同步用的账户密码
master-port = 3306 同步数据库的端口号
【2】调整权限
主库复制过来的数据库全部都是root权限,需要修改为mysql权限
#chown -R mysql:mysql /var/lib/mysql
【3】重启mysql服务
权限调整完成后重启服务,之前重启会出现启动报错
#service mysql restart
个别版本可能出现即使权限调整后也无法启动,可以使用下面的方式启动
#mysqld_safe --skipp-slave-start&
【4】手动同步
与主库进行同步,可将下面内容调整好后直接复制到mysql命令行下,这样不容易出错
设置连接MASTER MASTER_LOG_FILE为主库的File,MASTER_LOG_POS为主库的Position,就是在锁主库后要记录的那2个值
mysql> CHANGE MASTER TO
MASTER_HOST='192.168.1.41',
MASTER_USER='testuser',
MASTER_PASSWORD='123456',
MASTER_PORT=3306,
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=98,
MASTER_CONNECT_RETRY=60;
【5】启动Slave服务
mysql>slave start;
所有配置工作完成
【6】检查从库是否正常同步
mysql>show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.2
Master_User: testuser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000085
Read_Master_Log_Pos: 20705416
Relay_Log_File: TMac-relay-bin.000072
Relay_Log_Pos: 12753
Relay_Master_Log_File: mysql-bin.000085
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: 20705416
Relay_Log_Space: 1146541
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:
1 row in set (0.00 sec)
ERROR:
No query specified
当Slave_IO_Running 和 Slave_SQL_Running 两列的值都为 "Yes",表明 Slave 的 I/O 和 SQL 线程都在正常运行,如果出现错误,可以从Last_Error这个参数中看出哪里出错,然后进行排查。
