Mysql主从同步
主服务器: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
修改master端的/etc/my.cnf文件
vi /etc/my.cnf
server_id = 1 #(为1表示master,2表示slave)
binlog-do-db = test #(test表示要同步的数据库),如同步全部数据库可注释本行
binlog-ignore-db= mysql #设置不需要同步的数据库,多个数据库可用“,”分隔开(一般这条可以不写)
log-slave-updates
slave-skip-errors
log-bin = mysql-bin
进入mysql
mysql -uroot -p
创建一个数据库test
mysql>create database test;
创建同步用户
在主服务器上为从服务器建立一个连接帐户,该帐户必须授予REPLICAITON SLAVE权限。
mysql> grant replication slave on *.* to ‘replication‘@‘10.236.51.152‘ identified by ‘password‘;
mysql> flush privileges;
重启mysql
/etc/init.d/mysqld restart mysql -uroot -p
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
Position: 329
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
修改slave端的/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=0;
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:
1 row in set (0.00 sec)
mysql>
本文出自 “wemux” 博客,请务必保留此出处http://wemux.blog.51cto.com/2848943/1543959
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。