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

两台虚拟机一样操作到这一步,然后同样启动数据库服务

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的结束数字

进入数据库,使用数据库命令语句创建一个叫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)

这样主数据库就配置好了

配置从数据库的节点

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 |

相关推荐
Joy T5 小时前
MySQL Outbox、Redis Streams 与 LangGraph——如何构建可靠的异步 AI Agent
数据库·redis·mysql·任务队列·redis streams·mysql outbox
FungLeo5 小时前
FengCMS · PHP+MySQL 升级实录· 回归测试从 69 项到 478 项:HTTP/数据库回归套件怎么搭
数据库·mysql·php·回归测试·fengcms
captain3765 小时前
Redis简单使用
数据库·redis·缓存
代码不停6 小时前
Redis基本数据类型
数据库·redis
aifannihao6 小时前
ON CONFLICT 冲突后返回空:sqlite3 实测 get-or-create
数据库·python·postgresql·sqlite3·标准库
得物技术6 小时前
Computer Use与Browser Use:揭秘Agent如何操作电脑和浏览器|得物技术
数据库·github·ai编程
小小程序员.¥7 小时前
mysql备份以及数据恢复实战
数据库·mysql
AllData公司负责人7 小时前
实战一:AllData大模型与AI数据底座-数据传输平台-DBSyncer,搭建 File 到 Doris 高效数据同步链路,批量文件离线入仓
数据库·人工智能·分布式·大模型·数据传输·数据同步·连接器
黄俊懿8 小时前
【架构师从入门到进阶】第七章:缓存和存储的优化思路——第六节:数据库更新和缓存更新的组合方案
数据库·缓存·性能优化·架构·系统架构
小马过河R21 小时前
真实场景:用户自建定时任务与推送提醒Agent-架构设计与可靠性实践
数据库·人工智能·agent·vibe coding·ai coding