主从数据库搭建

  • 改主机名

  • 配置网络

  • 配置yum源(下载mysql)

  • 写域名解析文件

  • 主从同步:(备份,负载(读))

  • 第一步:数据库的初始化,修改配置文件,定义server-id(所有节点),开启二进制日志功能binlog(主节点、主库)

  • 第二步:授权,复制权限(主库)

  • 第三步:从库连接主库

  • 第四步:查询从库状态

  • 第五步:验证主从同步的结果

改主机名

[root@server ~]# hostnamectl set-hostname mysql1
[root@server ~]# bash
[root@mysql1 ~]#
[root@client ~]# hostnamectl set-hostname mysql2
[root@client ~]# bash
[root@mysql2 ~]#

配置网络和yum源此处省略

防火墙

如果数据库访问不成功,则需要关闭防火墙才可进行访问;
命令为: systemctl stop firewalld (关闭防火墙并不安全)
查看防火墙状态命令: systemctl status firewalld
打开防火墙命令为: systemctl start firewalld
如果不想关闭防火墙服务,可使用firewall-cmd命令添加规则;

查看链接(https://www.cnblogs.com/zhengyan6/p/15602877.html) 关于firewall-cmd的使用

写域名解析文件

[root@mysql1 ~]# vi /etc/hosts
末行写入
192.168.100.10 mysql1
192.168.100.20 mysql2
保存退出
[root@mysql1 ~]# scp /etc/hosts 192.168.100.20:/etc #复制一份到MySQL2或者去mysql2自己编辑写入

下载mysql服务

[root@mysql1 ~]#yum install -y mariadb mariadb-server
[root@mysql2 ~]#yum install -y mariadb mariadb-server

重启并设置自启动

-两边都要重启(此处只写一边)
[root@mysql1 ~]systemctl start mariadb
[root@mysql1 ~]systemctl enable mariadb

安全配置向导

-两边都要配置(此处只写一边)
[root@mysql1 ~]# mysql_secure_installation #安全配置向导
点击查看代码
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!

总结来说就是:

回车 (下次进入需要输入设置的密码)

y

设置密码

检查密码

y

n

y

y

编辑配置文件

mysql1

[root@mysql1 ~]# vim /etc/my.cnf
末行写入
[mysqld]
log-bin = mysql-bin
server-id =10 #(mysql1 ip:192.168.100.10,mysql2 ip:192.168.100.20)

mysql2

[root@mysql2 ~]# vim /etc/my.cnf
末行写入
[mysqld]
server-id =20 #(mysql1 ip:192.168.100.10,mysql2 ip:192.168.100.20)

重启

[root@mysql1 ~]# systemctl restart mariadb
[root@mysql2 ~]# systemctl restart mariadb

进行主从同步的二三四步

mysql1

[root@mysql1 ~]# mysql -uroot -p11111  #进入mysql数据库 五个一为自己设置的密码
MariaDB [(none)]> grant all privileges on *.* to root@'%' identified by "11111";
#对所有远程root身份登录并对所有库和表赋予读写权限,通过密码为11111 MariaDB [(none)]> grant replication slave on *.* to 'user'@'mysql2' identified by '11111';
#此处mysql2需和域名解析主机名一致
#所有库和所有表对从数据库赋予复制权限,并通过user用户登录到mysql2,通过密码为11111

mysql2

[root@mysql2 ~]# mysql -uroot -p11111  #进入mysql数据库 五个一为自己设置的密码
MariaDB [(none)]> grant all privileges on *.* to root@'%' identified by "11111";
#同上
MariaDB [(none)]> change master to master_host='mysql1',master_user='user',master_password='11111';
#此处mysql1需和域名解析主机名一致
#改变主人为主数据库,用户为user,主库通过密码为11111 MariaDB [(none)]> start slave;
#开始为“从库”
MariaDB [(none)]> show slave status\G
#查询从库状态 Slave_IO_Running: Yes
Slave_SQL_Running: Yes 为yes即可

验证主从同步的结果

mysql1

MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
#原本就有的数据库 MariaDB [(none)]> create database test; #创建一个名为test的数据库
MariaDB [(none)]> use test; #使用test数据库
MariaDB [(none)]> create table company(id int not null primary key,name varchar(50),addr varchar(255));
#创建一个名为company的表并设置字段类型 MariaDB [test]> insert into company values(1,"facebook","usa"); #插入数据

mysql2

MariaDB [(none)]> show databases; #mysql2中查看数据库 可以看到test
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec) MariaDB [(none)]> use test; #使用test数据库 而且显示只可读
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A Database changed
MariaDB [test]> show tables; #查看表
+----------------+
| Tables_in_test |
+----------------+
| company |
+----------------+
1 row in set (0.00 sec) MariaDB [test]> select * from company; #显示表记录
+----+----------+------+
| id | name | addr |
+----+----------+------+
| 1 | facebook | usa |
+----+----------+------+
1 row in set (0.00 sec)

从库创建数据库不会同步到主库并且主库看不到

mysql2

MariaDB [(none)]> create database zzz;
Query OK, 1 row affected (0.00 sec) MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| zzz |
+--------------------+
5 rows in set (0.00 sec)

mysql1

MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)

Linux之主从数据库(1+X)的更多相关文章

  1. Linux搭建主从数据库服务器(主从复制)

    配置主机数据库: 1.克隆linux操作系统 2.修改Linux系统主机IP地址 主机IP:192.168.247.150 从机IP:192.168.247.151 3.通过xshell连接Maste ...

  2. Linux下MySQL数据库主从同步配置

    说明: 操作系统:CentOS 5.x 64位 MySQL数据库版本:mysql-5.5.35 MySQL主服务器:192.168.21.128 MySQL从服务器:192.168.21.129 准备 ...

  3. 【linux】【mysql】mysql主从数据库

    系统环境:Centos7 主:192.168.8.162 从:192.168.8.127 前提条件 a.关闭防火墙  systemctl stop firewalld 关闭防火墙开机自启 system ...

  4. Linux 主从数据库

    主从数据库 主数据库的内容同步传输到附属数据库 客户访问附属数据库 这样做保证了数据库的稳定性 需要两台虚拟机 两边个虚拟机都要操作 配置hosts文件 进入/etc/hosts追加输入 192.16 ...

  5. MySQL主从数据库同步延迟问题解决(转)

    最近在做MySQL主从数据库同步测试,发现了一些问题,其中主从同步延迟问题是其中之一,下面内容是从网上找到的一些讲解,记录下来以便自己学习: MySQL的主从同步是一个很成熟的架构,优点为:①在从服务 ...

  6. mysql 主从数据库设置方法

    1.主从数据库都需开启bin-log日志 2.在my.ini(windows)或my.cnf(linux)配置文件中添加 server-id = 1(主从配置 id 必须不同) 例子: [mysqld ...

  7. Yii2 主从 数据库

    配置方法 参考资料:http://www.linuxidc.com/Linux/2015-07/120134.htm 读写分离(Read/Write Splitting). 1.原理:让主数据库(ma ...

  8. mysql配置主从数据库

    1.目的 1.1 实现数据备份 1.2 项目访问时可以实现读写分离,提高访问和操作数据的速度<读写分离好处> 2.背景 这次的主从配置主要实现主库数据的改变可以实现同步到从库中: 此次试验 ...

  9. Mysql 主从数据库

    MYSQL主从数据库同步备份配置 一.准备 用两台服务器做测试: Master Server: 172.16.0.180/Linux/MYSQL 5.1.41 Slave Server: 172.16 ...

随机推荐

  1. 接口偶尔超时,竟又是JVM停顿的锅!

    原创:扣钉日记(微信公众号ID:codelogs),欢迎分享,转载请保留出处. 简介 继上次我们JVM停顿十几秒的问题解决后,我们系统终于稳定了,再也不会无故重启了! 这是之前的文章:耗时几个月,终于 ...

  2. ACM-由数据范围反推算法复杂度以及算法内容

    一般ACM或者笔试题的时间限制是1秒或2秒. 在这种情况下,C++代码中的操作次数控制在 \(10^7\) 为最佳. 下面给出在不同数据范围下,代码的时间复杂度和算法该如何选择: 数据范围 算法选择 ...

  3. 【RPA之家BluePrism手把手教程】2.3 多重计算

    2.3.1 添加除法运算计算框 2.3.2 设置除法运算计算属性 2.3.3 程序运行前初始值 2.3.4 程序运行后结果 使用多重计算框实现以上操作 2.3.5 添加多重选择框 2.3.6 设置多重 ...

  4. 我不就是吃点肉,应该没事吧——爬取一座城市里的烤肉店数据(附完整Python爬虫代码)

    写在前面的一点屁话: 对于肉食主义者,吃肉简直幸福感爆棚!特别是烤肉,看着一块块肉慢慢变熟,听着烤盘上"滋滋"的声响,这种期待感是任何其他食物都无法带来的.如果说甜点是" ...

  5. 人人都能学会的 Python 多线程指南~

    大家好鸭!有没有想我~(https://jq.qq.com/?_wv=1027&k=rX9CWKg4) 在 Python 中,多线程最常见的一个场景就是爬虫,例如这样一个需求,有多个结构一样的 ...

  6. NC200190 矩阵消除游戏

    NC200190 矩阵消除游戏 题目 题目描述 牛妹在玩一个名为矩阵消除的游戏,矩阵的大小是 \({n}\) 行 \({m}\) 列,第 \({i}\) 行第 \({j}\) 列的单元格的权值为 \( ...

  7. Android 12(S) 图像显示系统 - drm_hwcomposer 简析(上)

    必读: Android 12(S) 图像显示系统 - 开篇 前言 Android源码中有包含drm_hwcomposer:/external/drm_hwcomposer/ drm_hwcompose ...

  8. Tapdata Cloud 2.1.2 来啦:大波细节已就绪!字段类型可批量修改、支持微信扫码登录、新增支持 Vika 为目标

    Tapdata Cloud cloud.tapdata.net 让数据实时可用 Tapdata Cloud 是国内首家异构数据库实时同步云平台,目前支持 Oracle.MySQL.PG.SQL Ser ...

  9. 业务可视化-让你的流程图"Run"起来(2.问题与改进)

    前言 首先,感谢大家对上一篇文章[业务可视化-让你的流程图"Run"起来]的支持. 分享一下近期我对这个项目的一些改进. 问题&改进 问题1: 流程运行开始后,异步执行,无 ...

  10. 打印三角形及debug用法

    package www.nihao; public class demo01 { public static void main(String[] args) { //打印三角形 5行 for(int ...