实验名称:搭建MySQL主从复制服务器
实验要求:搭建MySQL服务器,并完成主从复制
实验环境:centos7主服务器一台,IP地址192.168.22.100;centos7从服务器1,IP地址192.168.22.101;
实验步骤:
(一),配置实验环境
主服务器配置环境
[root@localhost ~]# vim /etc/hostname 设置主服务器名称
master.sevenwin.org
[root@localhost ~]# vim /etc/hosts 设置本地host文件缓存
192.168.22.100 master master.sevenwin.org
192.168.22.101 slave slave.sevenwin.org
[root@localhost ~]# vim /etc/selinux/config 修改沙河安全等级
SELINUX=permissive
[root@localhost ~]# reboot 重新启动生效配置
从服务器配置环境
[root@localhost ~]# vim /etc/hostname 设置从服务器名称
master.sevenwin.org
[root@localhost ~]# vim /etc/hosts 设置从服务器本地host缓存
192.168.22.100 master master.sevenwin.org
192.168.22.101 slave slave.sevenwin.org
[root@localhost ~]# vim /etc/selinux/config 修改沙河安全等级
SELINUX=permissive
[root@localhost ~]# reboot 重新启动神效配置
(二),主服务器安装Mariadb
Ping主服务器和从服务器看是否可以ping通
[root@master ~]# ping master.sevenwin.org
PING master (192.168.22.100) 56(84) bytes of data.
64 bytes from master (192.168.22.100): icmp_seq=1 ttl=64 time=0.045 ms
64 bytes from master (192.168.22.100): icmp_seq=2 ttl=64 time=0.048 ms
^C
--- master ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.045/0.046/0.048/0.007 ms
[root@master ~]# ping slave.sevenwin.org
PING slave (192.168.22.101) 56(84) bytes of data.
64 bytes from slave (192.168.22.101): icmp_seq=1 ttl=64 time=0.423 ms
64 bytes from slave (192.168.22.101): icmp_seq=2 ttl=64 time=0.331 ms
^C
--- slave ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 0.331/0.377/0.423/0.046 ms
配置网络源yum
[root@master ~]# cd /etc/yum.repos.d/
[root@master yum.repos.d]# ls
yum.repos.d.bak
[root@master yum.repos.d]# vim yum.repo 配置网络源yum
#CentOS-Base.repo
#
#The mirror system uses the connecting IP address of the client and the
#update status of each mirror to pick mirrors that are updated to and
#geographically close to the client. You should use this for CentOS updates
#unless you are manually picking other mirrors.
#
#If the mirrorlist= does not work for you, as a fall back you can try the
#remarked out baseurl= line instead.
#
#
[base]
name=CentOS-$releasever - Base
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/os/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
#released updates
[updates]
name=CentOS-$releasever - Updates
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/updates/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/extras/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/centosplus/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
[root@master yum.repos.d]# yum clean all 清空yum缓存
Loaded plugins: fastestmirror, langpacks
Cleaning repos: base extras updates
Cleaning up everything
Cleaning up list of fastest mirrors
[root@master yum.repos.d]# yum makecache 加载数据
[root@master yum.repos.d]# yum -y install yum-utils 安装yum扩展
[root@master yum.repos.d]# yum -y install mariadb-server 安装mysql数据库软件
(三),从服务器安装Mariadb
[root@slave ~]# cd /etc/yum.repos.d/
[root@slave yum.repos.d]# ls
CentOS-Base.repo CentOS-CR.repo CentOS-Debuginfo.repo CentOS-fasttrack.repo CentOS-Media.repo CentOS-Sources.repo CentOS-Vault.repo
[root@slave yum.repos.d]# mkdir yum.repos.d.bak 创建yum备份目录
[root@slave yum.repos.d]# mv C* yum.repos.d.bak/ 移动备份
[root@slave yum.repos.d]# ls
yum.repos.d.bak
[root@slave yum.repos.d]# vim yum.repo 配置网络源yum
#CentOS-Base.repo
#
#The mirror system uses the connecting IP address of the client and the
#update status of each mirror to pick mirrors that are updated to and
#geographically close to the client. You should use this for CentOS updates
#unless you are manually picking other mirrors.
#
#If the mirrorlist= does not work for you, as a fall back you can try the
#
#remarked out baseurl= line instead.
#
#
[base]
name=CentOS-$releasever - Base
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/os/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
#released updates
[updates]
name=CentOS-$releasever - Updates
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/updates/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/extras/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/centosplus/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
[root@slave yum.repos.d]# yum clean all 清空yum缓存
Loaded plugins: fastestmirror, langpacks
Cleaning repos: base extras updates
Cleaning up everything
Cleaning up list of fastest mirrors
[root@slave yum.repos.d]# yum makecache 重新加载数据
[root@slave yum.repos.d]# yum -y install yum-utils 安装yum扩展
[root@slave yum.repos.d]# yum -y install mariadb-server 安装mysql数据库软件
(四),配置日志(主服务器)
[root@master yum.repos.d]# vim /etc/my.cnf 配置日志
[mysqld]
log-bin=/var/log/mysql/master-bin bin-log日志存放位置以及名称
server-id=1 ID号,不可与其他服务器冲突
sync_binlog=1 同步二进制日志,事务提交则马上将内存中的二进制日志同步到磁盘
innodb_flush_log_at_trx_commit=1 innodb在事务提交时立即将日志文件写入到磁盘
innodb_support_xa=on 开启分布式事务
[root@master yum.repos.d]# mkdir /var/log/mysql 穿件日志目录
[root@master yum.repos.d]# chown -R mysql:mysql /var/log/mysql/ 更改属主属组
[root@master yum.repos.d]# systemctl enable mariadb 添加开机启动
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
[root@master yum.repos.d]# systemctl start mariadb 启动mysql
[root@master yum.repos.d]# firewall-cmd --add-port=3306/tcp 设置防火墙规则
success
[root@master yum.repos.d]# firewall-cmd --add-port=3306/tcp --permanent 永久开启3306的tcp端口
success
[root@master yum.repos.d]# mysqladmin -u root password 设置数据库密码
New password:
Confirm new password:
[root@master yum.repos.d]# mysql -u root -p 进入数据库
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 5.5.68-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> grant replication client,replication slave on *.* to 'lxy'@'192.168.22.%' identified by '123'; 授权用户
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges; 刷新
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show master status; 查看主服务器数据库的bin-log日志文件名称以及号
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000003 | 626 | | |
+-------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
(五),配置日志(从服务器)
[root@slave yum.repos.d]# vim /etc/my.cnf 配置从服务器日志
[mysqld]
relay-log = relay-bin bin-log日志名称
server-id = 10 ID号(不可重复)
read-only = on 只读
[root@slave yum.repos.d]# systemctl enable mariadb 开机自启动
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
[root@slave yum.repos.d]# systemctl start mariadb 开启数据库
[root@slave yum.repos.d]# mysqladmin -u root password 设置密码
New password:
Confirm new password:
[root@slave yum.repos.d]# mysql -u root -p 进入数据库
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.68-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='192.168.22.100',MASTER_USER='lxy',MASTER_PASSWORD='123';
Query OK, 0 rows affected (0.00 sec))
MariaDB [(none)]> change master to master_host='192.168.22.100',master_user='lxy',master_password='123',master_port=3306,master_log_file='master-bin.000003',master_log_pos=626; 授权
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> start slave; 开启
Query OK, 0 rows affected (0.03 sec)
MariaDB [(none)]> show slave status \G; 查看
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.22.100
Master_User: lxy
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000003
Read_Master_Log_Pos: 626
Relay_Log_File: relay-bin.000002
Relay_Log_Pos: 530
Relay_Master_Log_File: master-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
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: 626
Relay_Log_Space: 818
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: 0
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: 1
1 row in set (0.00 sec)
ERROR: No query specified
(六)验证
在主服务器中创建数据库
MariaDB [(none)]> create database lxyy;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| lxyy |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
从服务器查看
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| lxyy |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.01 sec)
【至此,主从复制服务搭建完成】