云计算2主从数据库

设置主从数据库的目的是将数据库1和数据库2分别建在两个虚拟机上,并实现数据互通访问

首先准备两个虚拟机,这里示例ip分别为:

192.168.200.10;192.168.200.20

修改主机名,一个是mysql1,一个是mysql2,(可改可不改,方便区别而已,我后续一个是mysql1一个是samba),

然后关闭SELinux和防火墙,并修改hosts的配置文件,下图增加的最后两行是自己虚拟机的ip和主机名,无需和我一样

配置yum源,安装数据库服务,注意如何配置yum源 ,不再重复细节,可以参考云计算1中ftp安装篇,现在设置里检查DVD光盘文件是否连接,再进行以下操作

root@localhost\~\]#mount /dev/cdrom /opt/centos 将cd设备挂载到/opt/centos,不存在的话可以先创建 \[root@localhost\~\]#mkdir -p /opt/centos \[root@localhost\~\]#cd /opt \[root@localhost\~\]#ll 查看是否创建成功 total 636则挂载成功 \[root@localhost\~\]#mv /etc/yum.repos.d/\* /media/ 移除repos.d下的文件 \[root@localhost\~\]#vi /etc/yum.repos.d/local.repo 创建local文件 \[centos7

name=cantos7

baseurl=file:///opt/centos

gpgcheck=0

enabled=1

root@localhost\~\]#cat /etc/yum.repos.d/local.repo 查看 进行数据库服务的安装 \[root@mysql1 \~\]# yum install -y mariadb mariadb-server ![在这里插入图片描述](https://file.jishuzhan.net/article/1768822456354607105/923adb21b45e9dac4fd346696c37c5b4.webp) 两台虚拟机一样操作到这一步,然后同样启动数据库服务 \[root@mysql1 /\]# systemctl start mariadb \[root@mysql1 /\]# systemctl enable mariadb 初始化数据库并进行配置 \[root@mysql1 /\]# mysql_secure_installation 接下来按照提示,set root password?,y,root,000000,remove anonymous users?,y,Disallow root login remotely? ,n remove test database and access to it?,y,Reload privilege tables now? y 配置mysql1主节点,注意server_id是ip的结束数字 ![在这里插入图片描述](https://file.jishuzhan.net/article/1768822456354607105/128ffdea6226ecd55e770e43ec8846d6.webp) 进入数据库,使用数据库命令语句创建一个叫test的数据库,在test下创建叫company的表格 \[root@mysql1 /\]# systemctl restart mariadb \[root@mysql1 /\]# mysql -uroot -p000000 Welcome to the MariaDB monitor. Commands end with ; or \\g. Your MariaDB connection id is 2 Server version: 5.5.44-MariaDB-log MariaDB Server Copyright © 2000, 2015, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\\h' for help. Type '\\c' to clear the current input statement. 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'@'samba' identified by'000000'; Query OK, 0 rows affected (0.00 sec) MariaDB \[(none)\]\> create database test; Query OK, 1 row affected (0.00 sec) MariaDB \[(none)\]\> use test; Database changed MariaDB \[test\]\> create table company(id int not null primary key,name varchar(50),addr varchar(255)); Query OK, 0 rows affected (0.01 sec) MariaDB \[test\]\> insert into company values(1,'alibaba','china'); Query OK, 1 row affected (0.00 sec) MariaDB \[test\]\> select\*from company; ±---±--------±------+ \| id \| name \| addr \| ±---±--------±------+ \| 1 \| alibaba \| china \| ±---±--------±------+ 1 row in set (0.00 sec) 这样主数据库就配置好了 配置从数据库的节点 ![在这里插入图片描述](https://file.jishuzhan.net/article/1768822456354607105/770b713fc8971c83955c9bb05cd4627a.webp) \[root@samba \~\]# systemctl restart mariadb \[root@samba \~\]# mysql -uroot -p000000 MariaDB \[(none)\]\> change master to master_host='mysql1',master_user='user',master_password='000000'; Query OK, 0 rows affected (0.00 sec) MariaDB \[(none)\]\> start slave; Query OK, 0 rows affected (0.00 sec) MariaDB \[(none)\]\> show slave status \\G \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* 1. row \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* Slave_IO_State: Waiting for master to send event Master_Host: mysql1 Master_User: user Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 527 Relay_Log_File: mariadb-relay-bin.000002 Relay_Log_Pos: 811 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes 配置成功,开始验证主从数据库的关联功能,试试能否在虚拟机2也就是从数据库访问到刚才在主数据库建立的表格信息 MariaDB \[(none)\]\> show databases; ±-------------------+ \| Database \| ±-------------------+ \| information_schema \| \| mysql \| \| performance_schema \| \| test \| ±-------------------+ 4 rows in set (0.00 sec) MariaDB \[(none)\]\> use 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 \| alibaba \| china \|

相关推荐
敖云岚2 小时前
【Redis】分布式锁的介绍与演进之路
数据库·redis·分布式
LUCIAZZZ3 小时前
HikariCP数据库连接池原理解析
java·jvm·数据库·spring·springboot·线程池·连接池
我在北京coding3 小时前
300道GaussDB(WMS)题目及答案。
数据库·gaussdb
小Tomkk3 小时前
阿里云 RDS mysql 5.7 怎么 添加白名单 并链接数据库
数据库·mysql·阿里云
明月醉窗台4 小时前
qt使用笔记二:main.cpp详解
数据库·笔记·qt
沉到海底去吧Go5 小时前
【图片自动识别改名】识别图片中的文字并批量改名的工具,根据文字对图片批量改名,基于QT和腾讯OCR识别的实现方案
数据库·qt·ocr·图片识别自动改名·图片区域识别改名·pdf识别改名
老纪的技术唠嗑局5 小时前
重剑无锋,大巧不工 —— OceanBase 中的 Nest Loop Join 使用技巧分享
数据库·sql
未来之窗软件服务5 小时前
JAVASCRIPT 前端数据库-V6--仙盟数据库架构-—-—仙盟创梦IDE
数据库·数据库架构·仙盟创梦ide·东方仙盟·东方仙盟数据库
Johny_Zhao5 小时前
2025年6月Docker镜像加速失效终极解决方案
linux·网络·网络安全·docker·信息安全·kubernetes·云计算·containerd·yum源·系统运维
一只爱撸猫的程序猿7 小时前
构建一个简单的智能文档问答系统实例
数据库·spring boot·aigc