主从数据库

本文详细介绍了如何配置MariaDB的主从复制集群。首先,设置了两台虚拟机的主机名和hosts映射,关闭了防火墙,然后在主从节点上安装并启动了mariadb服务。接着,初始化了数据库并修改了配置文件,设置了主从节点的server_id。在主节点上,创建了用于同步的用户并授权,而在从节点上则配置了连接主节点的信息并启动了复制服务。通过检查从节点的状态,确认了主从复制已成功建立。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需要两台虚拟机
主:1、yum源配置(本地)
2、修改主机名
主节点:
[root@localhost ~]# hostnamectl set-hostname feng
[root@localhost ~]# bash
[root@feng ~]#
从节点:
[root@localhost ~]# hostnamectl set-hostname jing
[root@localhost ~]# bash
3、 hosts映射(主从节点)

  [root@feng ~]# vi /etc/hosts
192.168.200.10 feng
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.200.10 feng
192.168.200.40 jing

3、关闭防火墙 (两节点)

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

4、安装软件包(主从节点)
主节点:
[root@feng ~]# yum install mariadb mariadb-server -y
从节点:
[root@jing ~]# yum install mariadb mariadb-server -y
5、启动&开机自启(主从节点)

[root@feng ~]# systemctl start mariadb
[root@feng ~]# systemctl enable  mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.

6、初始化mysql (主从节点 两节点)

[root@feng ~]# 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):      ### 回车
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
Enter current password for root (enter for none): 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
Enter current password for root (enter for none):       ### 不管 就ctrl+c  或 ctrl+d
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] 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    ### 输入 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     ## 输入 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    ### 输入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   ### 输入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!

7、修改配置文件(主从节点)
主节点:

[root@feng ~]# vi /etc/my.cnf
[mysqld]
log_bin = mysql-bin
binlog_ignore_db = mysql
server_id = 10       添加以上内容

从节点:

[root@jing ~]# vi /etc/my.cnf
[mysqld]
log_bin = mysql-bin
binlog_ignore_db = mysql
server_id = 40     填写节点对应id

8、重启数据库服务,并进入数据库
主节点:
在feng节点,授权在任何客户端机器上可以以root用户登录到数据库,然后在主节点上创建一个user用户连接节点jing,并赋予从节点同步主节点数据库的权限。

[root@feng ~]# systemctl restart mariadb
[root@feng ~]# mysql -uroot -p000000
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.44-MariaDB-log MariaDB Server

Copyright (c) 2000, 2015, 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 '000000';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> grant replication slave on *.* to 'user'@'jing' identified by '000000';
Query OK, 0 rows affected (0.00 sec)

从节点:
在从节点 jing 上登录MariaDB数据库,配置从节点连接主节点的连接信息。master_host为主节点主机名 feng master_user为上一步中创建的用户user

[root@jing ~]# systemctl restart mariadb
[root@jing ~]# mysql -uroot -p000000
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.44-MariaDB-log MariaDB Server

Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>MariaDB [(none)]> change master to master_host='feng',master_user='user',master_password='000000';
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> start slave;   开启从节点服务
Query OK, 0 rows affected (0.00 sec)

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: 70
1 row in set (0.00 sec)

MariaDB [(none)]> 

可以看到Slave_IO_Running和Slave_SQL_Running的状态都是Yes,配置数据库主从集群成功

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值