1.基礎環境配置

IP地址

主機名

節點

192.168.200.10

db1


主節點

192.168.200.20

db2

從節點

(1)創建兩台虛擬機,節點為192.168.200.10(20),分別更改主機名,此處以db1為例

[root@localhost ~]# hostnamectl set-hostname db1
[root@localhost ~]# bash
[root@db1 ~]#

(2)兩台虛擬機配置本地yum源,此處以db1為例

[root@db1 ~]# mkdir /opt/centos
[root@db1 ~]# mount /dev/cdrom /opt/centos
mount: /dev/sr0 is write-protected, mounting read-only
[root@db1 ~]# mv /etc/yum.repos.d/* /media/
[root@db1 ~]# vi /etc/yum.repos.d/local.repo
[centos]
name=centos
baseurl=file:///opt/centos
gpgcheck=0
enabled=1
[root@db1 ~]# yum clean all
[root@db1 ~]# yum repolist

(3)兩台虛擬機關閉防火牆和selinux,此處以db1為例

[root@db1 ~]# systemctl stop firewalld
[root@db1 ~]# systemctl disable firewalld
[root@db1 ~]# setenforce 0

(4)兩台虛擬機修改映射文件,此處以db1為例

[root@db1 ~]# vi /etc/hosts
192.168.200.10 db1
192.168.200.20 db2
2.初始化MariaDB數據庫服務,此處以db1為例

(1)安裝MariaDB數據庫並啓動

[root@db1 ~]# yum install -y mariadb mariadb-server
[root@db1 ~]# systemctl start mariadb
[root@db1 ~]# systemctl enable mariadb

(2)初始化MariaDB數據庫

[root@db1 ~]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):	#默認回車
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] y	#確定設置root密碼
New password:			#輸入數據庫root密碼000000
Re-enter new password:	#重複輸入設置的root密碼
Password updated successfully!
Reloading privilege tables..
 ... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y	#確認刪除匿名賬户
 ... Success!
Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] n   #允許root遠程連接
 ... skipping.
By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] y   #刪除test數據庫
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y   #確認初始化
 ... Success!
Cleaning up...
All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!

(3)登錄MariaDB數據庫

[root@db1 ~]# mysql -uroot -p000000
MariaDB [(none)]>

(4)使用Ctrl+C組合鍵退出MariaDB數據庫

MariaDB [(none)]> Ctrl-C -- exit!
Aborted
[root@db1 ~]#
3.部署主從複製數據庫服務

(1)分別修改db1和db2的/etc/my.cnf數據庫配置文件,在[mysqld]下增加三行配置

[root@db1 ~]# vi /etc/my.cnf
[mysqld]
log_bin = mysql-bin			#記錄操作日誌
binlog_ignore_db = mysql		#不同步MYSQL系統數據庫
server_id = 1					#數據庫集羣中每個節點的id都要不同

[root@db2 ~]# vi /etc/my.cnf
[mysqld]
log_bin = mysql-bin
binlog_ignore_db = mysql
server_id = 2

(2)在db1上重啓MariaDB數據庫並設置主節點權限

[root@db1 ~]# systemctl restart mariadb
[root@db1 ~]# mysql -uroot -p000000
MariaDB [(none)]> grant all privileges on *.* to root@'localhost' identified by '000000'; #授權本地使用root用户登錄到數據庫
MariaDB [(none)]> grant all privileges on *.* to root@'%' identified by '000000'; #授權任何客户端機器都可以使用root用户登錄到數據庫
MariaDB [(none)]> grant replication slave on *.* to 'user'@'db2' identified by '000000'; #創建user用户供從節點db2連接
MariaDB [(none)]> flush privileges; #刷新權限

(3)在db2上重啓MariaDB數據庫並設置從節點權限

[root@db2 ~]# systemctl restart mariadb
[root@db2 ~]# mysql -uroot -p000000
MariaDB [(none)]> change master to master_host='db1',master_user='user',master_password='000000'; #通過user用户連接主節點db1
MariaDB [(none)]> flush privileges; #刷新權限
MariaDB [(none)]> Ctrl-C -- exit!
[root@db2 ~]# systemctl restart mariadb #再次重啓MariaDB數據庫
[root@db2 ~]# mysql -uroot -p000000
MariaDB [(none)]> start slave; #開啓從節點服務
MariaDB [(none)]> show slave status\G; #查看從節點服務狀態

當Slave_IO_Running和Slave_SQL_Running的狀態都為YES,則説明主從數據庫搭建成功

4.驗證主從數據庫功能

(1)在主節點db1上創建庫test

[root@db1 ~]# mysql -uroot -p000000
MariaDB [(none)]> create database test;
MariaDB [(none)]> use test
MariaDB [test]> create table company(id int not null primary key,name varchar(50),addr varchar(255));
MariaDB [test]> insert into company values(1,"baidu","china");
MariaDB [test]> select * from company;
+----+-------+-------+
| id | name  | addr  |
+----+-------+-------+
|  1 | baidu | china |
+----+-------+-------+

(2)在從節點db2上查看,其會自動同步db1上創建的庫

[root@db2 ~]# mysql -uroot -p000000
MariaDB [(none)]> use test;
MariaDB [test]> select * from company;
+----+-------+-------+
| id | name  | addr  |
+----+-------+-------+
|  1 | baidu | china |
+----+-------+-------+