安装数据库,在centos7中MySQL数据库为mariadb,此次试验需要开启两台Linux虚拟机做数据库的备份(主从复制)
两台都安装数据库
[root@localhost ~]# yum -y install mariadb*
![](https://images2015.cnblogs.com/blog/1096705/201701/1096705-20170130162821729-2126224355.png)
开启数据库
[root@localhost ~]# systemctl start mariadb
![](https://images2015.cnblogs.com/blog/1096705/201701/1096705-20170130163131526-771610349.png)
两台服务器都必须关闭防火墙
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
![](https://images2015.cnblogs.com/blog/1096705/201701/1096705-20170130170901370-177628909.png)
由于数据库的旧数据无法做主从复制,所以需要先导出数据,在利用某些服务复制到另一台虚拟机(从服务器)上,以备导入备份数据库
[root@localhost ~]# mysqldump --all-databases > all.sql
![](https://images2015.cnblogs.com/blog/1096705/201701/1096705-20170130163355229-96154084.png)
咱们此次用的是ssh,现在安装ssh
[root@localhost ~]# yum -y install openssh*
![](https://images2015.cnblogs.com/blog/1096705/201701/1096705-20170130163446995-1141799771.png)
开始ssh服务
[root@localhost ~]# systemctl start sshd
![](https://images2015.cnblogs.com/blog/1096705/201701/1096705-20170130163559433-1456704041.png)
上传到从服务器上
scp /root/all.sql root@192.168.209.111:/root
![](https://images2015.cnblogs.com/blog/1096705/201701/1096705-20170130163723683-1300362627.png)
将数据导入到数据库
[root@localhost ~]# mysql < all.sql
![](https://images2015.cnblogs.com/blog/1096705/201701/1096705-20170130163848026-504021991.png)
编辑数据库配置文件,配置主从复制条件
[root@localhost ~]# vi /etc/my.cnf
![](https://images2015.cnblogs.com/blog/1096705/201701/1096705-20170130164401479-1853088660.png)
(主服务器配置文件中的添加项)
server-id=1
log-bin=mysql-bin
![](https://images2015.cnblogs.com/blog/1096705/201701/1096705-20170130164122901-1762696857.png)
(从服务器的添加项)
![](https://images2015.cnblogs.com/blog/1096705/201701/1096705-20170130164720167-54228884.png)
创建远程用户并授予复制的权限
MariaDB [(none)]> grant replication slave on *.* to lll@'%' identified by '111’
Query OK, 0 rows affected (0.01 sec)
![](https://images2015.cnblogs.com/blog/1096705/201701/1096705-20170130164010745-246181106.png)
查看主服务器文件信息
MariaDB [(none)]> show master status;
![](https://images2015.cnblogs.com/blog/1096705/201701/1096705-20170130165415214-585449213.png)
此处是从服务器的数据库中,关闭复制功能
MariaDB [(none)]> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
![](https://images2015.cnblogs.com/blog/1096705/201701/1096705-20170130165504901-1778196513.png)
关联主数据库,主从复制
MariaDB [(none)]> change master to master_host='192.168.209.110',
-> master_user='lll',
-> master_password='111',
-> master_log_file='mysql-bin.000003',
-> master_log_pos=245;
Query OK, 0 rows affected (0.01 sec)
![](https://images2015.cnblogs.com/blog/1096705/201701/1096705-20170130165657120-1149768483.png)
开始功能
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)
![](https://images2015.cnblogs.com/blog/1096705/201701/1096705-20170130165734370-555120084.png)
查看数据库同步状态,出现双‘yes’,则表示同步成功
MariaDB [(none)]> show slave statusG;
![](https://images2015.cnblogs.com/blog/1096705/201701/1096705-20170130171319698-582897218.png)