修改my.cnf:
log-bin=mysql-bin #开启二进制文件
server-id=2 #唯一标识
binlog-ignore-db=information_schema #忽略数据库计入binlog
binlog-ignore-db=cluster
binlog-ignore-db=mysql
binlog-do-db=ufind_db #开启数据库计入binlog
在Master数据库命令行中输入:
#为从库分配用户,包含文件权限 (5.6以下)
>GRANT FILE ON *.* TO '为从库分配的用户名'@'从库地址' IDENTIFIED BY '为从库分配的密码';
#为从库分配用户,包含复制二进制文件权限 (5.6及以上)
>GRANT REPLICATION SLAVE ON *.* TO '为从库分配的用户名'@'从库地址' IDENTIFIED BY '为从库分配的密码';
#更新权限
>FLUSH PRIVILEGES
键入命令
mysql> show master status;
显示以下信息则配置成功
+------------------+----------+--------------+----------------------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+----------------------------------+-------------------+
| mysql-bin.000004 | 28125 | ufind_db | information_schema,cluster,mysql | |
+------------------+----------+--------------+----------------------------------+-------------------+
1 row in set (0.00 sec)
如果执行这个步骤始终为 ** Empty set(0.00 sec) ** ,那说明前面的my.cnf没配置对。
修改my.cnf:
log-bin=mysql-bin
server-id=3 #不可与主库重复
binlog-ignore-db=information_schema
binlog-ignore-db=cluster
binlog-ignore-db=mysql
replicate-do-db=ufind_db #同步数据库
replicate-ignore-db=mysql #忽略同步的数据库
log-slave-updates=1 #io线程同步数据时也计入logbin中
slave-skip-errors=all #跳过错误
slave-net-timeout=60
开启线程连接主库:
mysql> stop slave; #关闭Slave
mysql> change master to master_host='主库地址',master_port=主库的端口号,master_user='主库分配的用户',master_password='主库分配的密码',master_log_file='主库的File', master_log_pos=主库的Position;
mysql> start slave; #开启Slave
mysql> show slave status\G;
在输出的信息中如果出现以下两条则说明主从配置成功
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
master端:
show master status; #查看状态:
show processlist; #查看slave下mysql进程信息
reset master; #慎用,将清空日志及同步position
slave端:
show slave status;
show slave logs;
show processlist;
reset slave; #慎用,将清空slave配置信息、日志及同步position在从服务器上跳过错误事件
reset slave #清除日志同步位置标志,并重新生成master.info
show slave hosts #主机运行,看连入的从机的情况。
FLUSH TABLES WITH READ LOCK;
mysqldump -uroot -p -hlocalhost > mysql.bak.sql
unlock tables;
show master status; 找出File和Position
scp mysql.bak.sql [email protected]:/tmp/
mysql> stop slave;
mysql> source /tmp/mysql.bak.sql
change master to master_host = ‘主库主机’, master_user = ‘同步用户名’, master_port=3306, master_password=’’, master_log_file = ‘第3步中获取的file’, master_log_pos=第3步中获取的position;
mysql> start slave;
mysql> show slave status\G 查看: