mysql配置master/slave模式

本文介绍了如何在两台虚拟机上配置MySQL的主从复制(master/slave)模式。首先在master节点创建备份账户并授予REPLICATION权限,然后修改配置文件设置server_id和log_bin。接着在slave节点上同样修改配置,包括server_id、log_bin、relay_log和read_only。通过在master上建表并添加数据,验证slave节点能成功同步。注意,实际生产环境中需要更多设置,read_only参数对于root账户可能无效。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上文讲了安装mysql步骤,此时我们在两台虚拟机上安装同样的mysql

192.168.153.129  master

192.168.153.130  slave

好了,现在开始配置master/slave


一、在master上增加slave的账号

创建backup账户,再设置REPLICATION权限

GRANT USAGE ON *.* TO 'backup'@'192.168.%' IDENTIFIED BY 'backup' WITH GRANT OPTION;
GRANT REPLICATION SLAVE,RELOAD,SUPER ON *.* TO backup@'192.168.%' IDENTIFIED BY 'backup';

二、配置master

修改/usr/local/mysql/my.cnf

server_id=1

log_bin=mysql_bin

此时可以登陆到mysql命令行,查看目前的状态

show master status;
会出现File=mysql_bin.000001, Position=120的输出


三、配置slave

同样,也是修改/usr/local/mysql/my.cnf

server_id=2

log_bin=mysql_bin

relay_log=mysql_relay_bin

log_slave_updates=1

read_only=1


四、开始replication

change master to master_host='192.168.153.129', master_user='backup', master_password='backup', master_log_file='mysql_bin.000001', master_log_pos=120;
start slave;

五、测试验证

在192.168.153.129在test库中建表A,随意增加一条数据

查看192.168.153.130的test库中,出现了表A,且数据完全一致

至此,master/slave模式的mysql配置成功


以上为简单的描述,实际的production环境需要设置的会更多,这里不一一赘述。



P.S.关于slave中的read_only参数,这里由于我测试用的是root账户,所以依然可写

贴一篇别人博客里的介绍 来自http://blog.youkuaiyun.com/cindy9902/article/details/5886355

 --read_only         Make all non-temporary tables read-only, with the 
                               exception for replication (slave) threads and users with 
                               the SUPER privilege

SUPER privilege :

The SUPER privilege enables an account to use CHANGE MASTER TO KILL or mysqladmin kill to kill threads belonging to other accounts (you can always kill your own threads), PURGE BINARY LOGS , configuration changes using SET GLOBAL to modify global system variables, the mysqladmin debug command, enabling or disabling logging, performing updates even if the read_only system variable is enabled, starting and stopping replication on slave servers, specification of any account in the DEFINER attribute of stored programs and views, and enables you to connect (once) even if the connection limit controlled by the max_connections system variable is reached.

To create or alter stored routines if binary logging is enabled, you may also need the SUPER privilege, as described in Section 18.6, “Binary Logging of Stored Programs” .

read-only选项:对所有的非临时表进行只读控制。但是有两种情况例外:

                             1. 对replication threads例外,以保证slave能够正常的进行replication。

                             2. 对于拥有super权限的用户,可以ignore这个选项。

SUPER 权限 : 1. 可以有change master to, kill其他用户的线程的权限。

                             2. Purge binary logs 来删除binary log, set global来动态设置变量的权限。

                             3. 执行mysqladmin debug命令,开启或者关闭log,在read-only打开时执行update/insert操作。

                             4. 执行start slave, stop slave.

                             5. 当连接数已经达到max_connections的最大值时,也可以连接到server。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值