主服务器:10.236.51.151
从服务器:10.236.51.152
安装mysql
yum -y install mysql-devel mysql-server
运行安全设置向导
mysql_secure_installation
主服务器
cp /usr/share/mysql/my-large.cnf /etc/my.cnf (my-large是一个注释了很多配置的文件)
修改master端的/etc/my.cnf文件
server_id = 1 (为1表示master,2表示slave)
binlog-do-db = test(test表示要同步的数据库),如果有多个数据库,每个数据库一行
binlog-ignore-db= mysql 设置不需要同步的数据库,每个数据库一行 (一般这条可以不写)(若该2条都不写,就代表同步全部)
log-bin = mysql-bin
进入mysql(不要忘记;)
创建一个数据库test
create database test;
创建同步用户
在主服务器上为从服务器建立一个连接帐户,该帐户必须授予REPLICAITON SLAVE权限。
mysql> grant replication slave on *.* to ‘replication‘@‘10.236.51.152‘ identified by ‘password‘;
mysql> flush privileges;
重启mysql
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
mysql> show master status\G
*************************** 1. row ***************************
File: mysql-bin.000002(这个在slave的配置中要用到)
Position: 329(这个在slave的配置中要用到)
Binlog_Do_DB: test
Binlog_Ignore_DB: mysql
1 row in set (0.00 sec)
mysql> unlock tables;
注:这里锁表的目的是为了生产环境中不让进新的数据,好让从服务器定位同步位置。初次同步完成后,记得解锁。
从服务器
cp /usr/share/mysql/my-large.cnf /etc/my.cnf
修改master端的/etc/my.cnf文件
[mysqld]
server-id = 2
log-bin = mysql-bin
replicate-do-db = test
replicate-ignore-db = mysql,information_schema
重启mysql
用change master语句指定同步位置
mysql> change master to master_host=‘10.236.51.151‘, master_user=‘replication‘, master_password=‘password‘, master_log_file=‘mysql-bin.000002‘, master_log_pos=0;
ERROR 1198 (HY000): This operation cannot be performed with a running slave; run STOP SLAVE first
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)
mysql> change master to master_host=‘10.236.51.151‘, master_user=‘replication‘, master_password=‘password‘, master_log_file=‘mysql-bin.000002‘, master_log_pos=329;
Query OK, 0 rows affected (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.236.51.151
Master_User: replication
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 329
Relay_Log_File: mysqld-relay-bin.000002
Relay_Log_Pos: 474
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: test
Replicate_Ignore_DB: mysql
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: 329
Relay_Log_Space: 630
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: