微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

部署主从数据库

部署主从数据库

 

  1. 基础环境安装

(1) 修改主机名

使用远程连接工具连接到192.168.140.130,192.168.140.131这两台虚拟机,并对这两台虚拟机修改主机名,192.168.140.130为MysqL1,192.168.140.131为MysqL2。

MysqL1节点:

 

 

 

MysqL2节点:

 

 

 

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

   两个节点关闭防火墙及SELinux服务,命令如下:

    [root@MysqL1 ~]# setenforce 0

    [root@MysqL1 ~]# systemctl stop firewalld

(3) 配置hosts文件

   两个节点配置hosts文件修改如下:

    [root@MysqL2 ~]# vim /etc/hosts

    127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4

    ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

    192.168.140.130 MysqL1

    192.168.140.131 MysqL2

(4)安装数据库服务

两个节点自行配置yum源,安装数据库服务,命令如下:

  # yum install -y mariadb mariadb-server

两个节点启动数据库服务并设置开机自启

  # systemctl start mariadb

  # systemctl enable mariadb 

初始化数据库并配置主从服务

(1)初始化数据库

两个节点初始化数据库,配置数据库root密码为000000

 [root@MysqL1 ~]# MysqL_secure_installation

 /usr/bin/MysqL_secure_installation:行379: find_MysqL_client: 未找到命令

 

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:

Sorry, passwords do not match.

 

New password:       #数据库root密码000000

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!

(2)配置MysqL1主节点

修改MysqL1节点的数据库配置文件,在配置文件/etc/my.cnf中的[MysqLd]增添内容

  log_bin = MysqL-bin #记录操作日志

  binlog_ignore_db = MysqL #不同步MysqL系统数据库

  server_id = 130 #数据库集群中的每个节点id都要不同一般用IP地址最后段数字

  datadir = /var/lib/MysqL

  socket = /var/lib/MysqL/MysqL.sockc

重启数据库服务,并进入数据库

  [root@MysqL2 ~]# systemctl restart mariadb

  [root@MysqL2 ~]# 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)]>

MysqL1节点,授权在任何用户客户端机器上可以以root用户登录数据库,然后在主节点上创建一个user用户连接节点MysqL2,并赋予从节点同步主节点数据库权限。

  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'@'MysqL2'identified by '000000';

  Query OK, 0 rows affected (0.00 sec)

(3)配置MysqL2从节点

修改MysqL2节点数据库配置文件,在配置文件/etc/my.cof中的[MysqLd]增添以下内容

  [root@MysqL2 ~]# vi /etc/my.cnf

  [MysqLd]

  datadir=/var/lib/MysqL

  socket=/var/lib/MysqL/MysqL.sock

  log_bin=MysqL-bin

  binlog_ignore_db=MysqL

  server_id=31

  # disabling symbolic-links is recommended to prevent assorted security risks

  symbolic-links=0

  # Settings user and group are ignored when systemd is used.

  # If you need to run MysqLd under a different user or group,

  # customize your systemd unit file for mariadb according to the

  # instructions in http://fedoraproject.org/wiki/Systemd

 

  [MysqLd_safe]

  log-error=/var/log/mariadb/mariadb.log

  pid-file=/var/run/mariadb/mariadb.pid

 

  #

  # include all files from the config directory

  #

  !includedir /etc/my.cnf.d

  在从节点MysqL2上登录Mariadb数据库,配置从节点连接主节点的连接信息。master——host为主节点主机名MysqL1,master_user为上一步创建的用户user

  [root@MysqL2 ~]# systemctl restart mariadb

  [root@MysqL2 ~]# 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)]> change master to

      -> master_host='MysqL1',master_user='user',master_password='000000';

  Query OK, 0 rows affected (0.04 sec)

  配置完毕主从数据库间的信息之后,开启从节点服务。使用show slave status\G;

命令,并查看从节点服务状态,如果Slave_IO_Running和Slave_sql_Running的状态都为yes,则开启成功

  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.000014

            Read_Master_Log_Pos: 886

                 Relay_Log_File: mariadb-relay-bin.000016

                  Relay_Log_Pos: 529

          Relay_Master_Log_File: MysqL-bin.000014

               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: 886

                Relay_Log_Space: 1750

                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: 130

  1 row in set (0.00 sec)

验证数据库主从服务

  (1) 主节点创建数据库

先在主节点MysqL1中创建库test,并在test中创建表compang,插入表数据,创建完成后,查看表company数据

  MariaDB [(none)]> create database test;

  Query OK, 1 row affected (0.00 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.00 sec)

  MariaDB [test]> insert into company values(1,"alibaba","china");

  Query OK, 1 row affected (0.00 sec)

   

 

 

 (2)从节点验证复制功能

登陆MysqL2节点的数据库,查看数据库列表。找到test数据库查询表,并查询内容验证从数据库的复制功能

   

 

 

可以查看到主数据库中刚刚创建的库、表、信息,验证从数据库的复制功能成功。

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐