MySQL主从数据库

基础准备

mysql1 IP:192.168.223.4 主数据库
mysql2 IP:192.168.223.3 从数据库

基础环境安装

修改主机名
使用xshell连接两台虚拟机

mysql1

[root@localhost ~]# hostname mysql1
[root@localhost ~]# bash
[root@mysql1 ~]# hostnamectl

mysql2

[root@localhost ~]# hostname mysql2
[root@localhost ~]# bash
[root@mysql1 ~]# hostnamectl

关闭防火墙及SELinux服务

mysql1

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

mysql2

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

配置hosts文件

mysql1

[root@mysql1 ~]# vi /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.223.4  mysql1
192.168.223.3  mysql2

mysql2

[root@mysql2 ~]# vi /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.223.4  mysql1
192.168.223.3  mysql2

配置yum源并安装数据库服务

挂载本地yum源

mysql1

[root@mysql1 ~]# mount /dev/sr0 /opt/centos
[root@mysql1 ~]# vi /etc/yum.repos.d/local.repo 
[root@mysql1 ~]# yum repolist all
[root@mysql1 ~]# yum clean all
[root@mysql1 ~]# yum install -y mariadb-server

mysql2

[root@mysql2 ~]# mount /dev/sr0 /opt/centos
[root@mysql2 ~]# vi /etc/yum.repos.d/local.repo 
[root@mysql2 ~]# yum repolist all
[root@mysql2 ~]# yum clean all
[root@mysql2 ~]# yum install -y mariadb-server

启动数据库服务并设置开机自启动

mysql1

[root@mysql1 ~]# systemctl start mariadb
[root@mysql1 ~]# systemctl enable mariadb

mysql2

[root@mysql2 ~]# systemctl start mariadb
[root@mysql2 ~]# systemctl enable mariadb

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

初始化数据库

配置密码自行设置,这里为000000

mysql1

[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密码000000
Re-enter new password:                          #再次输入密码000000
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!

mysql2

[root@mysql2 ~]# 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密码000000
Re-enter new password:                          #再次输入密码000000
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!

配置mysql1主节点

修改配置文件,在配置文件/etc/my.cnf中的[mysqld]增添如下内容:

[root@mysql1 ~]# vi /etc/my.cnf
[mysqld]
log_bin = mysql-bin                       #记录操作日志
binlog_ignore_db = mysql                  #不同步mysql系统数据库
server_id = 4                           #数据库集群中的每个节点id都要不同,一般使用IP地址的最后段的数字,例如192.168.200.4,server_id就写4

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

[root@mysql1 ~]# systemctl restart mariadb
[root@mysql1 ~]# mysql -uroot -p000000

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

MariaDB [(none)]> grant all privileges  on *.* to root@'%' identified by "000000";
MariaDB [(none)]> grant replication slave on *.* to 'user'@'mysql2' identified by '000000';

配置mysql2从节点

修改配置文件,在配置文件/etc/my.cnf中的[mysqld]增添如下内容:

[root@mysql2 ~]# vi /etc/my.cnf
[mysqld]
log_bin = mysql-bin                       #记录操作日志
binlog_ignore_db = mysql                  #不同步mysql系统数据库
server_id = 3                           #数据库集群中的每个节点id都要不同,一般使用IP地址的最后段的数字,例如192.168.200.3,server_id就写3

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

[root@mysql2 ~]# systemctl restart mariadb
[root@mysql2 ~]# mysql -uroot -p000000
MariaDB [(none)]> change master to master_host='mysql1',master_user='user',master_password='000000';

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

MariaDB [(none)]> start slave;
MariaDB [(none)]> show slave status\G

验证数据库主从服务

主节点创建数据库

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

MariaDB [(none)]> create database test;
MariaDB [(none)]> use test;
MariaDB [test]> create table company(id int not null primary key,name varchar(50),addr varchar(255));
MariaDB [test]> insert into company values(1,"alibaba","china");
MariaDB [test]> select * from company;
+----+---------+-------+
| id | name    | addr  |
+----+---------+-------+
|  1 | alibaba | china |
+----+---------+-------+
1 row in set (0.00 sec)

从节点验证复制功能

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

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

MariaDB [(none)]> use test;
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)

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值