1、主机环境
master: ip_master
slave: ip_slave
2. 创建数据库
分别登录master机和slave机的mysql:mysql –u root –p
创建数据库:create database dbname;
3.配置master,在my.cnf中添加以下字段
# MySQL同步配置
server-id=1
log-bin = mysql-bin-m
max_allowed_packet = 32M
binlog-do-db= dbname //需要同步的数据库,没有本行表示同步所有的数据库
binlog-ignore-db=mysql
其次,在master机上为slave机添加一同步帐号 ,运行
mysql> grant replication slave on . to ' dbname'@' ip_slave ' identified by '123456';
重启master机的mysql服务:
./mysqladmin -u root -p shutdown
./mysqld_safe &
用show master status 命令看日志情况
mysql>show master status;
4. slave机配置,,在my.cnf中设置
# MySQL 同步配置
server-id=2
master-host= ip_master
master-user= username
master-password=123456
master-port=8888
master-connect-retry=60
replicate-do-db= dbname
log-bin = mysql-bin-m
max_allowed_packet = 32M
replicate-ignore-db=mysql
重启slave机的mysql服务:
./mysqladmin -u root -p shutdown
./mysqld_safe &
执行命令:
mysql>start slave;
mysql>show slave status\G;
如果Slave_IO_Running、Slave_SQL_Running状态为Yes则表明设置成功