Skip to content

Instantly share code, notes, and snippets.

@wxianfeng
Created June 7, 2012 08:13
Show Gist options
  • Save wxianfeng/2887351 to your computer and use it in GitHub Desktop.
Save wxianfeng/2887351 to your computer and use it in GitHub Desktop.
mysql 主从复制
# mysql主从复制,两台机器
# 主: 192.168.10.107 UBUNTU
# 从: 192.168.10.105 UBUNTU
# 主配置
>vim /etc/mysql/my.cnf
[mysqld]
log-bin=mysql-bin // 启用二进制日志
server-id=107 // 服务器唯一ID,一般取IP最后一段
bind-address=192.168.10.107
## 添加用户用于slave
>grant replication slave,reload,super on *.* to 'slave'@'192.168.10.105' identified by 'netposa';
>flush privileges;
# 从配置
>vim /etc/mysql/my.cnf
[mysqld]
log-bin=mysql-bin
server-id=105
master-host=192.168.10.107
master-user=slave
master-password=netposa
# 或者去掉上面 master-host, master-user, master-password配置, mysql 5.5 已经不支持这种指令,用下面方法动态添加
主:
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000018 | 107 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
从:
>change master to master_host='192.168.10.107', master_user='slave', master_password='netposa', master_log_file='mysql-bin.000018', master_log_pos=107;
## 主,从重启mysql
>sudo /etc/init.d/mysql restart
# 从开启 slave
>start slave;
# 从查看slave状态:
>show slave status; 列显示
>show slave status\G; 行显示
---> ok 已经可以同步了!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment