在企业网站中,后端MySQL数据库只有一台时,会有以下问题:
基于语句的复制(默认):在主服务器上执行的语句,从服务器执行同样的语句
基于行的复制:把改变的内容复制到从服务器
混合类型的复制:—旦发现基于语句无法精确复制时,就会采用基于行的复制
使用Mysql的replication机制实现主从同步时,其是由三个线程实现了,主库一个I/O线程,从库一个I/O线程和一个SQL线程
1.配置一台主MySQL服务器
2. 配置两台从MySQL服务器
3. 搭建主从备份
4. 在主服务器写入数据测试
建立时间同步环境:
[root@localhost ~]# yum -y install ntp
[root@localhost ~]# vi /etc/ntp.conf
……省略部分
server 127.127.1.0 //末尾插入这两行配置
fudge 127.127.1.0 stratum 8
[root@localhost ~]# yum -y install ntpdate
[root@localhost ~]# ntpdate 20.0.0.12 //一定要和主服务器时间同步
15 Sep 00:57:54 ntpdate[10127]: step time server 20.0.0.12 offset -0.960713 sec
[root@localhost ~]# yum -y install ntpdate
[root@localhost ~]# ntpdate 20.0.0.12 //一定要和主服务器时间同步
15 Sep 00:57:54 ntpdate[10127]: step time server 20.0.0.12 offset -0.960713 sec
[root@localhost ~]# vi /etc/my.cnf
[mysqld]
……
server-id = 11 //配置server-id
log-bin = master-bin //指定二进制日志文件,默认在安装目录下的、data/目录中
log-slave-updates = true //允许从库从主库复制数据时可以写入到binlog日志
[root@localhost ~]# systemctl restart mysqld //重启生效配置
[root@localhost data]# mysql -uroot -p 登入数据库
mysql> grant replication slave on *.* to 'myslave'@'20.0.0.%' identified by 'abc123'; //授予账户监视Replication(复制)状况的权力,授予slave服务器以该账户连接master后可以执行replicate操作的权利
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> flush privileges; //重新加载权限
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> show master status; //查看master状态表,记住 File 和 Position这两个字段的参数,到从服务器有用
+-------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000002 | 447 | | | |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
[root@localhost ~]# vi /etc/my.cnf
[mysqld]
……省略部分
server-id = 22
relay-log = relay-log-bin //从服务器二进制日志文件路径
relay-log-index = slave-relay-bin.index
[root@localhost ~]# systemctl restart mysqld //重启生效
[root@localhost data]# mysql -uroot -p //登入MySQL
mysql> change master to master_host='20.0.0.12', //master_host对应主服务器的IP地址。
-> master_user='myslave',
-> master_password='abc123',
-> master_log_file='master-bin.000001', //master_log_file对应show master status显示的File列:master-bin.000001。
-> master_port=3306, //master_port对应主服务器的端口。
-> master_log_pos=447; //master_log_pos对应show master status显示的Position列:447。
Query OK, 0 rows affected, 2 warnings (0.02 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 20.0.0.12
Master_User: myslave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000001
Read_Master_Log_Pos: 447
Relay_Log_File: relay-log-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: master-bin.000001
Slave_IO_Running: yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
[root@localhost ~]# vi /etc/my.cnf
[mysqld]
……省略部分
server-id = 33
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index
[root@localhost ~]# systemctl restart mysqld
[root@localhost data]# mysql -uroot -p
mysql> change master to master_host='20.0.0.12', //指定主服务器地址
-> master_user='myslave', //主服务器管理账号
-> master_password='abc123', //账号密码
-> master_log_file='master-bin.000001', //注意,这个文件一定要去master再次查看,保证一致
-> master_log_pos=447; //二进制文件位置,可在主服务器show master status查看
Query OK, 0 rows affected, 2 warnings (0.00 sec)
mysql> start slave; //开启从服务器
Query OK, 0 rows affected (0.01 sec)
mysql> show slave status \G; //查看从服务器状态
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 20.0.0.12
Master_User: myslave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000001
Read_Master_Log_Pos: 447
Relay_Log_File: relay-log-bin.000001
Relay_Log_Pos: 321
Relay_Master_Log_File: master-bin.000002
Slave_IO_Running: Yes // I/O进程已经运行
Slave_SQL_Running: Yes //SQL进程已经运行
Replicate_Do_DB:
[root@localhost data]# mysql -uroot -p //登入
mysql> create database test; //创建库
Query OK, 1 row affected (0.01 sec)
mysql> use test;
Database changed
mysql> create table text (a int,b int,c int); //创建表
Query OK, 0 rows affected (0.02 sec)
mysql> insert into text values (1,2,3); //写入数据
Query OK, 1 row affected (0.01 sec)
mysql> select * from text;
+------+------+------+
| a | b | c |
+------+------+------+
| 1 | 2 | 3 |
+------+------+------+
1 row in set (0.00 sec)
[root@localhost data]# mysql -uroot -p
mysql> show databases; //存在新建的库
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
5 rows in set (0.00 sec)
mysql> select * from test.text; //数据也正常,同步成功
+------+------+------+
| a | b | c |
+------+------+------+
| 1 | 2 | 3 |
+------+------+------+
1 row in set (0.00 sec)
[root@localhost data]# mysql -uroot -p
mysql> show databases; //存在新建的库
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
5 rows in set (0.00 sec)
mysql> select * from test.text; //数据也正常,同步成功
+------+------+------+
| a | b | c |
+------+------+------+
| 1 | 2 | 3 |
+------+------+------+
1 row in set (0.00 sec)