第一次配置mysql主从,遇到了许多问题,现在把正确的配置记录下来,请多指教。(回头买几本书恶补一下)
两台服务器分别为:192.168.1.11(Slave),192.168.1.13(Master)
虚拟机软件情况为:CentOS 6.4 ,mysql-5.5
(1)首先确认两台机器上的mysql版本,版本最好一致,mysql升级后,bin-log会有更改。
即使两台机器上的mysql版本不一致也需要Slave的版本高于Master,尽量做到一致吧!
(2)在Master上首先建立同步所用的帐号:
- mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave'@'192.168.157.11' IDENTIFIED BY '111111';
- Query OK, 0 rows affected (0.13 sec)
- mysql> flush privileges
(3)修改主数据库的配置文件my.cnf,开启BINLOG,并设置server-id的值,修改之后必须重启Mysql服务。
- [mysqld]
- log-bin=mysql-bin
- server-id=1
- binlog-do-db = test
- binlog-ignore-db=mysql
(4)之后可以得到主服务器当前二进制日志名和偏移量,这个操作的目的是为了在从数据库启动后,从这个点开始进行数据的恢复
- mysql> show master status\G;
- *************************** 1. row ***************************
- File: mysql-bin.000008
- Position: 338
- Binlog_Do_DB: test
- Binlog_Ignore_DB: mysql
- 1 row in set (0.00 sec)
(5)停止对主库的更新操作,并将主库的数据库文件导出一份,并且导入到从库中
- #首先添加一个读锁保证数据库的一致性
- mysql> flush tables with read lock;
- mysql> quit;
- mysqldump -uroot -p -P 3306 test > /usr/local/mysql/test.sql
- #然后将test.sql传到Slave机器上,并且导入。这里要求从库必须是启动的。
- mysql> -uroot -p --default-character-set=utf8 test < test.sql
- #最后恢复Master机器的读锁
- mysql> unlock tables
(6)修改从库的/etc/my.cnf,要求与主库的id不同,不然后面执行slave start会报错。
(7)配置从服务器
- mysql>change master to master_host='192.168.157.13',master_user='slave',master_password='111111',master_log_file='mysql-bin.000008',master_log_pos=338; //注意不要断开,“338”无单引号
(8)执行show salve status验证主从配置是否生效
- mysql> show slave status\G;
- *************************** 1. row ***************************
- Slave_IO_State: Waiting for master to send event
- Master_Host: 192.168.157.13
- Master_User: slave
- Master_Port: 3306
- Connect_Retry: 60
- Master_Log_File: mysql-bin.000008
- Read_Master_Log_Pos: 834
- Relay_Log_File: lvstest-relay-bin.000004
- Relay_Log_Pos: 980
- Relay_Master_Log_File: mysql-bin.000008
- 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: 834
- Relay_Log_Space: 1284
- 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: 1
- 1 row in set (0.00 sec)
主要看这两项:
- Slave_IO_Running: Yes
- Slave_SQL_Running: Yes
说明主从配置成功,然后在主库中建表插数据,测试成功!
|