主从数据库概念地址:
首先,新建虚拟机,我们默认是使用CentOS-7-x86_64-DVD-1511的镜像
镜像地址:
链接:https://pan.baidu.com/s/1Myl_GXnUg7t3OR01mCuMrQ
提取码:1511
安装虚拟机,配置ip,配置yum源地址教程
这里我们设置主数据库为mysql0,从数据库为mysql1
> [root@localhost /]# hostnamectl set-hostname mysql0 (修改主机名)
> [root@localhost /]# bash (刷新shell命令行)
> [root@mysql0 /]# su - (重新登录)
> [root@mysql0 ~]# hostnamectl (查看主机信息)
防火墙和SELinux开启关闭教程
> [root@mysql0 ~]# setenforce 0 (设置关闭 1开启0关闭)
> [root@mysql0 ~]# systemctl stop firewalld (关闭防火墙)
> [root@mysql0 ~]# getenforce (查看进程 Enforcing开启Permissive关闭)
> Permissive
> [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都要不同)
在数据库进行如下两条命令
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