设置主从数据库的目的是将数据库1和数据库2分别建在两个虚拟机上,并实现数据互通访问
首先准备两个虚拟机,这里示例ip分别为:
192.168.200.10;192.168.200.20
修改主机名,一个是mysql1,一个是mysql2,(可改可不改,方便区别而已,我后续一个是mysql1一个是samba),
然后关闭SELinux和防火墙,并修改hosts的配置文件,下图增加的最后两行是自己虚拟机的ip和主机名,无需和我一样
云计算2主从数据库-LMLPHP
配置yum源,安装数据库服务,注意如何配置yum源,不再重复细节,可以参考云计算1中ftp安装篇,现在设置里检查DVD光盘文件是否连接,再进行以下操作
[root@localhost~]#mount /dev/cdrom /opt/centos 将cd设备挂载到/opt/centos,不存在的话可以先创建
[root@localhost~]#mkdir -p /opt/centos
[root@localhost~]#cd /opt
[root@localhost~]#ll 查看是否创建成功 total 636则挂载成功

[root@localhost~]#mv /etc/yum.repos.d/* /media/ 移除repos.d下的文件
[root@localhost~]#vi /etc/yum.repos.d/local.repo 创建local文件
[centos7]
name=cantos7
baseurl=file:///opt/centos
gpgcheck=0
enabled=1
[root@localhost~]#cat /etc/yum.repos.d/local.repo 查看
进行数据库服务的安装
[root@mysql1 ~]# yum install -y mariadb mariadb-server
云计算2主从数据库-LMLPHP
两台虚拟机一样操作到这一步,然后同样启动数据库服务
[root@mysql1 /]# systemctl start mariadb
[root@mysql1 /]# systemctl enable mariadb
初始化数据库并进行配置
[root@mysql1 /]# mysql_secure_installation
接下来按照提示,set root password?,y,root,000000,remove anonymous users?,y,Disallow root login remotely? ,n
remove test database and access to it?,y,Reload privilege tables now? y
配置mysql1主节点,注意server_id是ip的结束数字
云计算2主从数据库-LMLPHP
进入数据库,使用数据库命令语句创建一个叫test的数据库,在test下创建叫company的表格
[root@mysql1 /]# systemctl restart mariadb
[root@mysql1 /]# mysql -uroot -p000000
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.44-MariaDB-log MariaDB Server

Copyright © 2000, 2015, Oracle, MariaDB Corporation Ab and others.

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

MariaDB [(none)]> grant all privileges on . to root@‘%’ identified by “000000”;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> grant replication slave on . to ‘user’@‘samba’ identified by’000000’;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> create database test;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> use test;
Database changed
MariaDB [test]> create table company(id int not null primary key,name varchar(50),addr varchar(255));
Query OK, 0 rows affected (0.01 sec)

MariaDB [test]> insert into company values(1,‘alibaba’,‘china’);
Query OK, 1 row affected (0.00 sec)

MariaDB [test]> select*from company;
±—±--------±------+
| id | name | addr |
±—±--------±------+
| 1 | alibaba | china |
±—±--------±------+
1 row in set (0.00 sec)
这样主数据库就配置好了
配置从数据库的节点
云计算2主从数据库-LMLPHP
[root@samba ~]# systemctl restart mariadb
[root@samba ~]# mysql -uroot -p000000
MariaDB [(none)]> change master to master_host=‘mysql1’,master_user=‘user’,master_password=‘000000’;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: mysql1
Master_User: user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 527
Relay_Log_File: mariadb-relay-bin.000002
Relay_Log_Pos: 811
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
配置成功,开始验证主从数据库的关联功能,试试能否在虚拟机2也就是从数据库访问到刚才在主数据库建立的表格信息

MariaDB [(none)]> show databases;
±-------------------+
| Database |
±-------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
±-------------------+
4 rows in set (0.00 sec)

MariaDB [(none)]> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [test]> show tables;
±---------------+
| Tables_in_test |
±---------------+
| company |
±---------------+
1 row in set (0.00 sec)

MariaDB [test]> select*from company;
±—±--------±------+
| id | name | addr |
±—±--------±------+
| 1 | alibaba | china |

03-16 05:46