mysql设置主从同步

原因(略)

说明:空了再说!

步骤

  1. 设置主数据库配置文件my.cnf(或my.ini);
# 启用二进制日志
log_bin = mysql-bin
server-id = 111
log-bin-index=mysql-bin.index
sync_binlog=1
binlog_format=mixed
binlog-do-db = testdb # 同步的数据库名称
binlog-ignore-db = mysql
binlog-ignore-db = performance_schema
binlog-ignore-db = information_schema
binlog_checksum=NONE
  1. 在主服务器设置同步帐号
mysql> grant replication slave on *.* to [email protected] identified by '123456'
mysql> flush privileges; # 刷新权限
mysql> show master status; # 查看主库状态
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000008 |      337 |              |                  |
+------------------+----------+--------------+------------------+
  1. 修改从数据库配置文件my.cnf
# [必需]启用二进制日志
log-bin=mysql-bin   
# [必需]服务器唯一ID,默认是1,一般取IP最后一段
server-id=222
relay-log-index = slave-relay-bin.index
relay-log = slave-relay-bin
sync_master_info = 1
sync_relay_log = 1
sync_relay_log_info = 1
  1. 配置连接主库
mysql> change master to master_host='192.168.1.10',master_user='slave',master_password='123456', master_log_file='mysql-bin.000008',master_log_pos=337;
  1. 开启从数据库同步
mysql> start slave; # 开启从库同步
mysql> show slave status\G; # 显示从库状态
# 若看到Slave_IO_Running: Yes,Slave_SQL_Running: Yes,表示正常

相关命令

mysql> start slave; # 开启从库同步
mysql> stop slave; # 停止从库同步
mysql> show slave satatus; # 显示从库状态
mysql> show master status; # 显示主库状态
mysql> show processlist; # 显示mysql子进程列表
mysql> set global sql_slave_skip_counter =1; # 设置跳过错误步数,数字任意
mysql> flush tables with read lock; # 锁表,防止数据写入(同步失败时,或备份时使用)
mysql> unlock tables # 解锁
mysql> source /tmp/mysql.bak.sql # 导入mysql.bak.sql文件(导入前设置操作的数据库)
mysql> change master to master_host = '192.168.128.10', master_user = 'slave', master_port=3306, 
mysql> master_password='123456', master_log_file = 'mysqld-bin.000001', master_log_pos=3260; # 修改链接到主库的信息;
mysql> change master to master_user='repl', master_password='replpwd'; # 修改链接到主库的帐号信息
mysql> show grants for 'repl'@'192.168.1.177' # 显示复制帐号信息
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.1.177' IDENTIFIED BY 'replpwd'; # 修改帐号密码
mysql> system grep repl /data/inst3506/data3506/master.info // 使用系统命令查看mysql生成的主库信息

参考文章

Mysql 5.6主从同步配置与解决方案
FLUSH TABLES WITH READ LOCK有多快
MySQL 5.6主从Slave_IO_Running:Connecting/error connecting to master *- retry
MySQL修改复制用户及密码

你可能感兴趣的:(mysql设置主从同步)