构建读写分离的数据库集群

规划节点

Linux操作系统的节点规划,见下表

IP 主机名 节点
192.168.200.10 mycat Mycat 中间件服务节点
192.168.200.20 db1 MariaDB 数据库集群主节点
192.168.200.30 db1 MariaDB 数据库集群从节点

基础环境配置

(1)修改主机名

使用 hostnamectl 命令修改 3 台主机的主机名。
Mycat 节点修改主机名命令:

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

db1 节点修改主机名命令:

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

db2 节点修改主机名命令:

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

(2)关闭防火墙及SELinux服务

3 台集群虚拟机关闭防火墙firewalld及SELinux服务,命令如下:

# setenforce 0 
# systemctl stop firewalld

(3)编辑 hosts 文件

3 台集群虚拟机的/etc/hosts 文件配置部分:

# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.200.10 mycat
192.168.200.20 db1
192.168.200.30 db2

(4)配置 yum 安装源

移除原有的本地repo文件,

# mv /etc/yum.repos.d/* /media/

创建本地Yum源文件local.repo。

# cat /etc/yum.repos.d/local.repo 
[centos]
name=centos
baseurl=file:///opt/centos
gpgcheck=0
enabled=1
[mariadb]
name=mariadb
baseurl=file:///root/gpmall-repo
gpgcheck=0
enabled=1
# mkdir /opt/centos
# mount CentOS-7-x86_64-DVD-1511.iso /opt/centos/
mount: /dev/loop0 is write-protected, mounting read-only

验证本地repo文件是否配置成功,

# yum clean all
Loaded plugins: fastestmirror
Cleaning repos: centos mariadb
Cleaning up everything
Cleaning up list of fastest mirrors
# yum repolist
Loaded plugins: fastestmirror
centos                                                                        | 3.6 kB  00:00:00     
mariadb                                                                       | 2.9 kB  00:00:00     
(1/3): centos/group_gz                                                        | 155 kB  00:00:00     
(2/3): mariadb/primary_db                                                     | 144 kB  00:00:00     
(3/3): centos/primary_db                                                      | 2.8 MB  00:00:00     
Determining fastest mirrors
repo id                                        repo name                                       status
centos                                         centos                                          3,723
mariadb                                        mariadb                                           165
repolist: 3,888

(4)安装 JDK 环境

Mycat 节点安装 Java 环境:

# yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel
...
[root@mall ~]# java -version
openjdk version "1.8.0_222"
OpenJDK Runtime Environment (build 1.8.0_222-b10)
OpenJDK 64-Bit Server VM (build 25.222-b10, mixed mode)

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

(1)安装 MariaDB 服务

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

# yum install -y mariadb mariadb-server

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

# systemctl start mariadb
# systemctl enable mariadb

(2)初始化 MariaDB 数据库

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

[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
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
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值