云--分布式mariadb(MySQL)数据库

系统镜像:CentOS-7-x86_64-DVD-1511.iso
虚拟机软件:VMware® Workstation 16 Pro VMware® Workstation 16 Pro
主机:mysql1(192.168.200.131);mysql2(192.168.200.132)
统一命令执行程序:bash
流程:ip映射 → 关闭安全防护 → 配置本地yum源 → 安装、启动服务、配置与初始化mariadb → 查看与测试

SELinux是一个安全子系统,它提供了对访问控制的额外层次,可以强制执行或警告不符合安全策略的行为。
Enforcing:SELinux正在强制执行其安全策略。
Permissive:SELinux不会强制执行其安全策略,但会记录所有违反策略的行为。
准备

一、准备

ip与主机名映射

1.网络主机

操作机:mysql1

hostnamectl set-hostname mysql1

操作机:mysql2

hostnamectl set-hostname mysql2
ps:
执行bash命令使修改在当前进程中生效

操作机:mysql1 mysql2

echo 192.168.200.131 mysql1 >> /etc/hosts;

echo 192.168.200.132 mysql2 >> /etc/hosts;

读写权限问题解决:

2.关闭安全防护

操作机:mysql1 mysql2

2.1 关闭防火墙

2.1.1 firewalld

systemctl stop firewalld

2.1.2 iptables

iptables -F
iptables -X
iptables -Z
iptables-save

ps:
systemctl disable firewalld 禁止开机自启动
iptables -L 查看iptables状态
getenforce 查看SELinux状态
SELinux每回重启都会自动开启,需要重新关闭

2.2.关闭selinux

setenforce 0

包管理器设置

3.本地yum源

操作机:mysql1 mysql2

3.1 挂载镜像文件

mkdir /opt/packages
mount /dev/cdrom / /opt/packages

3.2 配置repo文件

3.2.1 原始配置文件目录备份

mv /etc/yum.repos.d/ /etc/yum.repos.d.bak

3.2.2 配置文件编辑

mkdir /etc/yum.repos.d/
echo [Local] >> /etc/yum.repos.d/Packages-Local.repo
echo name=local >> /etc/yum.repos.d/Packages-Local.repo
echo baseurl=file:///opt/packages >> /etc/yum.repos.d/Packages-Local.repo
echo gpgcheck=0 >> /etc/yum.repos.d/Packages-Local.repo
echo enable=1 >> /etc/yum.repos.d/Packages-Local.repo
yum clean all

三、安装与配置

5.安装mariadb

操作机:mysql1 mysql2

yum -y install mariadb mariadb-server

6.配置

6.1 启动服务

操作机:mysql1 mysql2

systemctl start mariadb
ps:
systemctl enable firewalld 设置开机自启动

6.2 初始化

操作机:mysql1 mysql2

/usr/bin/mysql_secure_installation
ps:
find / -name "mysql*"
[root@bogon ~]# /usr/bin/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] 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!

6.3 id

操作机:mysql1

sed -i "/\[mysqld\]/aserver_id=10" /etc/my.cnf

操作机:mysql2

sed -i "/\[mysqld\]/aserver_id=20" /etc/my.cnf

操作机:mysql1 mysql2

sed -i "/\[mysqld\]/alog_bin=mysql-bin" /etc/my.cnf

6.4 权限

操作机:mysql1 mysql2

mysql -uroot -p000000 -e"

grant all privileges on *.* to 'root'@'%' identified by '000000';

"

操作机:mysql1

mysql -uroot -p000000 -e"

grant replication slave on *.* to 'user'@'mysql2' identified by '000000';

"

ps:
允许从机制作副本,当名为mysql2的主机以user身份登陆时验证密码'000000'

操作机:mysql2

mysql -uroot -p000000 -e"

change master to master_host='mysql1',master_user='user',master_password='000000';

"

ps:
更改主机,主机的主机名为mysql1,主机开放登录的用户名为user,密码为000000

6.5 启动主从模式

操作机:mysql1 mysql2

systemctl restart mariadb

操作机:mysql2

mysql -uroot -p000000 -e"

start slave;

"

ps:
每次重启后master状态会发生变化,需要重新启动从机去连接master
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: 529
               Relay_Log_File: mariadb-relay-bin.000002
                Relay_Log_Pos: 813
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes      //启动成功
            Slave_SQL_Running: Yes      //启动成功
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 529
              Relay_Log_Space: 1109
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 10

四、测试

操作机:mysql1

mysql -uroot -p000000 -e"

create database jcg;

use jcg;

create table jcg_table(name varchar(15), age int);

insert into jcg_table (name,age)values('jcg','7336');

"

操作机:mysql2

mysql -uroot -p000000 -e"

show databases;

use jcg;

show tables;

select * from jcg.jcg_table;
" >> /tmp/output;cat /tmp/output

posted @ 2025-04-08 11:05  基础狗  阅读(31)  评论(0)    收藏  举报