1、选择2个ip,1个为主,1个为从:
例:
主:192.168.12.76 从:192.168.12.77
2、在192.168.12.76的my.cnf 配置master,添加如下:(红色为添加的内容)
#vi my.cnf
[mysqld]
basedir=/export/servers/mysql
datadir=/export/servers/mysql/data
log-error=/export/servers/mysql/mysql_error.log
pid-file=/export/servers/mysql/mysql.pid
user=mysql
tmpdir=/tmp
log-bin=master-bin
log-bin-index=master-bin.index
server-id=1
在192.168.12.77的my.cnf 配置slave,添加如下:(红色为添加内容)
#vi my.cnf
[mysqld]
basedir=/export/servers/mysql
datadir=/export/servers/mysql/data
log-error=/export/servers/mysql/mysql_error.log
pid-file=/export/servers/mysql/mysql.pid
user=mysql
tmpdir=/tmp
server-id=2
relay-log-index=slave-relay-bin.index
relay-log=slave-relay-bin
3、重启两台服务器的mysql:
#./mysql restart
4、在主服务器上建立账户并授权slave:
mysql>use mysql;
mysql> create user master;
mysql>grant replication slave on *.* to master identified by '123456'; (GRANT REPLICATION SLAVE ON *.* to 'master'@'%' identified by 'q123456';)
5、查询master状态:
show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000004 | 308 | | |
+------------------+----------+--------------+------------------+
6、配置从服务器slave:
mysql>use mysql;
mysql> change master to master_host='192.168.12.76',master_port=3306,master_user='master',master_password='123456',master_log_file='mysql-bin.000004',master_log_pos=308;
mysql>flush privileges;
mysql>show slave status\G
mysql>slave start;
Slave_IO_Running: Yes //此状态必须YES
Slave_SQL_Running: Yes //此状态必须YES
Read_Master_Log_Pos: 600 //#同步读取二进制日志的位置,大于等于>=Exec_Master_Log_Pos
7、在主库上创建一个数据库,在从库查看是否存在,存在为ok。