部署MariaDB主从数据库集群服务

来自CloudWiki
跳转至: 导航搜索

实训环境

两台使用CentOS 7.2系统的虚拟机:db1,db2

实训步骤

安装MariaDB服务

通过YUM命令在db1和db2虚拟机节点上安装MariaDB服务,命令如下。

yum install  mariadb mariadb-server

2个节点启动MariaDB服务,并设置MariaDB服务为开机自启。

 systemctl start mariadb
 systemctl enable mariadb

初始化MariaDB数据库

在db1和db2虚拟机节点上初始化MariaDB数据库,并设置MariaDB数据库root访问用户的密码为123456。

[root@db1 ~]# mysql_secure_installation

/usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found
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
New password:                               #输入数据库root密码123456
Re-enter new password:                        #重复输入密码123456
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
 ... 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
 - 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!

配置数据库集群主节点

编辑主节点db1虚拟机的数据库配置文件my.cnf,在配置文件my.cnf中增添下面的内容。

[root@db1 ~]# cat /etc/my.cnf

[mysqld]
log_bin = mysql-bin                       #记录操作日志
binlog_ignore_db = mysql                  #不同步MySQL系统数据库
server_id = 18                            #数据库集群中的每个节点id都要不同,一般使用IP地址的最后段的数字,例如172.16.51.18,server_id就写18 

datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

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

直接粘贴版:

[mysqld]
log_bin=mysql-bin
binlog_ignore_db=mysql
server_id=145

datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0

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

编辑完成配置文件my.cnf后,重启MariaDB服务。

[root@db1 ~]# systemctl restart mariadb

开放主节点的数据库权限

在主节点db1虚拟机上使用mysql命令登录MariaDB数据库,授权在任何客户端机器上可以以root用户登录到数据库。

[root@db1 ~]# mysql -uroot -p123456

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 137
Server version: 10.3.18-MariaDB-log 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 all privileges  on *.* to root@'%' identified by "123456";

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.000 sec)

在主节点db1数据库上创建一个user用户让从节点db2连接,并赋予从节点同步主节点数据库的权限,命令如下。

 MariaDB [(none)]> grant replication slave on *.* to 'user'@'db2' identified by '123456';

配置从节点db2同步主节点db1

在从节点db2虚拟机上使用mysql命令登录MariaDB数据库,配置从节点连接主节点的连接信息。master_host为主节点主机名db1,master_user为在步骤(4)中创建的用户user,命令如下。

[root@db2 ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 88
Server version: 10.3.18-MariaDB-log 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='db1',master_user='user',master_password='123456';

配置完毕主从数据库之间的连接信息之后,开启从节点服务。使用show slave status\G; 命令并查看从节点服务状态,如果Slave_IO_Running和Slave_SQL_Running的状态都为YES,则从节点服务开启成功。

MariaDB [(none)]> start slave;
MariaDB [(none)]> show slave status\G;

注:第一次启动时,可能会显示Error:.... 这是正常的。

验证主从数据库的同步功能

先在主节点db1的数据库中创建库test,并在库test中创建表company,插入表数据。创建完成后,查看表company数据,如下所示。

MariaDB [(none)]> create database test;
Query OK, 1 row affected (0.001 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.165 sec)
MariaDB [test]> insert into company values(1,"facebook","usa");
Query OK, 1 row affected (0.062 sec)
MariaDB [test]> select * from company;
+----+----------+------+
| id | name     | addr |
+----+----------+------+
|  1 | facebook | usa  |
+----+----------+------+
1 row in set (0.000 sec)

这时从节点db2的数据库就会同步主节点数据库创建的test库,可以在从节点查询test数据库与表company,如果可以查询到信息,就能验证主从数据库集群功能在正常运行。查询结果如下所示。

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.001 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

MariaDB [(none)]> select * from test.company;
+----+----------+------+
| id | name     | addr |
+----+----------+------+
|  1 | facebook | usa  |
+----+----------+------+
1 row in set (0.001 sec)