主从数据库部署

主从数据库部署

主从数据库概念地址:
首先,新建虚拟机,我们默认是使用CentOS-7-x86_64-DVD-1511的镜像
镜像地址:
链接:https://pan.baidu.com/s/1Myl_GXnUg7t3OR01mCuMrQ
提取码:1511

①安装两台虚拟机,配置ip,yum源

安装虚拟机,配置ip,配置yum源地址教程

②修改主机名(主从数据库同理)

这里我们设置主数据库为mysql0,从数据库为mysql1

> [root@localhost /]# hostnamectl set-hostname mysql0    (修改主机名)
> [root@localhost /]# bash  (刷新shell命令行)
> [root@mysql0 /]# su -    (重新登录) 
> [root@mysql0 ~]# hostnamectl  (查看主机信息)

主从数据库部署_第1张图片

③关闭防火墙和SELinux服务

防火墙和SELinux开启关闭教程

> [root@mysql0 ~]# setenforce 0   (设置关闭   1开启0关闭) 
> [root@mysql0 ~]# systemctl stop firewalld    (关闭防火墙) 
> [root@mysql0 ~]# getenforce     (查看进程  Enforcing开启Permissive关闭) 
> Permissive

④配置/etc/hosts文件(主从数据库同理)

文件最后加上主从数据库ip和主机名
主从数据库部署_第2张图片

⑤安装数据库和数据库服务(主从数据库同理)


> [root@mysql0 /]# yum install -y mariadb mariadb-server(安装数据库以及服务)
> [root@mysql0 /]# systemctl start mariadb    (启动数据库) 
> [root@mysql0 /]# systemctl enable mariadb   (设置开机自启) 
> Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service. 

⑥初始化数据库(主从数据库同理)

> [root@mysql1 ~]# mysql_secure_installation  (格式化数据库)

步骤参考以下↓

[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.
为了登录MariaDB以保护它,我们需要根用户的密码。如果你刚刚安装了MariaDB
您尚未设置根密码,密码将为空,所以你应该在这里按回车键。


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.
默认情况下,MariaDB安装有一个匿名用户,允许任何人无需创建用户帐户即可登录MariaDB
他们。这仅用于测试和安装做得更流畅一点。你应该在搬进生产环境。


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.
通常,只允许root从“localhost”连接。这个确保有人无法从网络中猜出根密码。


Disallow root login remotely? [Y/n] n      (不仅仅允许root登录)
 ... 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.
默认情况下,MariaDB附带了一个名为“test”的数据库,任何人都可以进入。这也仅用于测试,应将其移除在进入生产环境之前。

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!

⑦修改数据库配置文件(主从数据库同理)

> [root@mysql0 /]# cat /etc/my.cnf    (修改这个文件 添加以下这三项)
log_bin=mysql-bin          (记录操作日志)
binlog_ignore_db=mysql     (不同步mysql系统数据库)
server_id=111              (数据库集群中每个节点ID都要不同)

主从数据库部署_第3张图片

⑧主数据配置

在数据库进行如下两条命令

grant all privileges on *.* to root@'%'identified by "SICT";
grant replication slave on *.* to 'xiaobai'@'mysql1' identified by "SICT";

(SICT为数据库密码,xiaobai为主数据库节点上新建的用户)

> [root@mysql0 /]# systemctl restart mariadb    (重启数据库) 
> [root@mysql0 /]# mysql -uroot -p      (输入密码) 
> MariaDB [(none)]> grant all privileges on *.* to root@'%'identified by "SICT"; 
> Query OK, 0 rows affected (0.00 sec)
> 
> MariaDB [(none)]> grant replication slave on *.* to 'xiaobai'@'mysql1' identified by "SICT";
>  Query OK, 0 rows affected (0.00 sec)

⑨从数据库配置

在数据库进行如下命令

MariaDB [(none)]> change master to master_host='mysql0',master_user='xiaobai',master_password='SICT';
    (这里的mysql0是我的主数据库,xiaobai是主数据库用户,SICT我的数据库密码)
Query OK, 0 rows affected (0.36 sec)

MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)

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

以下两项均为yes则表示主从数据库部署成功,在主数据库进行的修改,会自动同步到从数据库。
主从数据库部署_第4张图片

你可能感兴趣的:(1+x认证,数据库,linux,centos,mysql)