lamp架构中的mysql

1.mysql的主从复制
##需要两台以上的主机具有mysql数据库
一台主机的数据库为主, 一台为从属的数据库
开启主从复制功能

编写主配置文件/etc/my.cnf
server-id=3
/etc/init.d/mysqld restart ##重启mysql
log-bin=mysql-bin ##启动二进制日志,并为二进制日志命名

/etc/init.d/mysqld restart

登陆数据库进行命令
grant REPLICATION SLAVE ON . TO ‘repl’@’%’ IDENTIFIED BY ‘westos’;
show master status ;

数据同步:
锁表

客户端的操作:
编写数据库的主配置文件
/etc/my.cnf
server-id=2
log-bin=mysql-bin

CHANGE MASTER TO MASTER_HOST=‘172.25.40.3’, MASTER_USER=‘repl’,MASTER_PASSWORD=‘westos’,MASTER_LOG_FILE=‘mysql-bin.000001’,MASTER_LOG_POS=633;

通过这条命令查看二进制日志
mysqlbinlog mysql-bin.000001

gdit的主从日志的同步设置
主服务器的配置:
编辑主配置文件
/etc/my.cnf
gtid_mode=ON
enforce-gtid-consistency=ON
重新平滑加载mysql
/etc/init.d/mysql restart

从属服务器的配置设置
编辑主配置文件
/etc/my.cnf
gtid_mode=ON
enforce-gtid-consistency=ON
重新平滑加载mysql
/etc/init.d/mysql restart

进入数据库
stop slave;
change master to master_host=‘172.25.40.3’,master_user=‘repl’, master_password=‘westos’,MASTER_AUTO_POSITION =1;
start slave;

服务的配置完成

官网中的地址:https://dev.mysql.com/doc/refman/5.7/en/replication-semisync.html

MySQL 半同步复制
主服务器中的配置:
进入数据库中
INSTALL PLUGIN rpl_semi_sync_master SONAME ‘semisync_master.so’;
SET GLOBAL rpl_semi_sync_master_enabled = 1;
编写mysql数据库的主配置文件
enforce-gtid-consistency=ON
rpl_semi_sync_master_enabled=1

版同步数据库的配置过程
进入数据库
INSTALL PLUGIN rpl_semi_sync_slave SONAME ‘semisync_slave.so’;
SET GLOBAL rpl_semi_sync_slave_enabled = 1;
STOP SLAVE IO_THREAD;
START SLAVE IO_THREAD;
编写mysql的主配置文件:
/etc/my.cnf
rpl_semi_sync_slave_enabled=1
添加内容

完成之后需要重新加载mysql

mysql> show variables like ‘rpl%’;
±------------------------------------------±-----------+
| Variable_name | Value |
±------------------------------------------±-----------+
| rpl_semi_sync_master_enabled | ON |
| rpl_semi_sync_master_timeout | 10000 |
| rpl_semi_sync_master_trace_level | 32 |
| rpl_semi_sync_master_wait_for_slave_count | 1 |
| rpl_semi_sync_master_wait_no_slave | ON |
| rpl_semi_sync_master_wait_point | AFTER_SYNC |
| rpl_stop_slave_timeout | 31536000 |
±------------------------------------------±-----------+
7 rows in set (0.00 sec)

服务端的查看服务:

mysql> show status like ‘rpl%’;
±---------------------------±------+
| Variable_name | Value |
±---------------------------±------+
| Rpl_semi_sync_slave_status | ON |
±---------------------------±------+
1 row in set (0.00 sec)

延迟处理:
参看官网的Delayed Replication

CHANGE MASTER TO MASTER_DELAY = 1;

编写mysql的主配置文件
添加内容:

MYSQL组复制协议:
三个实验主机的mysql的主配置文件的编写
实验环境:参与饰演的三台主机,必须具有完全相同的数据,所以需要对数据库重新进行初始化!
初始化的过程中,登陆数据库之后在进行改密码的操作,不要利用脚本进行数据库的初始化!

[root@server1 data]# cat /etc/my.cnf
[mysqld]
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/data/mysql.sock
symbolic-links=0

disabled_storage_engines="MyISAM,BLACKHOLE,FEDERATED,ARCHIVE,MEMORY"
server_id=1
gtid_mode=ON
enforce_gtid_consistency=ON
master_info_repository=TABLE
relay_log_info_repository=TABLE
binlog_checksum=NONE
log_slave_updates=ON
log_bin=binlog
binlog_format=ROW

plugin_load_add='group_replication.so'
transaction_write_set_extraction=XXHASH64
group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
group_replication_start_on_boot=off
group_replication_local_address= "172.25.0.1:33061"
group_replication_group_seeds= "172.25.0.1:33061,172.25.0.2:33061,172.25.0.3:33061"
group_replication_bootstrap_group=off
group_replication_ip_whitelist="172.25.0.0/24,127.0.0.1/8"
group_replication_single_primary_mode=OFF
group_replication_enforce_update_everywhere_checks=ON
[root@server2 data]# cat /etc/my.cnf
[mysqld]
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/data/mysql.sock
symbolic-links=0

disabled_storage_engines="MyISAM,BLACKHOLE,FEDERATED,ARCHIVE,MEMORY"
server_id=2
gtid_mode=ON
enforce_gtid_consistency=ON
master_info_repository=TABLE
relay_log_info_repository=TABLE
binlog_checksum=NONE
log_slave_updates=ON
log_bin=binlog
binlog_format=ROW

plugin_load_add='group_replication.so'
transaction_write_set_extraction=XXHASH64
group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
group_replication_start_on_boot=off
group_replication_local_address= "172.25.0.2:33061"
group_replication_group_seeds= "172.25.0.1:33061,172.25.0.2:33061,172.25.0.3:33061"
group_replication_bootstrap_group=off
group_replication_ip_whitelist="172.25.0.0/24,127.0.0.1/8"
group_replication_single_primary_mode=OFF
group_replication_enforce_update_everywhere_checks=ON
group_replication_allow_local_disjoint_gtids_join=ON

[root@server3 data]# cat /etc/my.cnf
[mysqld]
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/data/mysql.sock
symbolic-links=0

disabled_storage_engines="MyISAM,BLACKHOLE,FEDERATED,ARCHIVE,MEMORY"
server_id=3
gtid_mode=ON
enforce_gtid_consistency=ON
master_info_repository=TABLE
relay_log_info_repository=TABLE
binlog_checksum=NONE
log_slave_updates=ON
log_bin=binlog
binlog_format=ROW

plugin_load_add='group_replication.so'
transaction_write_set_extraction=XXHASH64
group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
group_replication_start_on_boot=off
group_replication_local_address= "172.25.0.3:33061"
group_replication_group_seeds= "172.25.0.1:33061,172.25.0.2:33061,172.25.0.3:33061"
group_replication_bootstrap_group=off
group_replication_ip_whitelist="172.25.0.0/24,127.0.0.1/8"
group_replication_single_primary_mode=OFF
group_replication_enforce_update_everywhere_checks=ON
group_replication_allow_local_disjoint_gtids_join=ON

数据库的操作

alter user root@localhost identified by 'westos';
show databases;
mysql> SET SQL_LOG_BIN=0;
mysql> CREATE USER rpl_user@'%' IDENTIFIED BY 'westos';
mysql> GRANT REPLICATION SLAVE ON \*.* TO rpl_user@'%';
mysql> GRANT BACKUP_ADMIN ON \*.* TO rpl_user@'%';                                           ##没有执行
mysql> FLUSH PRIVILEGES;
mysql> SET SQL_LOG_BIN=1;

mysql> CHANGE MASTER TO MASTER_USER='rpl_user', MASTER_PASSWORD='password' FOR CHANNEL 'group_replication_recovery';

mysql> SET GLOBAL group_replication_bootstrap_group=ON;                                   (只有第一个节点执行)
mysql> START GROUP_REPLICATION;
mysql> SET GLOBAL group_replication_bootstrap_group=OFF;                                          (只有第一个节点执行)

SELECT * FROM performance_schema.replication_group_members;


mysql> SET SQL_LOG_BIN=0;                                ##不保存数据到日志
mysql> CREATE USER rpl_user@'%' IDENTIFIED BY 'password';    创建远程登陆数据库的用户
mysql> GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%';
mysql> GRANT BACKUP_ADMIN ON *.* TO rpl_user@'%';
mysql> FLUSH PRIVILEGES;
mysql> SET SQL_LOG_BIN=1;                             ##打开日志存储的功能

mysql> CHANGE MASTER TO MASTER_USER='rpl_user', MASTER_PASSWORD='password' FOR CHANNEL 'group_replication_recovery';      ##告诉系统数据库的用户的账户和密码




在作第一个节点的时候必须要有引导组;执行以下命令
mysql> SET GLOBAL group_replication_bootstrap_group=ON;    只有第一个节点执行
mysql> START GROUP_REPLICATION;
mysql> SET GLOBAL group_replication_bootstrap_group=OFF;   只有第一个节点执行

你可能感兴趣的:(linux)