mysql master-slave_如何设置MySQL主从复制 (MySQL Master-Slave)服务器

本文将帮助你在MySQL服务器之间建立主从复制。我们在CentOS 6.3使用MySQL 5.5。

设置详细信息:

Master Server: 192.168.1.10

Slave Server: 192.168.1.20

Database: mydb

如果您使用的是不同版本的MySQL,使用这个链接来检查兼容性。

1.安装MySQL的主服务器

在具有 REPLICATION SLAVE 权限的主(master)服务器上创建一个mysql 用户来连接到 master。

mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl_user'@'192.168.1.20' IDENTIFIED BY 'secretpassword';

mysql> FLUSH PRIVILEGES;

在所有表中写入加锁声明(加锁后不能插入、更新、删除)。

mysql> use mydb;

mysql> FLUSH TABLES WITH READ LOCK;

mysql> exit;

编辑MySQL配置文件,在[mysqld]部分添加以下行。

# vim /etc/my.cnf

[mysqld]

log-bin=mysql-bin

binlog-do-db=mydb

server-id=1

innodb_flush_log_at_trx_commit=1

sync_binlog=1

重新启动主MySQL服务器更改生效。

# service mysqld restart

检查当前的二进制日志文件名(文件),并用以下命令查看偏移(位置)值。

mysql > SHOW MASTER STATUS;

+------------------+----------+--------------+------------------+

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.000002 | 107 | mydb | |

+------------------+----------+--------------+------------------+

上述输出是表示当前的二进制文件使用的MySQL-bin.000002和偏移值是107。记下这些值来从服务器上使用。

备份数据库,并将其复制到从MySQL服务器(slave)。

# mysqldump -u root -p mydb > mydb.sql

# scp mydb.sql 192.168.1.20:/opt/

完成备份从表中删除读取锁,这样用户就可以改变表数据。

mysql> UNLOCK TABLES;

2.设置MySQL从服务器

编辑salve mysql的配置文件,在[mysqld]部分添加以下值。

# vim /etc/my.cnf

[mysqld]

server-id=2

replicate-do-db=mydb

服务器标识始终是一个非零数值。这些值永远不会与其他主从服务器相似。

重启MySQL从服务器,如果你已经在开始配置的复制使用 –skip-slave-start 不直接连接到主服务器。

# /etc/init.d/mysqld restart

从恢复主服务器采取数据库备份。

# mysql -u root -p mydb

使用以下命令从服务器上设置选项值。

mysql> CHANGE MASTER TO MASTER_HOST='192.168.1.10',

-> MASTER_USER='repl_user',

-> MASTER_PASSWORD='secretpassword',

-> MASTER_LOG_FILE='mysql-bin.000002',

-> MASTER_LOG_POS=107;

最后启动 slave

mysql> SLAVE START;

检查从服务器的状态。

mysql> show slave status G

*************************** 1. row ***************************

Slave_IO_State:

Master_Host: 192.168.1.15

Master_User: repl_user

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000002

Read_Master_Log_Pos: 107

Relay_Log_File: mysqld-relay-bin.000001

Relay_Log_Pos: 4

Relay_Master_Log_File: mysql-bin.000002

Slave_IO_Running: No

Slave_SQL_Running: No

Replicate_Do_DB: mydb

Replicate_Ignore_DB:

Replicate_Do_Table:

Replicate_Ignore_Table:

Replicate_Wild_Do_Table:

Replicate_Wild_Ignore_Table:

Last_Errno: 0

Last_Error:

Skip_Counter: 0

Exec_Master_Log_Pos: 107

Relay_Log_Space: 107

Until_Condition: None

Until_Log_File:

Until_Log_Pos: 0

Master_SSL_Allowed: No

Master_SSL_CA_File:

Master_SSL_CA_Path:

Master_SSL_Cert:

Master_SSL_Cipher:

Master_SSL_Key:

Seconds_Behind_Master: NULL

Master_SSL_Verify_Server_Cert: No

Last_IO_Errno: 0

Last_IO_Error:

Last_SQL_Errno: 0

Last_SQL_Error:

Replicate_Ignore_Server_Ids:

Master_Server_Id: 1

1 row in set (0.00 sec)

mysql>

MySQL的主从复制已在系统上,并在工作模式下配置成功。感谢您使用本教程,请不要忘了与你的朋友分享。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值