首页 > 技术文章 > linux 之 mysql 主从复制环境搭建专题

jiapengchu 2020-10-31 14:43 原文

1.配置Mysql主从同步

一主一从架构图

 

 

 

一主多从架构图

 

 

 

 

 主从复制原理原理图

 

MySQL复制

 

 

了解完原理后开始搭建

实验环境

192.168.170.7 主数据库服务器

192.168.170.30 主数据从服务器

 

 

 2.初始化工作

分别在主从服务器上安装mysql环境

   86  yum install mariadb-server -y
   87  systemctl start mariadb
   88  mysql_secure_installation

3.配置主服务器,配置serverid 和开启二进制日志

vim /etc/my.cnf

  

 

4.主服务器上创建账号

 

MariaDB [(none)]> grant replication slave on *.* to 'repluser'@'192.168.170.%' identified by 'centos';

 

 从节点配置、

1.改配置文件

 

 

 2.配置change maser to  如果记不住可以用help change master to 查看

CHANGE MASTER TO
  MASTER_HOST='192.168.170.7',#主服务器ip
  MASTER_USER='repluser', #授权账号
  MASTER_PASSWORD='centos',#授权密码
  MASTER_PORT=3306,
  MASTER_LOG_FILE='mysql-bin.000001',#二进制日志
  MASTER_LOG_POS=484;#从哪个节点位置同步,这里的位置是主服务器的,可以用show master logs;查看记录

  

 

 

 3.查看从节点状态

MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.170.7
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 484
Relay_Log_File: mariadb-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: No               #两个线程
Slave_SQL_Running: No
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 484
Relay_Log_Space: 245
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: NULL    #同步延迟秒数
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 0
1 row in set (0.00 sec)

 4.开启从节点两个线程

MariaDB [(none)]> start slave;

5.再次查看,到此我们主从复制已搭建完毕

 

 

 

 

 

 测试,在主服务器上创建数据库dbtest,在从库上查看

 

 

 

 

 从库再次查看,已同步

 2.使用MHA实现Mysql高可用。

读写分离架构图原理

 

 

 

 

 实验环境 4台虚拟机

192.168.170.7  mha

192.168.170.17 主数据库

192.168.170.27 从数据库

192.168.170.37 从数据库

 

 第一步:192.168.170.7  mha 实现步骤

 1.安装软件包,启用epel yum 源,执行

yum install mha*.rpm

  

1.准备配置文件

[server default]
user=lala
password=aaaaaa


manager_workdir=/data/mastermha/app1/
manager_log=/data/mastermha/app1/manager.log
remote_workdir=/data/mastermha/app1/
ssh_user=root
repl_user=repluser
repl_password=abcccccccc
ping_interval=1
[server1]
hostname=192.168.170.17
candidate_master=1
[server2]
hostname=192.168.170.27
candidate_master=1
[server3]
hostname=192.168.170.37
candidate_master=1

  

3.实现基于ssh key验证

[root@mha-master ~]# ssh-keygen  #生成key
ssh-copy-id 192.168.170.7   #拷贝到本机
#分别把key拷贝到远程数据库主机节点
[root@mha-master ~]# scp -r .ssh/ 192.168.170.17:/root/
[root@mha-master ~]# scp -r .ssh/ 192.168.170.27:/root/
[root@mha-master ~]# scp -r .ssh/ 192.168.170.37:/root/

4.检查 有没有错误

#检ssh是否正常链接
[root@mha-master ~]#masterha_check_ssh --conf=/etc/mha/app1.conf  
#检查复制账号是否可用
[root@mha-master ~]#masterha_check_repl --conf=/etc/mha/app1.conf
#启动
[root@mha-master ~]#masterha_manager --conf=
/etc/mha/app1.conf

第二部配置数据库主从同步环境

1.配置主数据库master

[mysqld]
datadir=/var/lib/mysql
log-bin
server_id=1
skip_name_resolve=1



socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

  

创建复制账号和改变主从权限账号

MariaDB [(none)]> grant all on *.* to mhauser@'192.168.170.%’identified by‘bbbbbb';  #创建切换主从账号
MariaDB [(none)]>grant replication slave on *.* to repluser@'192.168.170.%' identified by 'abcccccccc'; #创建复制账号

3.配置从服务器  27,37 一样配置

[mysqld]
server_id=2
log-bin
read_only
relay_log_purge=0
skip_name_resolve=1


datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

  2.设置指向主服务器

CHANGE MASTER TO
  MASTER_HOST='192.168.170.17',
  MASTER_USER='repluser',
  MASTER_PASSWORD='abcccccccc',
  MASTER_PORT=3306,
  MASTER_LOG_FILE='mariadb-bin.000001',
  MASTER_LOG_POS=245;

  3.启动slave

MariaDB [(none)]> start slave

 到此所有环境搭建完毕

测试

1.启动mha服务

 

 

 

 

 

#主服务执行
create table testlog (id int auto_increment primary key,name char(10),age int default 20);

delimiter $$

create procedure  sp_testlog() 
begin  
declare i int;
set i = 1; 
while i <= 100000 
do  insert into testlog(name,age) values (concat('wang',i),i); 
set i = i +1; 
end while; 
end$$

delimiter ;

  call sp_testlog

 

 

关机

此时27已提升成主了我们,看到,实验完毕 37指向了27,到此实验完毕

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

推荐阅读