实验拓扑图模型:
############################【实验MySQL服务器的主从复制:】#######################################
1
2
3
4
5
6
7
8
9
|
###########################修改主配置文件##########################################
[root@director2 ~ ]#vim /etc/mysql/my.cnf
[mysqld]
42 thread_concurrency= 8
43 innodb_file_per_table = on
44 datadir = /mydata ###############库目录
45 skip_name_resolve = on ###############不反解主机名
59 log-bin=mysql-bin ###############启用二进制日志
67 server-id = 1 ###############唯一ID
|
1
2
|
[root@director2 ~ ]# chkconfig --list mysqld #####保证mysqld的开启自启动
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
|
############################授权一个有复制权限的用户##############################################
1
2
3
4
|
mysql> grant replication client,replication slave on *.* to 'cpuser'@'172.16.249.106' identified by "magedu";
Query OK, 0 rows affected (0.08 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
|
###########################从节点准备#############################################################
1
2
3
4
5
6
|
[root@node2 ~ ]# groupadd -g 306 mysql
[root@node2 ~ ]# useradd -g 306 -u 306 mysql
[root@node2 ~ ]# mkdir /mydata
[root@node2 ~ ]# cd /mydata/
[root@node2 / ]# chown -R mysql:mysql /mydata/
[root@node2 / ]# chmod -R a+w /mydata/
|
###################主节点:把mysql文件传输给主节点################################################
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
[root@node2 mysql ]#id mysql
uid=306(mysql) gid=306(mysql) groups=306(mysql)
[root@node2 mysql ]#ll /mydata
total 0
[root@node2 mysql ]#ll -d /mydata
drwxrwxrwx 2 mysql
mysql 4096 Jan 23 11:03 /mydata
[root@node2 mysql ]#mkdir /etc/mysql
[root@node2 mysql ]# scripts/mysql_install_db --user=mysql --datadir=/mydata ###初始化
[root@director2 ~ ]# scp /etc/mysql/my.cnf 172.16.249.106:/etc/mysql/ ###把主节点的配置文件拷贝过来
[root@node2 / ]# vim /etc/mysql/my.cnf ####编辑从节点的配置文件
59 #log-bin=mysql-bin ####从节点不启用二禁止日志
67 server-id = 20 ####server-id保持唯一
68 relay-log = relay-bin ####从节点开启中继日志
69 read-only =on ####只读模式开启(注意:这个只能限制普通用户)
|
方法二:此处也可以直接施加读锁。在mysql> lock tables testdb read;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
mysql> show variables like 'relay%'; #####中继日志开启了
+-----------------------+--------------------+
| Variable_name | Value |
+-----------------------+--------------------+
|
relay_log | relay-bin |
|
relay_log_index | |
|
relay_log_info_file | relay-log.info |
|
relay_log_purge | ON |
|
relay_log_recovery | OFF |
|
relay_log_space_limit | 0 |
+-----------------------+--------------------+
mysql> show variables like "read%";
#############只读模式开启
+----------------------+---------------------+
|
Variable_name | Value |
+----------------------+---------------------+
|
read_buffer_size | 1048576 |
|
read_only | ON |
|
read_rnd_buffer_size | 4194304 |
+----------------------+---------------------+
|
#############################至此主从各单个节点配置结束########################################
#############################配置从连接主#######################################################
#####主节点查看二进制文件的存储位置
1
2
3
4
5
6
7
8
9
10
|
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB |
Binlog_Ignore_DB | |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 | 501 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.04 sec)
######################################建立主从连接#######################################
mysql> change master to master_host='172.16.249.122',master_user='cpuser',master_password='magedu',master_log_file='mysql-bin.000003',master_log_pos=501;
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
mysql> show slave status\G; #####查看当前从节点状态
***************************1. row ***************************
Slave_IO_State:
Waiting for master to send event
Master_Host:172.16.249.122
Master_User: cpuser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File:mysql-bin.000003
Read_Master_Log_Pos:501
Relay_Log_File: relay-bin.000002
Relay_Log_Pos: 529
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes ###IO线程启用
Slave_SQL_Running: Yes ###SQL线程已经启用
|
#############################################测试结果##############################################
######主节点创建库
1
2
3
4
|
mysql> create database testdb;
Query OK, 1 row affected (0.03 sec)
mysql> use testdb;
Database changed
|
此时从节点已经同步了:
############################################创建一个表############################################
1
2
|
mysql> create table students(id tinyint unsigned not null auto_increment unique key);
Query OK, 0 rows affected (0.25 sec)
|
总结步骤:::
【主服务器】
1,主服务器的server-id,保持唯一。
2,开启二进制日志。
3,授权一个可以复制的用户,最小权限使用,指向从服务器最好。
【从服务器】
1,关闭二进制日志,开启中继日志
2,保持唯一的server-id
3,为了防止从服务器写入数据,开启只读模式。
4,连接主服务器,指定主服务器Ip,授权的用户,密码,上一次的二进制文件的位置。
5,开启IO,SQL线程。
########################################【半主从模式】########################################
##############此处的半主从是在主从的基础上的半同步:
1,条件是有一个从服务器,他和主服务器同样的写同步,如果同步超时,把这个从这个从节点降级为异步。恢复正常的状态。
【完成半同步,需要的是步骤是各自的插件】
1
2
3
|
[root@node2 plugin]# pwd
/usr/local/mysql/lib/plugin
[root@node2 plugin ]# semisync_master.so(主节点的插件) semisync_slave.so (从节点的插)
|
主节点:安装插件
此时全局参数变多了
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
mysql> install plugin rpl_semi_sync_master soname 'semisync_master.so';
Query OK, 0 rows affected (0.15 sec)
mysql> show global variables like '%semi%';
#####安装好了之后,生成很多变量
+------------------------------------+---------------------+
| Variable_name | Value |
+------------------------------------+---------------------+
| rpl_semi_sync_master_enabled | OFF | 表示虽然安装,但是没有启用
| rpl_semi_sync_master_timeout | 10000 | ###探测主的超时时长
| rpl_semi_sync_master_trace_level | 32 | ####追踪级别
| rpl_semi_sync_master_wait_no_slave | ON | ####等待从服务器的上线的时间
+------------------------------------+---------------------+
mysql> set global rpl_semi_sync_master_enabled=1; #####启用这个插件
Query OK, 0 rows
affected (0.00 sec)
mysql> set global
rpl_semi_sync_master_timeout=2000;
Query OK, 0 rows
affected (0.00 sec)
|
【从节点安装插件配置】
1
2
3
4
5
6
7
8
9
10
11
12
|
mysql> install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
Query OK, 0 rows affected (0.10 sec)
mysql> show global variables like '%semi%';
+---------------------------------+-------------------+
|Variable_name | Value |
+---------------------------------+-------------------+
| rpl_semi_sync_slave_enabled | OFF | ###插件未启用
| rpl_semi_sync_slave_trace_level | 32 |
+---------------------------------+-------------------+
mysql> set global rpl_semi_sync_slave_enabled=1;
####启用插件
Query OK, 0 rows affected (0.00 sec)
|
###############################至此两个节点的插件全部安装,启用完毕###############################
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
mysql> show
global status like '%semi%';
+--------------------------------------------+-----------------------------+
|Variable_name | Value |
+--------------------------------------------+-----------------------------+
| Rpl_semi_sync_master_clients | 0 |
| Rpl_semi_sync_master_net_avg_wait_time | 0 |
| Rpl_semi_sync_master_net_wait_time | 0 |
| Rpl_semi_sync_master_net_waits | 0 |
| Rpl_semi_sync_master_no_times | 0 |
| Rpl_semi_sync_master_no_tx | 0 |
| Rpl_semi_sync_master_status | ON |
| Rpl_semi_sync_master_timefunc_failures | 0 |
| Rpl_semi_sync_master_tx_avg_wait_time | 0 |
| Rpl_semi_sync_master_tx_wait_time | 0 |
| Rpl_semi_sync_master_tx_waits | 0 |
| Rpl_semi_sync_master_wait_pos_backtraverse | 0 |
| Rpl_semi_sync_master_wait_sessions | 0 |
| Rpl_semi_sync_master_yes_tx | 0 |
+--------------------------------------------+-----------------------------+
mysql> stop slave
IO_THREAD;
Query OK, 0 rows
affected (0.00 sec)
mysql> start
slave IO_THREAD;
Query OK, 0 rows
affected (0.00 sec)
|
再次查看已经启用了。下面直接测试使用即可######
##################################【实验二双主模式的实现】######################################
#################还是用的之前两台主机,重新初始化重新配置
1,双节点各自使用不同的server id
2, 都启用binlog 和relay log
3,自动自动增长的id字段的增长特殊定义:
3, 都授权有复制权限的用户账号
修改节点一的配置文件
节点一:配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
[root@director2 ~ ]#vim .my.cnf
59 log-bin=mysql-bin ####开启二进制日志
67 server-id = 10 ####服务器唯一ID
68 relay-log=relay-bin ####开启中继日志,因为互为主从
69 auto_increment_offset=1 ####指定日志的初始偏移量
70 auto_increment_increment=2 ####每次的偏移步长
[root@director2 bin ]# mysql
mysql> grant replication slave,replication client on *.* to 'cpuser'@'%' identified by 'magedu';Query OK, 0 rows affected (0.04 sec) ####给节点授权一个可链接用户
mysql> flush privileges;
mysql> show master status;
+------------------+----------+--------------+------------------------------------+
|File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------------------------+
|mysql-bin.000003 | 1796 | | |
+------------------+----------+--------------+------------------------------------+
mysql> change master to master_host='172.16.249.106',master_user='cpuser',master_password='magedu',master_log_file='mysql-bin.000001',master_log_pos=493;
Query OK, 0 rows affected (0.10 sec)
mysql> start slave;
|
【节点二的配置文件】
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
59 log-bin=mysql-bin ####开启二进制日志
67 server-id = 20 ####区别节点一的ID
68 relay-log = relay-bin ####开启中继日志(也有从的角色)
69 auto-increment_offset =2 ####自增其实位置
70 auto-increment_increment=2 ####自增的步长
[root@node2 mysql ]# mysql
mysql> grant replication slave,replication client on *.* to 'cpuser'@'%' identified by'magedu';
mysql> flush privileges;
mysql> change master to
mysql> show master status;
+------------------+----------+--------------+-------------------------------+
|File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+-------------------------------+
|mysql-bin.000001 | 493 | | |
+------------------+----------+--------------+-------------------------------+
master_host='172.16.249.122',master_user='cpuser',master_password='magedu',master_log_file='mysql-bin.000003',master_log_pos=1796;
Query OK, 0 rows affected (0.07 sec)
mysql> start slave;
|
##################节点一创建数据库tb1
##################节点二可以立即同步
##################节点二:在这个库里面创建表
##################并且插入数据1
##################节点一,已经同步
至此双主模式模式也实现完毕!!!!!!!!!!!!!