1+X中级A卷:主从数据库管理
创建两个虚拟机
①IP:192.168.128.111 主机名:mysql0 节点:主数据库节点
②IP:192.168.128.122 主机名:mysql1 节点:从数据库节点
(主从数据库部署)
首先,新建虚拟机,我们默认是使用CentOS-7-x86_64-DVD-1511的镜像 镜像地址: 链接:https://pan.baidu.com/s/1Myl_GXnUg7t3OR01mCuMrQ 提取码:1511
①安装两台虚拟机,配置ip,yum源
主数据库节点配置IP: vi /etc/sysconfig/network-scripts/ifcfg-eno16777736
TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
NAME=eno16777736
UUID=0b3ebb09-1b40-41b2-8a20-9b090320e810
DEVICE=eno16777736
ONBOOT=yes
IPADDR=192.168.128.111
NETMASK=255.255.255.0
GATEWAY=192.168.200.2
配置yum源:
> mkdir /opt/centos
> rm -rf /opt/centos
> mkdir /opt/centos
> mount /dev/cdrom /opt/centos
> mv /etc/yum.repos.d/* /media/
> vi /etc/yum.repos.d/local.repo
> cat /etc/yum.repos.d/local.repo
[centos7]
name=centos7
baseurl=file:///opt/centos
gpgcheck=0
enabled=1
从数据库节点同理
②修改主机名(主从数据库同理)
这里我们设置主数据库为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 disable firewalld (永久关闭防火墙)
> [root@mysql0 ~]# getenforce (查看进程 Enforcing开启Permissive关闭)
> Permissive
开机关闭SELinux编辑/etc/selinux/config文件将SELINUX的值设置为disabled下次开机SELinux就不会启动了。
④配置/etc/hosts文件(主从数据库同理) (hosts文件是Linux系统上一个负责ip地址与域名快速解析的文件,以ascii格式保存在/etc/目录下。hosts文件包含了ip地址与主机名之间的映射,还包括主机的别名。在没有域名解析服务器的情况下,系统上的所有网络程序都通过查询该文件来解析对应于某个主机名的ip地址,否则就需要使用dns服务程序来解决。通过可以将常用的域名和ip地址映射加入到hosts文件中,实现快速方便的访问。)
文件最后加上主从数据库ip和主机名
⑤安装数据库和数据库服务(主从数据库同理)
> [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 (格式化数据库)
/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.
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.
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!
⑦修改数据库配置文件(主从数据库同理)
> [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';
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
Slave_IO_Running: Yes
Slave_SQL_Running: Yes 这两项均为yes则表示主从数据库部署成功,在主数据库进行的修改,会自动同步到从数据库。