主从复制的用途: 1 灾难备份,防止主库数据丢失; 2 故障切换,主库挂断,可以切换到从库,不影响业务; 3 读写分离的基础,从库分担读的压力,主库只有写的压力。
windows系统下的配置: master:localhost:3306 slaves1:localhost:3307 一:配置主库(1)修改主配置文件
在MySQL的配置文件my.ini里面加入如下配置:
(2)重启mysql
net stop mysql主服务
用show master status 查看是否配置正确,查出来记录说明配置正确。 (4)分配从库复制的账号
grant replication slave on *.* to 'salves1' @'localhost' identified by '123456';
salves1是从库复制的账号 localhost是从库的ip 123456是密码
如果执行报:Error Code: 1290. The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement 在配置文件里注掉skip-grant-tables 重启mysql (5)查看账号
select user,host from mysql.user; OK,主库就配好了。 二:配置从库(1)修改从库配置文件
在从库的配置文件my.ini里加入如下配置:
(2)重启mysql从服务器
net stop mysql从服务
执行命令: change master to master_host='localhost', //主库的ip master_user = 'salves1', //复制的账号 master_password ='123456' //复制账号的密码 (4)启动从库复制线程
start slave (5)检查从库复制状态
show slave status
主要检查两个参数:Slave_IO_Running和Slave_Sql_Running。这两个值为Yes,OK从库配置好了。
三:主从复制测试
主库里添加一条数据:
然后去从库里去查询 ok,从库里能看到刚才添加的数据,主从复制OK了。
如上以一主一从为例,一主多从配置与此雷同。 |
|