两台机器都运行数据库安装 yum –y install mysql mysql-server
[root@233 ~]# cat /etc/my.cnf 添加红色字段
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
#######################################
log-bin=mysql-bin ##开启日志
server-id=2 ##ID不能一样 这个为2 192.168.1.46为1
binlog-do-db=test ## 需要同步的数据库
binlog-ignore-db=mysql ## 不同步的数据库
########################################
old_passwords=1
# Disabling symbolic-links is recommended to prevent assorted security risks;
# to do so, uncomment this line:
# symbolic-links=0
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
grant replication slave,file on *.*to 'copy'@'192.168.1.46' identified by '123456';
grant replication slave,file on *.*to 'copy'@'192.168.1.233' identified by '123456';
mysql> show master status\G
*************************** 1. row ***************************
File: mysql-bin.000010
Position: 98
Binlog_Do_DB: test
Binlog_Ignore_DB: mysql
1 row in set (0.00 sec)
mysql> show master status\G
*************************** 1. row ***************************
File: mysql-bin.000009
Position: 98
Binlog_Do_DB: test
Binlog_Ignore_DB: mysql
1 row in set (0.00 sec)
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)
mysql> change master to master_host='192.168.1.233', master_user='copy', master_password='123456', master_log_file='mysql-bin.000009',master_log_pos=98;
Query OK, 0 rows affected (0.01 sec)
#####红色为192.168.1.46的mysql日志文件
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)
mysql> change master to master_host='192.168.1.233', master_user='copy', master_password='123456', master_log_file='mysql-bin.000010',master_log_pos=98;
Query OK, 0 rows affected (0.01 sec)
#####红色为192.168.1.233的mysql日志文件
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
当在两个服务器执行一下命令 是两个YES 成功
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.46
Master_User: copy
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000009
Read_Master_Log_Pos: 281
Relay_Log_File: mysqld-relay-bin.000002
Relay_Log_Pos: 418
Relay_Master_Log_File: mysql-bin.000009
Slave_IO_Running: Yes
Slave_SQL_Running: Yes