ITPub博客

首页 > 数据库 > MySQL > MySQL基于binlog主从复制配置

MySQL基于binlog主从复制配置

原创 MySQL 作者:wolfreturn 时间:2015-09-11 10:40:12 0 删除 编辑

Master-Slave 搭建环境:
Master:
Os: rhel-server-6.3-x86_64
Mysql: MySQL -5.5.35-1
虚拟机:Virtual BOX
Ip:192.168.56.102

Slave:
Os: rhel-server-6.3-x86_64
Mysql: MySQL -5.5.35-1
虚拟机:Virtual BOX
Ip:192.168.56.103

总体步骤:
1、 修改Master配置文件(my.cnf)
2、 在Master上创建一个有复制权限的用户
3、 复制Master上的数据库到Slave
4、 修改Slave配置文件(my.cnf)
5、 Slave初始化复制
6、 测试
7、 常见问题

1、 修改Master配置文件(my.cnf)

[mysqld]
server-id=1  //必须是服务器唯一id,默认是0,  
log-bin=mysql-bin //必须启用二进制日志
replicate-do-db=dba


[mysqld]

server-id=1

log-bin=/var/lib/mysql/master-bin

#log-bin-index=/var/lib/mysql/master-bin.index

replicate-do-db=abc

2、 在Master上创建一个有复制权限的用户

mysql>create user abc;
mysql>grant replication slave on *.* to abc identified by ‘abc';

3、 复制Master上的数据库到Slave

mysql>flush tables with read lock;
mysql> show master status
    -> ;
+-----------------+----------+--------------+------------------+
| File            | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-----------------+----------+--------------+------------------+
| ogg1-bin.000003 |      107 |              |                  |
+-----------------+----------+--------------+------------------+1 row in set (0.00 sec)
mysql>quit
[root@ogg /]# /usr/bin/mysqldump abc -uroot -proot --opt | mysql abc -uroot -proot -h 192.168.56.103

MySQL 5.6 不允许使用明密码文件远程

[root@guo ~]# /usr/bin/mysqldump abc -uroot -p --opt | mysql abc -uroot -p -h 192.168.56.103

489487.png


需要在slave中授权:
GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.56.103' IDENTIFIED BY 'root' WITH GRANT OPTION;

mysql>unlock tables;

4、修改Slave配置文件(my.cnf)

[mysqld]
server-id=2  //必须是服务器唯一id,默认是0,  
log-bin=mysql-bin //必须启用二进制日志
replicate-do-db=zbdba


[mysqld]

server-id=11

relay-log=/var/lib/mysql/slave-relay-bin

#relay-log-index=/var/lib/mysql/slave-relay-bin.index

#port=3316

#datadir=/var/lib/mysql/data

replicate-do-db=abc


5、 Slave初始化复制

mysql>slave stop;
mysql>CHANGE MASTER TO MASTER_HOST='192.168.56.12',
MASTER_USER='zbdba',MASTER_PASSWORD='123456',MASTER_LOG_FILE='ogg1-bin.000003',MASTER_LOG_POS=107;

CHANGE MASTER TO MASTER_HOST='192.168.56.102',

MASTER_PORT=3306,MASTER_USER='abc',

MASTER_PASSWORD='abc';

mysql> show slave status\G;
*************************** 1. row ***************************
              Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.56.12
                  Master_User: zbdba
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: ogg1-bin.000003
          Read_Master_Log_Pos: 107
              Relay_Log_File: ogg2-relay-bin.000002
                Relay_Log_Pos: 252
        Relay_Master_Log_File: ogg1-bin.000003
            Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: abc

138216.png

6、 测试

master:

mysql> show tables;
+-----------------+
| Tables_in_zbdba |
+-----------------+
| guestbook      |
| test            |
| test1          |
+-----------------+

create table test2 select * from test1;

mysql> show tables;
+-----------------+
| Tables_in_zbdba |
+-----------------+
| guestbook      |
| test            |
| test1          |
| test2          |
+-----------------+

salve:

mysql> show tables;
+-----------------+
| Tables_in_zbdba |
+-----------------+
| guestbook      |
| test            |
| test1          |
| test2          |
+-----------------+
4 rows in set (0.00 sec)

如果需要配置相互主从的关系,只需要在slave数据库上面建立对应用户,原master上面做一下初始化就行了。

7、 常见问题
/etc下面没有对应的my.cnf文件
解决方法:
如果mysql使用rpm包安装的,/etc下没有对应的my.cnf文件,这时候只需要复制一个/usr/share/mysql目录下的.cnf文件到/etc目录,并改名为my.cnf即可。

Server-id参数无法修改
解决方法:
看my.cnf中是否存在相同的server-id=1

MySQL主备出现这个错误: ERROR 1201 (HY000):
解决方法:
mysql> slave stop;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> reset slave;

Query OK, 0 rows affected,(0.00 sec)

ERROR 1130 (HY000): Host '*.*.*.*' is not allowed to connect to this MySQL server
解决方法:
如果你想允许用户myuser从ip为192.168.1.103的主机连接到mysql服务器,并使用mypassword作为密码
GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'192.168.1.103' IDENTIFIED BY 'mypassword' WITH GRANT OPTION;

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/24742969/viewspace-1797042/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: ORA-06054探究
请登录后发表评论 登录
全部评论
数据库维修工。 http://idber.github.io

注册时间:2014-07-11

  • 博文量
    58
  • 访问量
    681958