Centos7 主从 读写与分离

1.准备配置

1.1基础配置

三台Centos7虚拟机(可以先做一台,后面克隆)

1.2主机分布

IP 主机名 节点
192.168.237.133 db1 Mariadb数据库集群主节点
192.168.237.134 db2 Mariadb数据库集群主节点
192.168.237.132 mycat Mycat中间件服务节点

2.安装基础的配置

2.1 更改主机名

1.db1(主节点)

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

2.db2(从节点)

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

3.mycat

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

2.2 修改hosts文件

1.db1

[root@db1 ~]# vim /etc/hosts(三台虚拟机都要有此操作)
    127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
    ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
    192.168.237.133 db1
    192.168.237.134 db2
    192.168.237.132 mycat
————————————————

2.db2

[root@db2 ~]# scp root@192.168.237.133:/etc/hosts /etc/hosts
The authenticity of host '192.168.237.133 (192.168.237.133)' can't be established.
ECDSA key fingerprint is SHA256:IhEO6nbmFRirAb9Y9ge3YJaEu5sEClOGEKs77gKd+Cw.
ECDSA key fingerprint is MD5:66:a8:47:36:0b:56:50:a4:08:b5:8e:8c:ee:7a:8e:33.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.237.133' (ECDSA) to the list of known hosts.
root@192.168.237.133's password:
hosts                                                  100%  220   505.0KB/s   00:00

3.mycat

[root@mycat ~]# scp root@192.168.237.133:/etc/hosts /etc/hosts
The authenticity of host '192.168.237.133 (192.168.237.133)' can't be established.
ECDSA key fingerprint is SHA256:IhEO6nbmFRirAb9Y9ge3YJaEu5sEClOGEKs77gKd+Cw.
ECDSA key fingerprint is MD5:66:a8:47:36:0b:56:50:a4:08:b5:8e:8c:ee:7a:8e:33.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.237.133' (ECDSA) to the list of known hosts.
root@192.168.237.133's password:
hosts                                                  100%  220   380.5KB/s   00:00

2.3 mycat安装java环境

[root@mycat ~]# yum -y install  list java* | grep java-1.8.0-openjdk-devel
java-1.8.0-openjdk-devel.i686              1:1.8.0.342.b07-1.el7_9       updates
java-1.8.0-openjdk-devel.x86_64            1:1.8.0.342.b07-1.el7_9       updates
[root@mycat ~]# yum -y install java-1.8.0-openjdk-devel
[root@mycat ~]# java -version
openjdk version "1.8.0_342"
OpenJDK Runtime Environment (build 1.8.0_342-b07)
OpenJDK 64-Bit Server VM (build 25.342-b07, mixed mode)

2.4 关闭防火墙和Selinux设置

1.db1:

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

2.db2:

[root@db2 ~]# systemctl stop firewalld
[root@db2 ~]# setenforce 0

3.mycat

[root@mycat ~]# systemctl stop firewalld
[root@mycat ~]# setenforce 0

2.5 安装并初始化Mariadb服务

1.db1:

1.1安装mariadb

[root@db1 ~]# yum -y install mariadb mariadb-server
[root@db1 ~]# systemctl start mariadb
[root@db1 ~]#  netstat -tunpl | grep 3306
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      1417/mysq
[root@db1 ~]# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
[root@db1 ~]#
1.2 初始化
[root@master ~]# 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): // 输入root密码默认为空
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:
Re-enter new password:
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] y  // 是否拒绝root远程登陆
 ... Success!

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.

做完到这可以克隆。也可以不克隆继续做下面的。

2. db2:

2.1 安装
[root@db2 ~]# yum -y install mariadb mariadb-server
[root@db2 ~]# systemctl start mariadb
[root@db2 ~]# netstat -tunpl | grep 3306
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      1394/mysqld
[root@db2 ~]# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
[root@db2 ~]#



2.2 初始化
[root@db2 ~]# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
[root@node ~]# 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:
Re-enter new password:
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] y
 ... Success!

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.

T
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值