云计算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 \|

相关推荐
SelectDB1 分钟前
Apache Doris 4.0 AI 能力揭秘(一):AI 函数之 LLM 函数介绍
数据库·人工智能·数据分析
我是哈哈hh15 分钟前
【MySQL】在UBuntu环境安装以及免密码登录入门
linux·数据库·mysql·ubuntu
nshkfhwr32 分钟前
什么是微服务
微服务·云原生·架构·云计算·集群
喪彪1 小时前
MySQL新手教学
数据库·mysql·adb
丘大梨3 小时前
QT 基础聊天应用项目文档
运维·数据库·系统架构
HMBBLOVEPDX4 小时前
MySQL的多版本并发控制(MVCC):
数据库·mysql·mvcc
.用户昵称已存在.4 小时前
MongoDB 从入门到精通:安装配置与基础操作指令详解
数据库·mongodb
ClouGence5 小时前
CloudDM 新增支持 GaussDB 与 openGauss:国产数据库管理更高效
数据库·sql·ci/cd
sukalot5 小时前
window显示驱动开发—在混合系统中使用跨适配器资源
数据库·驱动开发·音视频