节点规划
IP | 主机名 | 节点 |
100.111.8.88 | db1 | MariaDB数据库集群主节点 |
100.111.8.131 | db2 | MariaDB数据库集群从节点 |
基础环境配置
修改主机名:
[root@test-1 ~]# hostnamectl set-hostname db1 #修改主机名 [root@test-1 ~]# bash #刷新 [root@db1 ~]# [root@test-2 ]# hostnamectl set-hostname db2 [root@test-2 ]# bash [root@db2 ]#
关闭防火墙、selinux
db1、db2两节点:
[root@db1 ~]# systemctl stop firewalld #关闭防火墙 [root@db1 ~]# setenforce 0 #临时关闭selinux,永久关闭selinux修改配置文件/etc/selinux/config [root@db2 ~]# systemctl stop firewalld [root@db2 ~]# setenforce 0
配置hosts
db1、db2两节点:配置一样
[root@db1 ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 10.43.8.88 db1 10.43.8.131 db2
配置yum源
db1、db2两节点:配置一样
[root@db1 ~]# cat /etc/yum.repos.d/local.repo [centos] name=centos baseurl=http://10.120.11.15/centos/ gpgcheck=0 enabled=1
安装mariadb数据库并启动
db1、db2两节点:
[root@db1 ~]# yum install -y mariadb mariadb-server Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile Resolving Dependencies --> Running transaction check ---> Package mariadb.x86_64 1:5.5.56-2.el7 will be installed ---> Package mariadb-server.x86_64 1:5.5.56-2.el7 will be installed --> Processing Dependency: perl-DBI for package: 1:mariadb-server-5.5.56-2.el7.x86_64 .... .... [root@db1 ~]# systemctl start mariadb [root@db1 ~]# systemctl enable mariadb Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
初始化数据库
db1、db2两节点:
一直按 y 就行
[root@db1 ~]# mysql_secure_installation #初始化数据库 NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY! .... .... Set root password? [Y/n] y New password: #输入数据库root密码 Re-enter new password: #重复输入密码 Password updated successfully! Reloading privilege tables.. ... Success! .... .... 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] y #是否开启远程登陆数据库 ... Success! 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! .... .... Reload privilege tables now? [Y/n] y #是否重新加载特权表 ... Success!
修改配置文件并重启
db1、db2两节点:
[root@db1 ~]# cat /etc/my.cnf.d/server.cnf ... ... [mysqld] log_bin = mysql-bin #记录操作日志 binlog_ignore_db = mysql #不同步mysql系统数据库 server_id = 88 #数据库集群中的每个节点id都要不同,一般使用IP地址的最后段的数字,例如172.30.11.12,server_id就写88 ... ... [root@db1 ~]# systemctl restart mariadb [root@db2 ~]# cat /etc/my.cnf.d/server.cnf ... ... [mysqld] log_bin = mysql-bin binlog_ignore_db = mysql server_id = 131 ... ... [root@db2 ~]# systemctl restart mariadb
数据库授权
db1:
[root@db1 ~]# mysql -uroot -p #root用户登录数据库 Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 2 Server version: 5.5.56-MariaDB MariaDB Server Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> MariaDB [(none)]> grant all privileges on *.* to root@'%' identified by "000000"; #赋予所以远程访问数据库权限 Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> grant replication slave on *.* to "user"@'db2' identified by "000000"; #赋予从节点同步主节点数据库的权限 Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> Ctrl-C -- exit! Aborted [root@db1 ~]#
db2:
[root@db2 ~]# mysql -uroot -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 2 Server version: 5.5.56-MariaDB MariaDB Server Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> MariaDB [(none)]> change master to master_host='db1',master_user='user',master_password='000000'; #配置从节点连接主节点 Query OK, 0 rows affected (0.14 sec) MariaDB [(none)]> start slave; #开启从节点服务 Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> show slave status\G #检查Slave_IO_Running、Slave_SQL_Running字段是否为Yes,如果是那么主从数据库搭建成功 *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: db1 Master_User: user Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 526 Relay_Log_File: mariadb-relay-bin.000002 Relay_Log_Pos: 810 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes #检查字段是否为Yes Slave_SQL_Running: Yes #检查字段是否为Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 526 Relay_Log_Space: 1106 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 88 1 row in set (0.00 sec) MariaDB [(none)]>
至此mariadb主从数据库搭建完毕!!!!
总结
- 配置环境:修改主机名
添加hosts映射
配置yum源
关闭防火墙
selinux - 安装mariadb:启动mariadb
初始化mariadb
修改配置文件/etc/my.cnf.d/server.cnf
重启mariadb - 登录授权:开启root用户远程访问所有数据库
开启赋予从节点同步主节点数据库的权限
配置从节点连接主节点
开启从节点服务
检查Slave_IO_Running、Slave_SQL_Running字段状态