部署主从数据库

技术部署主从数据库 部署主从数据库IP主机名节点192.168.233.11mysql1主数据库节点192.168.233.13mysql2从数据库节点一 安装
(1)修改主机名
用 Xshell 连接

部署主从数据库

IP

主机名

节点

192.168.233.11

mysql1

主数据库节点

192.168.233.13

mysql2

从数据库节点

一 安装

(1)修改主机名

用Xshell连接到192.168.233.11、192.168.233.13 这两台虚拟机,并对这两台虚拟机进行修改主机名的操作,192.168.233.11 主机名修改为mysql1,192.168.233.13主机名修改为mysql2。

mysql1节点:

[root @ localhost ~]# hostname CTL set-hostname MySQL 1

[root@localhost ~]#注销

[root@mysql1 ~]# hostnamectl

静态主机名: mysql1

图标名称:计算机-虚拟机

机箱:虚拟机

机器id : 179 F6 c8 F2 e 7942 ef 81 b 0 f 5565 a 6883 fa

靴子id : 69 ad 020d 53 e 54892 b 9005 f 82 e 182 c 140

虚拟化: vmware

操作系统: CentOS Linux 7(核心)

客户终端设备(Customer Premise Equipment的缩写)操作系统名称: CPE :/o : centos : centos :7

内核: Linux 3。10 .0-327 .El 7。x86 _ 64

体系结构: x86-64

mysql2节点:

[root @ localhost ~]# hostname CTL set-hostname MySQL 2

[root@localhost ~]#注销

[root@mysql2 ~]# hostnamectl

静态主机名: mysql2

图标名称:计算机-虚拟机

机箱:虚拟机

机器id : 179 F6 c8 F2 e 7942 ef 81 b 0 f 5565 a 6883 fa

引导id : 816 b 270 a 1275496 CAA 3254300 fc 359 C4

虚拟化: vmware

操作系统: CentOS Linux 7(核心)

客户终端设备(Customer Premise Equipment的缩写)操作系统名称: CPE :/o : centos : centos :7

内核: Linux 3。10 .0-327 .El 7。x86 _ 64

体系结构: x86-64

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

# setenforce 0

# systemctl停止防火墙d

(3)配置 hosts 文件

两个节点配置/etc/hosts文件(vi/etc/hosts)进入)

127 .0 .0 .1 localhost localhost。本地域本地主机4本地主机

4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.233.11 mysql1
192.168.233.13 mysql2 (输入两个节点IP)

(4)配置 YUM 源并安装数据库服务

挂载CentOS-7-x86_64-DVD-1511.iso镜像并自行配置YUM源,配置完毕后,两个节点安装数据库服务

# yum install -y mariadb mariadb-server

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

# systemctl start mariadb
# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to/usr/lib/systemd/system/mariadb.service.

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

(1)初始化数据库

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

[root@mysql1 ~]# 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):                   #默认按回车
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, 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]增添如下内容

[root@mysql1 ~]# cat /etc/my.cnf
[mysqld]
log_bin = mysql-bin                       #记录操作日志
binlog_ignore_db = mysql                  #不同步mysql系统数据库
server_id = 11                          #数据库集群中的每个节点id都要不同,一般使用IP地址的最后段的数字,例如192.168.233.11,server_id就写11
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# 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

重启数据库服务,并进入数据库,命令如下:

[root@mysql1 ~]# systemctl restart mariadb
[root@mysql1 ~]# mysql -uroot -p123456
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)]

在 mysql2 中

MariaDB [(none)] grant all privileges  on *.* to root@'%' identified by "123456";
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)] grant replication slave on *.* to 'user'@'mysql2' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

(3)配置 mysql2 从节点

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

[root@mysql2 ~]# cat /etc/my.cnf
[mysqld]
log_bin = mysql-bin                       #记录操作日志
binlog_ignore_db = mysql                  #不同步mysql系统数据库
server_id =13                         #数据库集群中的每个节点id都要不同,一般使用IP地址的最后段的数字,例如192.168.233.13,server_id就写13
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# 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 -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.44-MariaDB 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.01 sec)

配置完毕主从数据库之间的连接信息之后,开启从节点服务。使用show slave status\G命令,并查看从节点服务状态,如果Slave_IO_Running和Slave_SQL_Running的状态都为YES,则从节点服务开启成功

MariaDB [(none)] start slave;
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.000003
          Read_Master_Log_Pos: 245
               Relay_Log_File: mariadb-relay-bin.000005
                Relay_Log_Pos: 529
        Relay_Master_Log_File: mysql-bin.000003
             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: 245
              Relay_Log_Space: 1256
              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: 30
1 row in set (0.00 sec)

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

三 验证数据库主从服务

(1)主节点创建数据库

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

[root@mysql1 ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4
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)] 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.01 sec)
MariaDB [test] insert into company values(1,"alibaba","china");
Query OK, 1 row affected (0.01 sec)
MariaDB [test] select * from company;
+----+---------+-------+
| id | name    | addr  |
+----+---------+-------+
|  1 | alibaba | china |
+----+---------+-------+
1 row in set (0.00 sec)

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

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

[root@mysql2 ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 5
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)] show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)
MariaDB [(none)] use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [test] show tables;
+----------------+
| Tables_in_test |
+----------------+
| company        |
+----------------+
1 row in set (0.00 sec)
MariaDB [test] select * from company;
+----+---------+-------+
| id | name    | addr  |
+----+---------+-------+
|  1 | alibaba | china |
+----+---------+-------+
1 row in set (0.00 sec)

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

内容来源网络,如有侵权,联系删除,本文地址:https://www.230890.com/zhan/134433.html

(0)

相关推荐

  • 酸菜素馅饺子都放啥,素酸菜粉条饺子馅怎么调

    技术酸菜素馅饺子都放啥,素酸菜粉条饺子馅怎么调酸菜在我们的饮食中是开胃小菜、下饭菜酸菜素馅饺子都放啥,也可以作为调味料来制作菜肴,酸菜配上粉条做成水饺,味道真的好极了。今天不做肉的饺子,就做素的“酸菜粉条饺子”,要多包一

    生活 2021年11月1日
  • Vue+ElementUI怎么处理超大表单

    技术Vue+ElementUI怎么处理超大表单Vue+ElementUI怎么处理超大表单,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。最近公

    攻略 2021年11月23日
  • java方法引用有什么用(java引用方式有哪些)

    技术Java中引用方法有哪些这篇文章主要介绍“Java中引用方法有哪些”,在日常操作中,相信很多人在Java中引用方法有哪些问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Java中引用方法

    攻略 2021年12月23日
  • 电脑手机互传文件,怎样通过电脑给手机发送文件

    技术电脑手机互传文件,怎样通过电脑给手机发送文件通过电脑给手机发送文件电脑手机互传文件,可以通过电脑QQ传到我到手机设备中,然后在从手机QQ下载到自己的手机就行了,具体的方法如下: 1、首先在电脑上的QQ页面点击【我的设

    生活 2021年10月24日
  • 拔苗助长告诉我们什么道理,揠苗助长告诉我们什么道理

    技术拔苗助长告诉我们什么道理,揠苗助长告诉我们什么道理揠苗助长的故事出自《孟子·公孙丑上》,揠,是拔起的意思。揠苗助长的意思就是把苗拔起,帮助其生长,比喻不管事物的发展规律,强求速成,反而把事情弄糟。天下不希望自己禾苗长

    生活 2021年10月26日
  • 如何在scrapy请求异常之后再设置代理IP

    技术如何在scrapy请求异常之后再设置代理IP如何在scrapy请求异常之后再设置代理IP,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。我们都知道 s

    攻略 2021年10月28日