1.使用root用户进入数据库

mysql -u root -p

2.创建slave的用户,密码为 :密码

GRANT REPLICATION SLAVE ON *.* to 'slave'@'%' identified by '密码';

3.查看账户

SHOW GRANTS FOR 'slave'@'%';

4.找到my.cnf文件编辑[mysqld]下面增加下面几行代码

server-id=1
log-bin=master-bin
log-bin-index=master-bin.index

5.查看日志

show master status\G;
SHOW MASTER STATUS;

File: mysql-bin.000002

    Position: 107

6.重启主mysql服务

service mysql restart

///**///

7.配置Slave从服务器

[mysqld]
server-id=2
relay-log-index=slave-relay-bin.index
relay-log=slave-relay-bin 

8.重启MySQL服务

service mysql restart

9.进入数据库

mysql -u root -p

10.连接Master服务器

CHANGE MASTER TO MASTER_HOST='服务器IP',
MASTER_PORT=端口号(默认3306),
MASTER_USER='用户',
MASTER_PASSWORD='密码',
MASTER_LOG_FILE='mysql-bin.000002',
MASTER_LOG_POS=107;

11.启动Slave

start slave;

12.查看同步状态

show slave status\G

显示以下则为成功
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

注意出现
问题1:

ERROR 1201 (HY000): Could not initialize master info structure; more error messages can be found in 

解决1:

reset slave;

问题2:

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

解决2:
查看账户是否有相应权限,或者是密码是否正确

问题3:

Slave_IO_Running: continue

解决:
A:查看账户是否有权限(远程,访问)
B 查看修改的my.cnf中 是否有重复的server-id=

参考:mysql搭建主从复制