Home >Database >Mysql Tutorial >MYSQL主备复制结构搭建_MySQL

MYSQL主备复制结构搭建_MySQL

WBOY
WBOYOriginal
2016-05-30 17:11:18985browse

1 选择两个服务器,分别作为主备数据库

2 登陆到服务器,

yum install mysql ;

yum install mysql-server;

3 启动服务器

service mysqld start

4 分别root登陆mysql 执行如下命令,增加复制用户并授权(主备都在192.168.119.*网段,为了方便主备切换,两边都建立)

GRANT REPLICATION SLAVE,REPLICATION CLIENT on *.* to repl@'192.168.119.%' identified by '1234';

5 配置/etc/my.cnf

第一服务器(主)

log_bin=mysql_bin
server_id=1

第二个服务器(备)

log_bin=mysql_bin
server_id=2
read_only=1

6 登陆主(root)执行

show master status\G;

显示

File: mysql-bin.000001
Position: 106

 

登陆备(root) 执行

测试 mysql -urepl -h192.168.119.128 -p1234 是否能连接到主库,不能需要检查防火墙或者/etc/my.cnf是否有访问限制,修改配置

mysql -uroot登陆本地库执行:

CHANGE MASTER TO
MASTER_HOST='192.168.119.128',
MASTER_USER='repl',
MASTER_PASSWORD='1234',
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=106;

start slave;

show slave status\G;

7,主库上创建表,备库上查看,可以看到

备库上repl创建表,显示The MySQL server is running with the --read-only option so it cannot execute this statement

因为配置了备库为read_only

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn