写在前面:如果此文有幸被某位朋友看见并发现有错的地方,希望批评指正。如有不明白的地方,愿可一起探讨。
MySQL复制的基本原理
MySQL复制解决的基本问题
让一台MySQL服务器的数据与其他MySQL服务器的数据保持同步。
MySQL复制的工作原理
MySQL复制的工作原理图如下所示(图来自高性能MySQL第3版)
MySQL主从复制的基本步骤:
1、启动主库上的二进制文件,并把数据更改记录到二进制日志中;
2、备库将主库上的二进制日志复制到自身的中继日志中;
3、备库读取自身的中继日志中的事件,将其重放到备库数据之上。
案例拓扑图
主从复制
双主模型
前提:在Master和Slave两台主机上安装了mariadb-5.5.36
安装mariadb-5.5.36
创建数据库相关文件所存放的目录
# mkdir -pv /mysql/data # mkdir /mysql/binlogs # mkdir /mysql/relaylogs
创建数据运行时的用户和用户组
# groupadd -r mysql # useradd -g mysql -s /sbin/nologin -M -d /mysql/data/ -r mysql
修改存放数据库相关文件目录的权限
# chown -R mysql:mysql /mysql/*
编译安装maridb-5.5.36
# tar xf mariadb-5.5.36-linux-x86_64.tar.gz -C /usr/local/ # cd /usr/local/ # ln -sv mariadb-5.5.36-linux-x86_64/ mysql # cd mysql/ # chown -R root:mysql ./* # mkdir /etc/mysql # cp support-files/my-large.cnf /etc/mysql/my.cnf # vim /etc/mysql/my.cnf datadir=/mysql/data # cp support-files/mysql.server /etc/rc.d/init.d/mysqld # chkconfig --add mysqld # chkconfig mysqld on # vim /etc/man.config MANPATH /usr/local/mysql/man # ln -sv /usr/local/mysql/include/ /usr/include/mysql # echo "/usr/local/mysql/lib/" > /etc/ld.so.conf.d/mysql.conf # ldconfig # vim /etc/profile.d/mysql.sh export PATH=/usr/local/mysql/bin:$PATH # source /etc/profile.d/mysql.sh # scripts/mysql_install_db --user=mysql --datadir=/mysql/data/ # /etc/rc.d/init.d/mysqld start
配置主从复制
配置Master服务器
配置基本过程:
1、启用二进制日志
2、定义server-id
3、创建有复制权限的账号
编辑配置文件,启动二进制日志和定义server-id
[[email protected] ~]# vim /etc/mysql/my.cnf log-bin=/mysql/binlogs/master-bin server-id = 1
创建有复制权限的账号
[[email protected] ~]# mysql MariaDB [(none)]> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO [email protected]‘10.170.%.%‘ IDENTIFIED BY ‘[email protected]‘; MariaDB [(none)]> FLUSH PRIVILEGES;
配置Slave服务器
配置基本过程:
1、关闭二进制日志,启动中继日志
2、定义server-id
3、使用有复制权限的账号连接Master服务器
4、启动I/O线程和SQL线程
编辑配置文件,启动中继日志功能和定义server-id
[[email protected] ~]# vim /etc/mysql/my.cnf #log-bin=mysql-bin relay-log= /mysql/relaylogs/relay-log server-id = 11
查看主节点的状态信息
MariaDB [(none)]> SHOW MASTER STATUS;
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000001 | 495 | | |
+-------------------+----------+--------------+------------------+
1 row in set (0.07 sec)
使用有复制权限的账号连接Master服务器
[[email protected] ~]# mysql
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST=‘10.170.2.36‘, MASTER_USER=‘repluser‘, MASTER_PASSWORD=‘[email protected]‘, MASTER_LOG_FILE=‘master-bin.000001‘, MASTER_LOG_POS=495;
启动I/O线程和SQL线程
MariaDB [(none)]> START SLAVE;
测试主从复制
在Master服务器上添加数据库
# mysql < mydb.sql
# mysql < hellodb.sql
查看Master服务器上的数据库
[[email protected] ~]# mysql
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| mydb |
| mysql |
| performance_schema |
| test |
+--------------------+
6 rows in set (0.05 sec)
在Slave服务器上查看数据库
[[email protected] ~]# mysql
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| mydb |
| mysql |
| performance_schema |
| test |
+--------------------+
6 rows in set (0.00 sec)
配置双主模型
准备工作
将所已经安装MariaDB数据库的两台主机还原到刚安装完的状态
具体过程:
1、停止MySQL服务;
2、删除/mysql/data/、/mysql/binlogs/、/mysql/relaylogs/目录下的所有数据;
3、初始化MySQL并启动MySQL服务。
配置Master1和Master2服务器
基本配置要求:
1、Master1和Master2都得创建具有复制权限的账号;
2、Master1和Master2都得启动中继日志和二进制日志;
3、为了保证具有自动增长功能的字段能正确生成ID,需要配置Master1和Master2分别使用偶数和奇数ID号;
4、Master1和Master2都要将对方配置为自己的主节点。
配置Master1服务器
编辑配置文件
[[email protected] ~]# vim /etc/mysql/my.cnf
log-bin=/mysql/data/master1-bin
relay-log=/mysql/relaylogs/relay1-bin
auto-increment-increment = 2
auto-increment-offset = 1
server-id = 1
重启MySQL服务
# /etc/init.d/mysqld restart
创建具有复制权限的账号
[[email protected] ~]# mysql
MariaDB [(none)]> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO [email protected]‘10.170.%.%‘ IDENTIFIED BY ‘[email protected]‘;
查看Master2的状态信息
MariaDB [(none)]> SHOW MASTER STATUS;
+--------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+--------------------+----------+--------------+------------------+
| master2-bin.000001 | 425 | | |
+--------------------+----------+--------------+------------------+
连接到Master2
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST=‘10.170.2.49‘, MASTER_USER=‘repluser‘, MASTER_PASSWORD=‘[email protected]‘, MASTER_LOG_FILE=‘master2-bin.000001‘, MASTER_LOG_POS=425;
启动I/O线程和SQL线程
MariaDB [(none)]> START SLAVE;
配置Master2服务器
编辑配置文件
[[email protected] ~]# vim /etc/mysql/my.cnf
log-bin=/mysql/data/master2-bin
relay-log=/mysql/relaylogs/relay2-bin
auto-increment-increment = 2
auto-increment-offset = 2
server-id = 11
重启MySQL服务
# /etc/init.d/mysqld restart
创建具有复制权限的账号
[[email protected] ~]# mysql
MariaDB [(none)]> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO [email protected]‘10.170.%.%‘ IDENTIFIED BY ‘[email protected]‘;
查看Master1的状态信息
MariaDB [(none)]> SHOW MASTER STATUS;
+--------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+--------------------+----------+--------------+------------------+
| master1-bin.000001 | 425 | | |
+--------------------+----------+--------------+------------------+
连接到Master1
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST=‘10.170.2.36‘, MASTER_USER=‘repluser‘, MASTER_PASSWORD=‘[email protected]‘, MASTER_LOG_FILE=‘master1-bin.000001‘, MASTER_LOG_POS=425;
启动I/O线程和SQL线程
MariaDB [(none)]> START SLAVE;
测试双主模型
在Master1服务器上添加hellodb.sql 数据库
mys[[email protected] ~]# mysql < hellodb.sql
在Master2服务器上,查看数据库
[[email protected] ~]# mysql
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.01 sec)
在Master2服务器上添加mydb.sql数据库
[[email protected] ~]# mysql < mydb.sql
在Master1服务器上,查看数据库
[[email protected] ~]# mysql
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| mydb |
| mysql |
| performance_schema |
| test |
+--------------------+
6 rows in set (0.05 sec)
说明:在整个配置过程中,本文将Master服务器作为Master1服务器使用,将Slave服务器作为Master2服务器使用。