[root@localhost ~]# hostnamectl set-hostname mysql1
[root@localhost ~]# bash
[root@localhost ~]# hostnamectl set-hostname mysql2
[root@localhost ~]# bash
3.关闭防火墙及SELinux服务
[root@localhost ~]# setenforce 0
[root@localhost ~]# systemctl stop firewalld
四、部署主从数据库(mysql1、mysql2节点)
1.基础配置
配置hosts文件
配置yum源
将镜像挂载到/opt/centos目录下
配置本地YUM源文件,先将/etc/yum.repos.d/下的文件先移走,然后创建local.repo文件
[centos7]
name=centos7
baseurl=file:///opt/centos
gpgcheck=0
enabled=1
安装数据库服务
[root@localhost ~]# yum install -y mariadb mariadb-server
启动数据库服务并设置开机自启
2.初始化数据库并配置主从服务
(1)初始化数据库
[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主节点
[root@mysql1 ~]# vi /etc/my.cnf #添加如下内容
log_bin = mysql-bin #记录操作日志
binlog_ignore_db = mysql #不同步mysql系统数据库
server_id = 22 #数据库集群中的每个节点id都要不同,一般使用IP地址的最后段的数字,例如172.16.51.22,server_id就写22
重启数据库服务,进入数据库,在mysql1节点授权在任何客户端机器上可以以root用户登录到数据库,然后在主节点上创建一个user用户连接节点mysql2,并赋予从节点同步主节点数据库的权限
(3)配置mysql2从节点
[root@mysql1 ~]# vi /etc/my.cnf #添加如下内容
log_bin = mysql-bin #记录操作日志
binlog_ignore_db = mysql #不同步mysql系统数据库
server_id = 23 #数据库集群中的每个节点id都要不同,一般使用IP地址的最后段的数字,例如172.16.51.23,server_id就写23
在从节点mysql2上登录MariaDB数据库,配置从节点连接主节点的连接信息。master_host为主节点主机名mysql1,master_user为上一步中创建的用户user
(4)开启从节点服务
(Slave_IO_Running和Slave_SQL_Running的状态都为YES)
MariaDB [(none)]> start slave;
MariaDB [(none)]> show slave status\G
3.验证数据库主从服务
(1)主节点mysql1中创建库test
在库test中创建表company,插入表数据,创建完成后,查看表company数据
(2)从节点验证复制功能
登录mysql2节点的数据库,查看数据库列表。找到test数据库,查询表,并查询内容验证从数据库的复制功能