1+X中级B卷:主从数据库管理

来自CloudWiki
Wang讨论 | 贡献2020年11月21日 (六) 07:09的版本
跳转至: 导航搜索

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

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

这里我们设置主数据库为wanghaobin1,从数据库为wanghaobin2

> [root@localhost /]# hostnamectl set-hostname wanghaobin1 (修改主机名)

> [root@localhost /]# bash (刷新shell命令行)

> [root@wanghaobin1 /]# su - (重新登录)

> [root@wanghaobin1 ~]# hostnamectl (查看主机信息)

③关闭防火墙和SELinux服务

> [root@wanghaobin1 ~]# setenforce 0 (设置关闭 1开启0关闭)

> [root@wanghaobin1 ~]# systemctl disable firewalld (永久关闭防火墙)

> [root@wanghaobin1 ~]# 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和主机名


112212.png


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

> [root@wanghaobin1 /]# yum install -y mariadb mariadb-server(安装数据库以及服务)

> [root@wanghaobin1 /]# systemctl start mariadb (启动数据库)

> [root@wanghaobin1 /]# systemctl enable mariadb (设置开机自启)

> Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service. ⑥初始化数据库(主从数据库同理)

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

步骤参考以下↓

112312.png


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

> [root@wanghaobin1 /]# cat /etc/my.cnf (修改这个文件 添加以下这三项)

log_bin=mysql-bin (记录操作日志)

binlog_ignore_db=mysql (不同步mysql系统数据库)

server_id=3 (数据库集群中每个节点ID都要不同)

114213.png


⑧主数据配置

在数据库进行如下两条命令 grant all privileges on *.* to root@’%'identified by “数据库密码”;

grant replication slave on . to ‘wanghaobin’@‘wanghaobin2’ identified by “数据库密码”;

(wanghaobin为主数据库节点上新建的用户)

> [root@wanghaobin1 /]# systemctl restart mariadb (重启数据库)

> [root@wanghaobin1 /]# mysql -uroot -p (输入密码)

> MariaDB [(none)]> grant all privileges on *.* to root@'%'identified by "数据库密码";

> Query OK, 0 rows affected (0.00 sec)

> MariaDB [(none)]> grant replication slave on *.* to 'wanghaobin'@'wanghaobin2' identified by "数据库密码";

> Query OK, 0 rows affected (0.00 sec)

⑨从数据库配置

在数据库进行如下命令

MariaDB [(none)]> change master to

  -> master_host='wanghaobin1',master_user='wanghaobin',master_password='数据库密码';

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


112111.png

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