云计算第二阶段---DBA Day05-DAY07

DBA Day05

这周的内容涉及到的是各类数据库的服务配置与数据的备份与恢复操作.

环境准备:

设置 ip 地址 和主机名

安装 mysql,mysql-server --->启动

bash 复制代码
yum  -y install  mysql  mysql-server  #装mysql环境包
systemclt  start  mysqld        #启动服务
exit                             #退出环境

这是新开的虚拟机,和上节课的mysql50虚拟机联动.


一.备份策略

备份系统三要素:

物理备份与恢复

传统的备份方式,用u盘,sd卡,光碟备份存储数据.

通常是在机器上进行的物理盘存储备份操作.能备份多少数据,取绝于你的硬件设备的容量情况

mysql50备份数据

cs 复制代码
[root@mysql50 ~]# systemctl  stop  mysqld
[root@mysql50 ~]# mkdir /bakdir  #创建备份目录
[root@mysql50 ~]# cp -r /var/lib/mysql /bakdir/mysql.bak  #拷贝数据库目录
[root@mysql50 ~]# cd /var/lib/mysql #进入数据库目录
[root@mysql50 mysql]# tar -zcf /bakdir/mysql.tar.gz  ./*  #打包压缩数据源文件
[root@mysql50 mysql]# ls /bakdir/                          #查看备份文件
mysql.bak  mysql.tar.gz

把备份文件拷贝给mysql51

cs 复制代码
[root@mysql50 ~]# scp -r /bakdir/mysql.bak [email protected]:/root/
[root@mysql50 ~]# scp /bakdir/mysql.tar.gz [email protected]:/root/

在MySQL51主机恢复数据

cs 复制代码
[root@mysql51 ~]# systemctl  stop  mysqld  停止服务
[root@mysql51 ~]# rm  -rf  /var/lib/mysql/* 清空数据库目录
[root@mysql51 ~]# tar -xf /root/mysql.tar.gz  -C /var/lib/mysql/  释放压缩包
[root@mysql51 ~]# chown -R mysql:mysql /var/lib/mysql 修改所有者和组用户
[root@mysql51 ~]# systemctl  start mysqld  启动服务
[root@mysql51 ~]# mysql -uroot -p123  连接服务查看数据
 mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| GAMEDB             |
| db1                |
| home               |
| information_schema |
| mysql              |
| performance_schema |
| studb              |
| sys                |
| tarena             |
| 学生库             |
+--------------------+
10 rows in set (0.00 sec)

也可使用cp拷贝的备份文件恢复数据

cs 复制代码
[root@mysql51 ~]# systemctl  stop  mysqld
[root@mysql51 ~]# rm -rf /var/lib/mysql/*
[root@mysql51 ~]# cp -r /bakdir/mysql.bak/* /var/lib/mysql/
[root@mysql51 ~]# chown  -R mysql:mysql /var/lib/mysql
[root@mysql51 ~]# systemctl  start mysqld
[root@mysql51 ~]# mysql -uroot -p123

mysqldump逻辑备份恢复

**注意:**备份好的 mysql库和表 是 .sql格式

//备份1张表

cs 复制代码
[root@mysql50 ~]# mysqldump -uroot -p123  tarena salary > /bakdir/tarena_salary.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.

/备份多张表

cs 复制代码
root@mysql50 ~]# mysqldump -uroot -pN123  tarena employees departments > /bakdir/tarena_employees_deparments.sql

//备份1个库

cs 复制代码
[root@mysql50 ~]# mysqldump -uroot -p123 -B tarena > /bakdir/tarena.sql 

//备份多个库

**注意:**备份1个或者多个数据库 加 -B 选项

cs 复制代码
[root@mysql50 ~]# mysqldump -uroot -p123 -B studb db1 > /bakdir/studb_db1.sql 

//备份所有库

**注意:**备份所有数据库 加 -A 选项

cs 复制代码
[root@mysql50 ~]# mysqldump -uroot -p123  -A  > /bakdir/allbak.sql 

查看并备份(mysql50--->mysql51)

cs 复制代码
[root@mysql50 ~]# ls  /bakdir/*.sql      #查看备份情况
[root@mysql50 ~]# scp  /bakdir/*.sql  [email protected]:/root/  #备份数据

模拟数据库误删情况,恢复数据.

注意: 通过之前的mysqldump 方式备份的数据,已经传到了 mysql51机器中.

在mysql51主机连接服务 删除库

复制代码
sql 复制代码
[root@mysql51 ~]# mysql -uroot -p123 #连接数据库
mysql> drop database tarena;         #删除库
mysql> exit

/恢复数据

cs 复制代码
[root@mysql51 ~]# mysql -uroot -p123 < /root/tarena.sql

登录查看

sql 复制代码
[root@mysql51 ~]# mysql -uroot -p123 
mysql> use tarena; //进库
mysql> show tables; //看表
+------------------+
| Tables_in_tarena |
+------------------+
| departments      |
| employees        |
| salary           |
| stu4             |
| user             |
| wage_grade       |
+------------------+
6 rows in set (0.00 sec)

注意:删除表,然后恢复也是同样的操作,前提条件是前面mysqldump导出了相应的表.

增量备份

**增量备份:**备份上次备份后,新产生的数据。

使用工具: PERCONA Xtrabackup 是一款强大的在线热备份工具,备份过程中++不锁库表,适合生产环境++。支持完全备份与恢复、增量备份与恢复、差异备份与恢复。
环境准备:

安装 percona 软件 (去官网找安装包哦)

//安装依赖

root@host50 \~\]# yum -y install perl-DBD-MySQL //解压源码 \[root@host50 \~ \]# tar -xf percona-xtrabackup-8.0.26-18-Linux-x86_64.glibc2.12-minimal.tar.gz //移动并改名 (名字太长,路径不好写) \[root@host50 \~ \]# mv percona-xtrabackup-8.0.26-18-Linux-x86_64.glibc2.12-minimal /usr/local/percona //把命令添加到系统环境变量 \[root@host50 \~ \]# vim /etc/bashrc export PATH=/usr/local/percona/bin:$PATH **#添加在文件末尾** 保存退出 ```cs [root@host50 ~ ]# source /etc/bashrc #启动 //查看帮助信息 [root@host50 ~ ]# man xtrabackup (按q 退出) ``` ................**mysql51 也是同样的安装配置操作**................................

注意: \ 是换行符,代码太长写的,两行都是代码.

--host: 指定 ip, --user: 指定用户 --password: 指定密码

周一完全备份(备份所有数据)

#指定备份日期为周一的时间段

sql 复制代码
mysql> insert into tarena.salary(date,employee_id,
basic,bonus)
values("20230610",18,25000,8000);

周二增量备份(备份周一备份后新产生的数据)

#注意增量备份 ,增加了 --incremental-basedir 选项

cs 复制代码
[root@mysql50 ~]# xtrabackup --host=127.0.0.1 --user=root --password=123 \ 
--backup --target-dir=/new2 --incremental-basedir=/fullbak --datadir=/var/lib/mysql

//插入新数据 (可以插入多行)

sql 复制代码
mysql> insert into tarena.salary(date,employee_id,
basic,bonus)
values("20230710",18,25000,8000);

周三增量备份(备份周二备份后新产生的数据)

cs 复制代码
//备份
[root@mysql50 ~]# xtrabackup --host=127.0.0.1 --user=root --password=123 \
 --backup --target-dir=/new3 --incremental-basedir=/new2  --datadir=/var/lib/mysql
//插入新数据 (可以插入多行)
mysql> insert into tarena.salary
(date,employee_id,
basic,bonus)
values("20230710",18,25000,8000);

#可以多次增量备份.后面几天的数据,看看效果.

步骤二:练习数据增量恢复

增量恢复数据步骤:

  1. 准备恢复数据
  2. 合并数据
  3. 清空数据库目录
  4. 拷贝数据
  5. 修改数据库目录所有者/组用户为mysql
  6. 重启数据库服务

把MySQL50主机的备份文件拷贝给mysql51

cs 复制代码
//拷贝完全备份文件
[root@mysql50~]# scp  --r  /fullbak  [email protected]:/opt/
//拷贝增量备份文件
[root@mysql50 ~]# scp --r  /new*  [email protected]:/opt/

在MySQL51主机使用备份文件恢复数据

cs 复制代码
准备恢复数据

[root@mysql51 ~]# xtrabackup --prepare --apply-log-only --target-dir=/opt/fullbak
合并数据

//将周二的增量数据拷贝到周一备份目录里

合并数据选项: --incremental-dir

cs 复制代码
[root@mysql51 ~]# xtrabackup --prepare --apply-log-only --target-dir=/opt/fullbak \
--incremental-dir=/opt/new2

#清空数据库目录,测试数据情况

清空数据库目录

  1. root@mysql51 \~\]# rm -rf /var/lib/mysql/\*

拷贝数据

#拷贝的是,上一部分合并在 fullback 的目录

  1. root@mysql51 \~\]# xtrabackup --copy-back --target-dir=/fullbak

修改数据库目录所有者/组用户为mysql

  1. root@mysql51 \~\]# chown -R mysql:mysql /var/lib/mysql

重启数据库服务

  1. root@mysql51 \~\]# systemctl restart mysqld

  1. root@mysql51 \~\]# mysql -uroot -p123

差异备份

差异备份:备份完全备份后新产生的数据。

#使用场景广泛,备份速度快,节省数据库空间。

步骤一:

周一完全备份

  1. root@mysql50 \~\]# xtrabackup --host=127.0.0.1 --user=root --password=123 --backup --target-dir=/allbak --datadir=/var/lib/mysql

  2. mysql> insert into tarena.salary(date,employee_id,basic,bonus)values("20230810",18,25000,8000);

#周二差异备份,备份周一备份后新产生的数据

  1. root@mysql50 \~\]# xtrabackup --host=127.0.0.1 --user=root --password=123 --backup --target-dir=/dir2 --incremental-basedir=/allbak --datadir=/var/lib/mysql

  2. mysql> insert into tarena.salary(date,employee_id,basic,bonus)values("20230810",18,25000,8000);

步骤二:练习差异恢复

差异恢复数据步骤:

  1. 准备恢复数据
  2. 合并数据
  3. 清空数据库目录
  4. 拷贝数据
  5. 修改数据库目录所有者/组用户为mysql
  6. 重启数据库服务

具体操作如下:

把MySQL50的备份文件拷贝给MySQL51

  1. root@mysql50 \~\]# scp --r /allbak [email protected]:/root/ 周一完全备份

准备恢复数据

  1. root@mysql51 \~\]# xtrabackup --prepare --apply-log-only --target-dir=/root/allbak

合并数据

#将差异备份的的周一周二内容合并

  1. root@mysql51 \~\]# xtrabackup --prepare --apply-log-only --target-dir=/root/allbak \\

#清空数据库目录,并测试

清空数据库目录

  1. root@mysql51 \~\]# rm -rf /var/lib/mysql/\*

  2. root@mysql51 \~\]# xtrabackup --copy-back --target-dir=/root/allbak

  3. root@mysql51 \~\]# chown -R mysql:mysql /var/lib/mysql

  4. root@mysql51 \~\]# systemctl restart mysqld

  5. root@mysql51 \~\]# mysql -uroot -p123

二.binlog日志

环境准备

  1. yum --y install mysql mysql-server
  2. systemctl start mysqld

#装包启动服务


步骤一:查看正在使用的binlog日志文件

sql 复制代码
[root@mysql52 ~]# mysql  连接服务
mysql> show master status; 查看日志文件
+----------------+----------+--------------+------------------+-------------------+
| File           | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+----------------+----------+--------------+------------------+-------------------+
| binlog.000001 |      156 |              |                  |                   |
+----------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
sql 复制代码
执行查询命令

mysql> select count(*) from  mysql.user;
+----------+
| count(*) |
+----------+
|        4 |
+----------+
1 row in set (0.00 sec)

执行写命令时, 日志偏移量会发生改变

步骤二:自定义日志目录和日志名

sql 复制代码
[root@mysql52 ~]# vim /etc/my.cnf.d/mysql-server.cnf 
[mysqld]
log-bin=/mylog/mysql52   //定义日志目录和日志文件名(手动添加)
:wq
[root@mysql52 ~]# mkdir /mylog  创建目录
[root@mysql52 ~]# chown  mysql  /mylog   修改目录所有者mysql用户
[root@mysql52 ~]# setenforce  0   关闭selinux
[root@mysql52 ~]# systemctl  restart mysqld   重启服务
[root@mysql52 ~]# ls /mylog/  查看日志目录
mysql52.000001  mysql52.index

登陆服务

  1. root@mysql52 \~\]# mysql

Mysql> show master status ;

  1. +----------------+----------+--------------+------------------+-------------------+
  2. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  3. +----------------+----------+--------------+------------------+-------------------+
  4. | mysql52.000001 | 156 | | | |
  5. +----------------+----------+--------------+------------------+-------------------+

说明:默认日志文件容量大于1G时会自动创建新的日志文件,在日志文件没写满时,执行的所有写命令都会保存到当前使用的日志文件里。

注意点:

//只要服务重启就会创建新日志

/完全备份后创建新的日志文件,创建的日志个数和备份库的个数一致

步骤四:练习日志相关命令的使用

sql 复制代码
//查看已有的日志文件

mysql> show binary  logs;
日志文件名        日志大小(字节)  加密(no/yes)
+----------------+-----------+-----------+
| Log_name       | File_size | Encrypted |
+----------------+-----------+-----------+
| mysql52.000001 |       201 | No        |
+----------------+-----------+-----------+
7 rows in set (0.00 sec)

//查看正在使用的日志

sql 复制代码
mysql> show master status;
+----------------+----------+--------------+------------------+-------------------+
| File           | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+----------------+----------+--------------+------------------+-------------------+
| mysql52.000002 |      156 |              |                  |                   |
+----------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

//插入记录

sql 复制代码
mysql> insert into db1.user values("yaya");

//查看日志文件内容

说明:

Log_name: 日志文件名。

Pos: 命令在日志文件中的起始位置。

Event_type: 事件类型,例如 Query、Table_map、Write_rows 等。

Server_id: 服务器 ID。

End_log_pos:命令在文件中的结束位置,以字节为单位。

Info:执行命令信息。

sql 复制代码
mysql> show binlog events in  "mysql52.000007";
+----------------+-----+----------------+-----------+-------------+--------------------------------------+
| Log_name       | Pos | Event_type     | Server_id | End_log_pos | Info                                 |
+----------------+-----+----------------+-----------+-------------+--------------------------------------+
| mysql52.000007 |   4 | Format_desc    |         1 |         125 | Server ver: 8.0.26, Binlog ver: 4    |
| mysql52.000007 | 125 | Previous_gtids |         1 |         156 |                                      |
| mysql52.000007 | 156 | Anonymous_Gtid |         1 |         235 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql52.000007 | 235 | Query          |         1 |         306 | BEGIN                                |
| mysql52.000007 | 306 | Table_map      |         1 |         359 | table_id: 108 (db1.user)             |
| mysql52.000007 | 359 | Write_rows     |         1 |         400 | table_id: 108 flags: STMT_END_F      |
| mysql52.000007 | 400 | Xid            |         1 |         431 | COMMIT /* xid=649 */                 |
+----------------+-----+----------------+-----------+-------------+--------------------------------------+
7 rows in set (0.00 sec)

//删除日志文件名之前的所有日志文件

  1. mysql> purge master logs to "mysql52.000003";
    //删除所有日志文件,并重新创建日志文件

  2. mysql> reset master;

步骤五:使用日志恢复数据

1)在mysql52主机执行如下操:

//重置日志

复制代码
  1. mysql> reset master;

//查看日志

复制代码
  1. mysql> show master status;
  2. +----------------+----------+--------------+------------------+-------------------+
  3. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  4. +----------------+----------+--------------+------------------+-------------------+
  5. | mysql52.000001 | 156 | | | |
  6. +----------------+----------+--------------+------------------+-------------------+
  7. 1 row in set (0.00 sec)

//建库

  1. mysql> create database gamedb;

//建表

mysql> create table gamedb.t1(name char(10),class char(3));

//插入记录

  1. mysql> insert into gamedb.t1 values ("yaya","nsd");
  2. mysql> insert into gamedb.t1 values ("yaya","nsd");
  3. mysql> insert into gamedb.t1 values ("yaya","nsd");

//查看表记录

  1. mysql> select * from gamedb.t1;
  2. +------+-------+
  3. | name | class |
  4. +------+-------+
  5. | yaya | nsd |
  6. | yaya | nsd |
  7. | yaya | nsd |
  8. +------+-------+
  9. 3 rows in set (0.00 sec)
  10. mysql> exit

//把日志文件拷贝给恢复数据的服务器,比如 mysql50

sql 复制代码
[root@mysql52 ~]# scp /mylog/mysql52.000001 [email protected]:/root/

2)在MySQL50 使用日志恢复数据

//查看日志

复制代码
[root@mysql50 ~]# ls /root/mysql52.000001
  1. /root/mysql52.000001

//执行日志恢复数据

复制代码
  1. root@mysql50 \~\]# mysqlbinlog /root/mysql52.000001 \| mysql -uroot -p123

  2. root@mysql50 \~\]# mysql -uroot -p123 -e 'select \* from gamedb.t1'

  3. +------+-------+

  4. | name | class |

  5. +------+-------+

  6. | yaya | nsd |

  7. | yaya | nsd |

  8. | yaya | nsd |

  9. +------+-------+


DBA day06

环境准备:

#有同步工具的,可以把两台虚拟机一起装包启动服务。

一、MySQL主从同步

原理:

形式介绍

第一种是老大和小弟的关系;第二种是两兄弟,第三种是多保险,多个数据备份的数据库,避免出现意外数据库宕机,单一从数据库恢复难度大的问题。

还有主--从---从关系。通俗来说,就是老大的老大,和小弟的小弟的关系。

好比 :老大下完命令,老二传达,最后老二的小弟执行命令。


理论结束,时间开始。(*^▽^*)

步骤一:把192.168.88.53配置为master服务器

  1. root@mysql53 \~\]# vim /etc/my.cnf.d/mysql-server.cnf

  2. server-id=53

  3. log-bin=mysql53

  4. :wq

  5. root@mysql53 \~\]# systemctl restart mysqld

  6. root@mysql53 \~\]# mysql

  7. mysql> grant replication slave on *.* to repluser@"%";

查看日志信息

  1. mysql> show master status;
  2. +----------------+----------+--------------+------------------+-------------------+
  3. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  4. +----------------+----------+--------------+------------------+-------------------+
  5. | mysql53.000001 | 667 | | | |
  6. +----------------+----------+--------------+------------------+-------------------+
  7. 1 row in set (0.00 sec)

步骤二:把192.168.88.54配置为slave服务器

1)指定server-id 并重启数据库服务

  1. root@mysql54 \~\]# vim /etc/my.cnf.d/mysql-server.cnf

  2. server-id=54

  3. :wq

  4. root@mysql54 \~\]# systemctl restart mysqld

  5. root@mysql54 \~\]# mysql

  6. master_host="0.0.0.0", #任何用户都可以访问

  7. master_user="repluser",

  8. master_password="123",

  9. master_log_file="mysql53.000001",

  10. master_log_pos=667;

//启动slave进程

  1. mysql> start slave ;

//查看状态信息

#主要看IO/SQL 线程 是否为yes状态

sql 复制代码
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Master_Host: 192.168.88.53
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql53.000001
Read_Master_Log_Pos: 667
Relay_Log_File: mysql54-relay-bin.000002
Relay_Log_Pos: 322
Relay_Master_Log_File: mysql53.000001
Slave_IO_Running: Yes //IO线程
Slave_SQL_Running: Yes //SQL线程
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: 667
Relay_Log_Space: 533
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: 53
Master_UUID: 38c02165-005e-11ee-bd2d-525400007271
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
Master_public_key_path:
Get_master_public_key: 0
Network_Namespace:
1 row in set, 1 warning (0.00 sec)
mysql>

步骤三:测试配置

1)在master服务器建库 、建表、添加用户 、授权

  1. root@mysql53 \~\]# mysql 连接服务

  2. mysql> create table gamedb.user(name char(10) , class char(3)); 建表
  3. mysql> create user dc@"%" identified by "123"; 创建用户
  4. mysql> grant select,insert,update,delete on gamedb.* to dc@"%" ; 授予权限
  1. slave服务器查看库、表、用户 #和master的用户对应上
  1. root@mysql54 \~\]# mysql 连接服务

  2. mysql> desc gamedb.user; 查看表头
  3. mysql> select user from mysql.user where user="dc"; 查看用户
  4. mysql> show grants for dc@"%" ; 查看权限

3)客户端连接从服务器查看数据

  1. root@mysql50 \~\]# mysql -h192.168.88.54 -usf -p123

  2. +------+-------+
  3. | name | class |
  4. +------+-------+
  5. | yaya | nsd |
  6. +------+-------+
  7. Mysql>

二、数据读写分离

在主从结构基础上,增加读写分离功能。

初步结构是 1个MySQL53个主,1个MySQL54从服务器,1个mysql50客户端服务器。

#就不去配置多的虚拟机测试环境了,免得你们看不过来。但是主从同步配置得弄懂。

#mycat 属于中间件软件的一种。


配置mycat服务器

#在客户端服务器上,下载并配置mycat

//安装jdk

  1. root@mycat50 upload\]# yum -y install java-1.8.0-openjdk.x86_64

  2. root@mycat50 upload\]# which unzip \|\| yum -y install unzip

  3. root@mycat50 upload\]# unzip mycat2-install-template-1.21.zip

//安装依赖

  1. root@mycat50 upload\]# cp mycat2-1.21-release-jar-with-dependencies.jar /usr/local/mycat/lib/

  2. root@mycat50 upload\]# chmod u+x /usr/local/mycat/

3)定义客户端连接mycat服务使用用户及密码:

  1. root@mycat58 \~\]# vim /usr/local/mycat/conf/users/root.user.json

  2. "dialect":"mysql",
  3. "ip":null,
  4. "password":"123", 密码
  5. "transactionType":"proxy",
  6. "username":"mycat" 用户名
  7. }
  8. :wq

4)定义连接的数据库服务器

  1. root@mycat50 \~\]# vim /usr/local/mycat/conf/datasources/prototypeDs.data

  2. "dbType":"mysql",
  3. "idleTimeout":60000,
  4. "initSqls":[],
  5. "initSqlsGetConnection":true,
  6. "instanceType":"READ_WRITE",
  7. "maxCon":1000,
  8. "maxConnectTimeout":3000,
  9. "maxRetryCount":5,
  10. "minCon":1,
  11. "name":"prototypeDs",
  12. "password":"123", #密码
  13. "type":"JDBC",
  14. "url":"jdbc:mysql://localhost:3306/mysql?useUnicode=true&serverTimezone=Asia/Shanghai&characterEncoding=UTF-8", 连接本机的数据库服务
  15. "user":"dc", #用户名
  16. "weight":0
  17. }
  18. :wq

#启动 mysql服务 #创建mysql时用户与授权与配置的一致。

启动mycat服务

复制代码
  1. //查看帮助
  2. root@mycat50 \~\]# /usr/local/mycat/bin/mycat help

  3. //启动服务
  4. root@mycat50 \~\]# /usr/local/mycat/bin/mycat start

  5. //半分钟左右 能看到端口
  6. root@mycat50 \~\]# netstat -utnlp \| grep 8066

  7. root@mycat50 \~\]#

连接mycat服务

  1. root@mycat50 \~\]# mysql -h127.0.0.1 -P8066 -umycat -p123

  2. +--------------------+
  3. | `Database` |
  4. +--------------------+
  5. | information_schema |
  6. | mysql |
  7. | performance_schema |
  8. +--------------------+
  9. 3 rows in set (0.11 sec)
  10. Mysql>

步骤三:配置读写分离

1)添加数据源:连接mycat服务后做如下操作

bash 复制代码
//连接mycat服务
[root@mycat50 ~]# mysql -h127.0.0.1 -P8066 -umycat -p123
//添加mysql53数据库服务器
MySQL> /*+ mycat:createdatasource{
"name":"whost56", 
"url":"jdbc:mysql://192.168.88.56:3306",
"user":"dca","password":"123"
}*/;
//添加mysql54数据库服务器
Mysql>/*+ mycat:createdatasource{
"name":"rhost57", 
"url":"jdbc:mysql://192.168.88.57:3306",
"user":"dca",
"password":"123"
}*/;
  1. //查看数据源
  2. mysql> /*+mycat:showDataSources{}*/ \G
  3. *************************** 1. row ***************************
  4. NAME: whost53
  5. USERNAME: dca
  6. PASSWORD: 123
  7. MAX_CON: 1000
  8. MIN_CON: 1
  9. EXIST_CON: 0
  10. USE_CON: 0
  11. MAX_RETRY_COUNT: 5
  12. MAX_CONNECT_TIMEOUT: 30000
  13. DB_TYPE: mysql
  14. URL: jdbc:mysql://192.168.88.53:3306?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true
  15. WEIGHT: 0
  16. INIT_SQL:
  17. INIT_SQL_GET_CONNECTION: true
  18. INSTANCE_TYPE: READ_WRITE
  19. IDLE_TIMEOUT: 60000
  20. DRIVER: {
  21. CreateTime:"2023-06-02 17:01:14",
  22. ActiveCount:0,
  23. PoolingCount:0,
  24. CreateCount:0,
  25. DestroyCount:0,
  26. CloseCount:0,
  27. ConnectCount:0,
  28. Connections:[
  29. ]
  30. }
  31. TYPE: JDBC
  32. IS_MYSQL: true
  33. *************************** 2. row ***************************
  34. NAME: rhost54
  35. USERNAME: dca
  36. PASSWORD: 123
  37. MAX_CON: 1000
  38. MIN_CON: 1
  39. EXIST_CON: 0
  40. USE_CON: 0
  41. MAX_RETRY_COUNT: 5
  42. MAX_CONNECT_TIMEOUT: 30000
  43. DB_TYPE: mysql
  44. URL: jdbc:mysql://192.168.88.54:3306?useUnicode=true&serverTimezone=Asia/Shanghai&characterEncoding=UTF-8&autoReconnect=true
  45. WEIGHT: 0
  46. INIT_SQL:
  47. INIT_SQL_GET_CONNECTION: true
  48. INSTANCE_TYPE: READ_WRITE
  49. IDLE_TIMEOUT: 60000
  50. DRIVER: {
  51. CreateTime:"2023-06-02 17:01:14",
  52. ActiveCount:0,
  53. PoolingCount:0,
  54. CreateCount:0,
  55. DestroyCount:0,
  56. CloseCount:0,
  57. ConnectCount:0,
  58. Connections:[
  59. ]
  60. }
  61. TYPE: JDBC
  62. IS_MYSQL: true
  63. *************************** 3. row ***************************
  64. NAME: prototypeDs #原型库
  65. USERNAME: dc
  66. PASSWORD: 123
  67. MAX_CON: 1000
  68. MIN_CON: 1
  69. EXIST_CON: 0
  70. USE_CON: 0
  71. MAX_RETRY_COUNT: 5
  72. MAX_CONNECT_TIMEOUT: 3000
  73. DB_TYPE: mysql
  74. URL: jdbc:mysql://localhost:3306/mysql?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true
  75. WEIGHT: 0
  76. INIT_SQL:
  77. INIT_SQL_GET_CONNECTION: true
  78. INSTANCE_TYPE: READ_WRITE
  79. IDLE_TIMEOUT: 60000
  80. DRIVER: {
  81. CreateTime:"2023-06-02 17:01:14",
  82. ActiveCount:0,
  83. PoolingCount:0,
  84. CreateCount:0,
  85. DestroyCount:0,
  86. CloseCount:0,
  87. ConnectCount:0,
  88. Connections:[
  89. ]
  90. }
  91. TYPE: JDBC
  92. IS_MYSQL: true
  93. 3 rows in set (0.00 sec)
  94. mysql>exit //断开连接
  95. //添加的数据源以文件的形式保存在安装目录下
  96. root@mycat50 conf\]# ls /usr/local/mycat/conf/datasources/

  97. root@mycat50 conf\]#

#测试主从同步,与读写分离情况

2)配置数据库服务器添加qqa用户

//在master服务器添加

  1. root@mysql53 \~\]# mysql

  2. mysql> grant all on *.* to qqa@"%";
  3. mysql>exit

//在slave服务器查看是否同步成功

  1. root@mysql54 \~\]# mysql -e 'select user , host from mysql.user where user="qqa"'

  2. | user | host |
  3. +------+------+
  4. | plja | % |
  5. +------+------+
  6. root@mysql54 \~\]#

//连接mycat服务

复制代码
  1. root@mycat58 \~\]# mysql -h127.0.0.1 -P8066 -umycat -p123

bash 复制代码
mysql>/*!mycat:createcluster{
"name":"rwcluster",
"masters":["whost53"],
"replicas":["rhost54"]
}*/ ;
Mysql>

//查看集群信息

bash 复制代码
mysql> /*+ mycat:showClusters{}*/ \G
*************************** 1. row ***************************
             NAME: rwcluster
      SWITCH_TYPE: SWITCH
MAX_REQUEST_COUNT: 2000
             TYPE: BALANCE_ALL
         WRITE_DS: whost53
          READ_DS: whost53,rhost54
          WRITE_L: io.mycat.plug.loadBalance.BalanceRandom$1
           READ_L: io.mycat.plug.loadBalance.BalanceRandom$1
        AVAILABLE: true
*************************** 2. row ***************************
             NAME: prototype
      SWITCH_TYPE: SWITCH
MAX_REQUEST_COUNT: 200
             TYPE: BALANCE_ALL
         WRITE_DS: prototypeDs
          READ_DS: prototypeDs
          WRITE_L: io.mycat.plug.loadBalance.BalanceRandom$1
           READ_L: io.mycat.plug.loadBalance.BalanceRandom$1
        AVAILABLE: true
2 rows in set (0.00 sec)
mysql>

//创建的集群, 会以文件的形式保存在目录下

  1. root@mycat50 conf\]#**ls** /usr/local/mycat/conf/clusters/

4)指定主机角色

//修改master角色主机仅负责写访问

  1. root@mycat50 \~\]# vim /usr/local/mycat/conf/datasources/whost56.datasource.json

  2. "dbType":"mysql",
  3. "idleTimeout":60000,
  4. "initSqls":[],
  5. "initSqlsGetConnection":true,
  6. "instanceType":"WRITE", #仅负责写访问
  7. "logAbandoned":true,
  8. "maxCon":1000,
  9. "maxConnectTimeout":30000,
  10. "maxRetryCount":5,
  11. "minCon":1,
  12. "name":"whost53",
  13. "password":"123",
  14. "queryTimeout":0,
  15. "removeAbandoned":false,
  16. "removeAbandonedTimeoutSecond":180,
  17. "type":"JDBC",
  18. "url":"jdbc:mysql://192.168.88.53:3306?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true",
  19. "user":"qqa",
  20. "weight":0
  21. }
  22. :wq

//修改slave角色主机仅负责读访问

  1. root@mycat50 \~\]# vim /usr/local/mycat/conf/datasources/rhost57.datasource.json

  2. "dbType":"mysql",
  3. "idleTimeout":60000,
  4. "initSqls":[],
  5. "initSqlsGetConnection":true,
  6. "instanceType":"READ", #仅负责读访问
  7. "logAbandoned":true,
  8. "maxCon":1000,
  9. "maxConnectTimeout":30000,
  10. "maxRetryCount":5,
  11. "minCon":1,
  12. "name":"rhost54",
  13. "password":"123",
  14. "queryTimeout":0,
  15. "removeAbandoned":false,
  16. "removeAbandonedTimeoutSecond":180,
  17. "type":"JDBC",
  18. "url":"jdbc:mysql://192.168.88.54:3306?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true",
  19. "user":"qqa",
  20. "weight":0
  21. }
  22. :wq

5)修改读策略

#类似负载均衡设置

  1. root@mycat50 \~\]# vim /usr/local/mycat/conf/clusters/rwcluster.cluster.json

  2. "clusterType":"MASTER_SLAVE",
  3. "heartbeat":{
  4. "heartbeatTimeout":1000,
  5. "maxRetryCount":3,
  6. "minSwitchTimeInterval":300,
  7. "showLog":false,
  8. "slaveThreshold":0.0
  9. },
  10. "masters":[
  11. "whost53"
  12. ],
  13. "maxCon":2000,
  14. "name":"rwcluster",
  15. "readBalanceType":"BALANCE_ALL_READ", #把读访问平均分配给read角色的主机
  16. "replicas":[
  17. "rhost54"
  18. ],
  19. "switchType":"SWITCH"
  20. }
  21. :wq

//重启mycat服务

  1. root@mycat50 \~\]# /usr/local/mycat/bin/mycat restart

  2. Stopped mycat2.
  3. Starting mycat2...

步骤四:测试配置

思路如下:

  1. 连接mycat服务建库
  2. 指定存储数据使用的集群
  3. 连接mycat服务建表
  4. 客户端连接mycat服务执行select 或 insert

具体操作如下:

//连接mycat服务

复制代码
  1. root@mycat50\~\]# mysql -h127.0.0.1 -P8066 -umycat -p123

  2. mysql> create database testdb;

  3. mysql> exit

  4. Bye

//指定库存储数据使用的集群

复制代码
  1. root@mycat50\~\]# vim /usr/local/mycat/conf/schemas/testdb.schema.json

  2. "customTables":{},
  3. "globalTables":{},
  4. "normalProcedures":{},
  5. "normalTables":{},
  6. "schemaName":"testdb",
  7. "targetName":"rwcluster", 添加此行(之前创建的集群名rwcluster)
  8. "shardingTables":{},
  9. "views":{}
  10. }
  11. :wq

//重启mycat服务

  1. root@mycat50 \~\]# /usr/local/mycat/bin/mycat restart

  2. Stopped mycat2.
  3. Starting mycat2...

测试读写分离

//在slave服务器本机插入记录,使其与master服务器的数据不一样

复制代码
  1. root@mysql54 \~\]# mysql -e 'insert into testdb.user values ("yayaA","654321")'

  2. +-------+----------+
  3. | name | password |
  4. +-------+----------+
  5. | yaya | 123456 |
  6. | yayaA | 654321 |
  7. +-------+----------+
  8. root@mysql57 \~\]#

复制代码
  1. root@mysql53 \~\]# mysql -e 'select \* from testdb.user'

  2. | name | password |
  3. +------+----------+
  4. | yaya | 123456 |
  5. +------+----------+

//binlog日志偏移量不变

复制代码
  1. root@mysql53 \~\]# mysql -e 'show master status'

  2. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  3. +----------------+----------+--------------+------------------+-------------------+
  4. | mysql56.000002 | 4514 | | | |
  5. +----------------+----------+--------------+------------------+-------------------+
  6. root@mysql53 \~\]#


DBA Day07

环境准备:


一、数据分片概述

二、部署mycat服务

分片规则

步骤一:把MySQL60配置为MySQL59的从服务器

#具体操作,看 Day06 MySQL主从同步设置

步骤二:把MySQL62配置为MySQL61的从服务器

#具体操作,看 Day06 MySQL主从同步设置

步骤三:把主机mycat63配置为mycat服务器。

安装mycat软件

  1. //安装jdk
  2. root@mycat63 \~\]# yum -y install java-1.8.0-openjdk.x86_64

  3. root@mycat63 \~\]# which unzip \|\| yum -y install unzip

  4. root@mycat63 \~\]# unzip mycat2-install-template-1.21.zip

  5. //安装依赖
  6. root@mycat63 \~\]# cp mycat2-1.21-release-jar-with-dependencies.jar /usr/local/mycat/lib/

  7. root@mycat63 \~\]# chmod -R 777 /usr/local/mycat/

复制代码
  1. root@mycat63 \~\]# vim /usr/local/mycat/conf/users/root.user.json

  2. "dialect":"mysql",
  3. "ip":null,
  4. "password":"123", 密码
  5. "transactionType":"proxy",
  6. "username":"mycat" 用户名
  7. }
  8. :wq

4)定义连接的数据库服务

复制代码
  1. root@mycat63 \~\]# vim /usr/local/mycat/conf/datasources/prototypeDs.data.json

  2. "dbType":"mysql",

  3. "idleTimeout":60000,

  4. "initSqls":[],

  5. "initSqlsGetConnection":true,

  6. "instanceType":"READ_WRITE",

  7. "maxCon":1000,

  8. "maxConnectTimeout":3000,

  9. "maxRetryCount":5,

  10. "minCon":1,

  11. "name":"prototypeDs",

  12. "password":"123", 密码

  13. "type":"JDBC",

  14. "url":"jdbc:mysql://localhost:3306/mysql?useUnicode=true&serverTimezone=Asia/Shanghai&characterEncoding=UTF-8", 连接本机的数据库服务

  15. "user":"qq", #直接设置用户名,其他不用管

  16. "weight":0

  17. }

  18. :wq
    5)在mycat63主机运行数据库服务

  19. //创建qq用户

  20. root@mycat63 \~\]# mysql

  21. mysql> grant all on *.* to qq@"%" ; 授予权限

  22. mysql> exit

  23. Bye

6)启动mycat服务

  1. //查看命令帮助
  2. root@mycat63 \~\]# /usr/local/mycat/bin/mycat help

  3. //启动mycat服务
  4. root@mycat63 \~\]# /usr/local/mycat/bin/mycat start

  5. //半分钟左右 能看到端口
  6. root@mycat63 \~\]# netstat -utnalp \| grep 8066

  7. root@mycat63 \~\]#

步骤四:连接mycat服务器

1)连接本机的mycat服务

  1. root@mycat63 \~\]# mysql -h127.0.0.1 -P8066 -umycat -p123

  1. //添加MySQL59主机
  2. MySQL>/*+ mycat:createdatasource{
  3. "name":"dw0",
  4. "url":"jdbc:mysql://192.168.88.59:3306",
  5. "user":"qq",
  6. "password":"123"
  7. }*/;
  8. //添加MySQL60主机
  9. Mysql>/*+ mycat:createdatasource{
  10. "name":"dr0",
  11. "url":"jdbc:mysql://192.168.88.60:3306",
  12. "user":"qq",
  13. "password":"123"
  14. }*/;
  15. //添加MySQL61主机
  16. Mysql>/*+ mycat:createdatasource{
  17. "name":"dw1",
  18. "url":"jdbc:mysql://192.168.88.61:3306",
  19. "user":"qq",
  20. "password":"123"
  21. }*/;
  22. //添加MySQL62主机
  23. Mysql>/*+ mycat:createdatasource{
  24. "name":"dr1",
  25. "url":"jdbc:mysql://192.168.88.62:3306",
  26. "user":"qq",
  27. "password":"123"
  28. }*/;
  29. Mysql>

2)查看数据信息

  1. mysql> /*+mycat:showDataSources{}*/ \G

#显示所有 服务器配置情况即可

步骤六:配置数据库服务器

//在主服务器 MySQL59 主机添加qq用户

复制代码
  1. root@mysql59 \~\]# mysql

  2. Mysql> grant all on *.* to qq@"%";

//在主服务器 MySQL61 主机添加qq用户

复制代码
  1. root@mysql61 \~\]# mysql

  2. Mysql> grant all on *.* to qq@"%";

//在从服务器MySQL60 查看用户

复制代码
  1. root@mysql60 \~\]# mysql -e 'select user from mysql.user where user="qq" '

  2. | user |
  3. +------+
  4. | qq |
  5. +------+
  6. root@mysql60 \~\]#

复制代码
  1. root@mysql62 \~\]# mysql -e 'select user from mysql.user where user="qq"'

  2. | user |
  3. +------+
  4. | qq|
  5. +------+
  6. root@host62 \~\]#

步骤七:创建集群

连接mycat服务

复制代码
  1. root@mycat63 \~\]# mysql -h127.0.0.1 -P8066 -umycat -p123

复制代码
  1. mysql>/*!mycat:createcluster{
  2. "name":"c0",
  3. "masters":["dw0"],
  4. "replicas":["dr0"]
  5. }*/;

3)创建第2个集群 #不用敲,直接复制粘贴。懂得原理即可

复制代码
  1. mysql>/*!mycat:createcluster{
  2. "name":"c1",
  3. "masters":["dw1"],
  4. "replicas":["dr1"]
  5. }*/;
  6. Mysql>

4)查看集群信息

复制代码
  1. mysql> /*+ mycat:showClusters{}*/ \G
  2. *************************** 1. row ***************************
  3. NAME: prototype
  4. SWITCH_TYPE: SWITCH
  5. MAX_REQUEST_COUNT: 200
  6. TYPE: BALANCE_ALL
  7. WRITE_DS: prototypeDs
  8. READ_DS: prototypeDs
  9. WRITE_L: io.mycat.plug.loadBalance.BalanceRandom$1
  10. READ_L: io.mycat.plug.loadBalance.BalanceRandom$1
  11. AVAILABLE: true
  12. *************************** 2. row ***************************
  13. NAME: c0
  14. SWITCH_TYPE: SWITCH
  15. MAX_REQUEST_COUNT: 2000
  16. TYPE: BALANCE_ALL
  17. WRITE_DS: dw0
  18. READ_DS: dw0,dr0
  19. WRITE_L: io.mycat.plug.loadBalance.BalanceRandom$1
  20. READ_L: io.mycat.plug.loadBalance.BalanceRandom$1
  21. AVAILABLE: true
  22. *************************** 3. row ***************************
  23. NAME: c1
  24. SWITCH_TYPE: SWITCH
  25. MAX_REQUEST_COUNT: 2000
  26. TYPE: BALANCE_ALL
  27. WRITE_DS: dw1
  28. READ_DS: dw1,dr1
  29. WRITE_L: io.mycat.plug.loadBalance.BalanceRandom$1
  30. READ_L: io.mycat.plug.loadBalance.BalanceRandom$1
  31. AVAILABLE: true
  32. 3 rows in set (0.03 sec)
  33. mysql>

5)创建的集群保存在mycat安装目录下

复制代码
  1. root@MySQL63 \~\]# ls /usr/local/mycat/conf/clusters/

  2. root@mycat63 \~\]#

三、测试配置

在客户端client50 连接mycat63 存储数据 ,验证mycat63的配置

步骤一:建库

1)在mycat63 连接本机的mycat服务

复制代码
  1. root@mycat63 \~\]# mysql -h127.0.0.1 -umycat -p654321 -P8066

复制代码
  1. mysql> create database tarena;
  2. mysql> exit
  1. 配置文件存放位置
复制代码
  1. root@mycat63 \~\]# ls /usr/local/mycat/conf/schemas/tarena.schema.json

  2. root@mycat63 \~\]#

说明:

dbpartition by 定义分库使用的分片规则,

tbpartition by 定义分表使用的分片规则。

mod_hash 分片规则,用employee_id表头的值做取模计算

tbpartitions 表的分片数量

dbpartitions 库的分片数量

1)连接mycat服务

复制代码
  1. root@client50 \~\]# mysql -h192.168.88.63 -P8066 -umycat --p654321

复制代码
  1. create table tarena.employees(
  2. employee_id int primary key,
  3. name char(10),dept_id int ,
  4. mail varchar(30)
  5. ) default charset utf8
  6. dbpartition BY mod_hash(employee_id) tbpartition BY mod_hash(employee_id)
  7. tbpartitions 1 dbpartitions 2;

3)在59主机查看库和表

复制代码
  1. root@mysql59 \~\]# mysql -e 'show databases' //看库

  2. | Database |
  3. +--------------------+
  4. | information_schema |
  5. | mysql |
  6. | performance_schema |
  7. | sys |
  8. | tarena |
  9. | tarena_0 |
  10. +--------------------+
  11. root@mysql59 \~\]# mysql -e 'use tarena_0 ; show tables' //看表

  12. | Tables_in_tarena_0 |
  13. +--------------------+
  14. | employees_0 |
  15. +--------------------+
  16. root@host61 \~\]#

复制代码
  1. root@mysql60 \~\]# mysql -e 'show databases' 看库

  2. | Database |
  3. +--------------------+
  4. | information_schema |
  5. | mysql |
  6. | performance_schema |
  7. | sys |
  8. | tarena |
  9. | tarena_0 |
  10. +--------------------+
  11. root@mysql60 \~\]# mysql -e 'use tarena_0 ; show tables' 看表

  12. | Tables_in_tarena_0 |
  13. +--------------------+
  14. | employees_0 |
  15. +--------------------+
  16. root@host62 \~\]#

复制代码
  1. root@mysql61 \~\]# mysql -e 'show databases' 看库

  2. | Database |
  3. +--------------------+
  4. | information_schema |
  5. | mysql |
  6. | performance_schema |
  7. | sys |
  8. | tarena |
  9. | tarena_1 |
  10. +--------------------+
  11. root@mysql61 \~\]# mysql -e 'use tarena_1;show tables' 看表

  12. | Tables_in_tarena_1 |
  13. +--------------------+
  14. | employees_1 |
  15. +--------------------+
  16. root@host63 \~\]#

复制代码
  1. root@mysql62 \~\]# mysql -e 'show databases' 看库

  2. | Database |
  3. +--------------------+
  4. | information_schema |
  5. | mysql |
  6. | performance_schema |
  7. | sys |
  8. | tarena |
  9. | tarena_1 |
  10. +--------------------+
  11. root@mysql62 \~\]# mysql -e 'use tarena_1;show tables' 看表

  12. | Tables_in_tarena_1 |
  13. +--------------------+
  14. | employees_1 |
  15. +--------------------+
  16. root@host64 \~\]#

复制代码
  1. root@client50 \~\]# mysql -h192.168.88.63 -P8066 -umycat --p654321

  2. mysql> insert into tarena.employees values (8,"tom","3","[email protected]");
  3. mysql> insert into tarena.employees values (7,"lucy","2","[email protected]");
  4. mysql> insert into tarena.employees values (6,"john","2","[email protected]");

查看数据

复制代码
  1. mysql> select * from tarena.employees;
  2. +-------------+------+---------+-----------+
  3. | employee_id | name | dept_id | mail |
  4. +-------------+------+---------+-----------+
  5. | 6 | jim | 2 | [email protected] |
  6. | 8 | tom | 3 | [email protected] |
  7. | 7 | lucy | 2 | [email protected] |
  8. | 9 | john | 1 | [email protected] |
  9. +-------------+------+---------+-----------+
  10. 4 rows in set (2.07 sec)

查看c0集群中 数据库服务器存储的数据

复制代码
  1. 查看master服务器数据
  2. root@mysql59 \~\]# mysql -e 'select \* from tarena_0.employees_0'

  3. | employee_id | name | dept_id | mail |
  4. +-------------+------+---------+----------+
  5. | 6 | jim | 2 | [email protected] |
  6. | 8 | tom | 3 | [email protected] |
  7. +-------------+------+---------+----------+
  8. root@mysql59 \~\]#

  9. 查看slave服务器数据
  10. root@mysql60 \~\]# mysql -e 'select \* from tarena_0.employees_0'

  11. | employee_id | name | dept_id | mail |
  12. +-------------+------+---------+----------+
  13. | 6 | jim | 2 | [email protected] |
  14. | 8 | tom | 3 | [email protected] |
  15. +-------------+------+---------+----------+
  16. root@mysql60 \~\]#

复制代码
  1. 查看master服务器数据
  2. root@mysql61 \~\]# mysql -e 'select \* from tarena_1.employees_1'

  3. | employee_id | name | dept_id | mail |
  4. +-------------+------+---------+-----------+
  5. | 7 | lucy | 2 | [email protected] |
  6. | 9 | john | 1 | [email protected] |
  7. +-------------+------+---------+-----------+
  8. root@mysql61 \~\]#

  9. 查看slave服务器数据
  10. root@mysql62 \~\]# mysql -e 'select \* from tarena_1.employees_1'

  11. | employee_id | name | dept_id | mail |
  12. +-------------+------+---------+-----------+
  13. | 7 | lucy | 2 | [email protected] |
  14. | 9 | john | 1 | [email protected] |
  15. +-------------+------+---------+-----------+
  16. root@mysql62 \~\]#

ER表,称为关联表,表示数据逻辑上有关联性的两个或多个表,例如工资表和员工表。对于关联表,通常希望他们能够有相同的分片规则,这样在进行关联查询时,能够快速定位到同一个数据分片中。MyCat2中对于关联表,不需要有过多的声明,他可以根据分片规则自行判断。

1)连接mycat服务建表

复制代码
  1. root@client50 \~\]# mysql -h192.168.88.63 -P8066 -umycat --p654321

复制代码
  1. mysql> create table tarena.salary(
  2. employee_id int primary key,
  3. p_date date , basic int , bonus int
  4. ) DEFAULT CHARSET=utf8
  5. dbpartition BY mod_hash(employee_id)
  6. tbpartition BY mod_hash(employee_id) tbpartitions 1;
  7. Query OK, 1 row affected (1.93 sec)

3)在MyCat2终端查看关联表关系。

复制代码
  1. root@mycat63 \~\]# mysql -h127.0.0.1 -P8066 -umycat --p654321

  2. mysql> /*+ mycat:showErGroup{}*/ ;
  3. +---------+------------+-----------+
  4. | groupId | schemaName | tableName |
  5. +---------+------------+-----------+
  6. | 0 | tarena | employees |
  7. | 0 | tarena | salary |
  8. +---------+------------+-----------+
  9. 2 rows in set (0.00 sec)
  10. mysql>

4)在2台master服务器查看表

复制代码
  1. 在c0集群master服务器查看表
  2. root@mysql59 \~\]# mysql -e 'use tarena_0 ; show tables'

  3. | Tables_in_tarena_0 |
  4. +--------------------+
  5. | employees_0 |
  6. | salary_0 |
  7. +--------------------+
  8. root@mysql59 \~\]#

  9. root@mysql61 \~\]# mysql -e 'use tarena_1;show tables'

  10. | Tables_in_tarena_1 |
  11. +--------------------+
  12. | employees_1 |
  13. | salary_1 |
  14. +--------------------+
  15. root@mysql61\~\]#

复制代码
  1. root@client50 \~\]# mysql -h192.168.88.63 -P8066 -umycat --p654321

  2. mysql> insert into tarena.salary values(7,20230210,25000,2500);
  3. mysql> insert into tarena.salary values(8,20230310,30000,3000);
  4. mysql> insert into tarena.salary values(9,20230410,35000,3500);

6)在4台数据库服务器本机查看

复制代码
  1. root@mysql59 \~\]# mysql -e 'select \* from tarena_0.employees_0'

  2. | employee_id | name | dept_id | mail |
  3. +-------------+------+---------+----------+
  4. | 6 | C | 2 | [email protected] |
  5. | 8 | B | 3 | [email protected] |
  6. +-------------+------+---------+----------+
  7. root@mysql60 \~\]# mysql -e 'select \* from tarena_0.salary_0'

  8. | employee_id | p_date | basic | bonus |
  9. +-------------+------------+-------+-------+
  10. | 6 | 2023-01-10 | 20000 | 2000 |
  11. | 8 | 2023-03-10 | 30000 | 3000 |
  12. +-------------+------------+-------+-------+
  13. root@mysql61 \~\]# mysql -e 'select \* from tarena_1.employees_1'

  14. | employee_id | name | dept_id | mail |
  15. +-------------+------+---------+-----------+
  16. | 7 | C | 2 | [email protected] |
  17. | 9 | a | 1 | [email protected] |
  18. +-------------+------+---------+-----------+
  19. root@mysql62 \~\]# mysql -e 'select \* from tarena_1.salary_1'

  20. | employee_id | p_date | basic | bonus |
  21. +-------------+------------+-------+-------+
  22. | 7 | 2023-02-10 | 25000 | 2500 |
  23. | 9 | 2023-04-10 | 35000 | 3500 |
  24. +-------------+------------+-------+-------+
  25. root@mysql62\~\]#

全局表 数据会插入到两个库中,并且两个库中都有全部的数据。

客户端client50 连接mycat63主机的 建表存储数据

复制代码
  1. root@client50 \~\]# mysql -h192.168.88.63 -umycat -p654321 -P8066

复制代码
  1. mysql> create table tarena.dept(
  2. dept_id int,
  3. dept_name char(10),
  4. primary key(dept_id)
  5. )default charset utf8 broadcast;

插入记录

复制代码
  1. mysql> insert into tarena.dept values(1,"开发部"),(2,"运维部"),(3,"测试部");

在4台数据库服务器查看数据

复制代码
  1. root@mysql59 \~\]# mysql -e 'select \* from tarena.dept'

  2. | dept_id | dept_name |
  3. +---------+-----------+
  4. | 1 | 开发部 |
  5. | 2 | 运维部 |
  6. | 3 | 测试部 |
  7. +---------+-----------+
  8. root@mysql60 \~\]# mysql -e 'select \* from tarena.dept'

  9. | dept_id | dept_name |
  10. +---------+-----------+
  11. | 1 | 开发部 |
  12. | 2 | 运维部 |
  13. | 3 | 测试部 |
  14. +---------+-----------+
  15. root@mysql61 \~\]# mysql -e 'select \* from tarena.dept'

  16. | dept_id | dept_name |
  17. +---------+-----------+
  18. | 1 | 开发部 |
  19. | 2 | 运维部 |
  20. | 3 | 测试部 |
  21. +---------+-----------+
  22. root@mysql62 \~\]# mysql -e 'select \* from tarena.dept'

  23. | dept_id | dept_name |
  24. +---------+-----------+
  25. | 1 | 开发部 |
  26. | 2 | 运维部 |
  27. | 3 | 测试部 |
相关推荐
代码吐槽菌3 分钟前
基于SpringBoot的律师事务所案件管理系统【附源码】
java·数据库·spring boot·后端·毕业设计
lsec14 分钟前
签名不等于可信:详解PE数字签名校验的漏洞与主动规避方案
安全
s_little_monster18 分钟前
【Linux】线程控制函数
linux·运维·服务器·经验分享·笔记·学习·学习方法
用户240036192350029 分钟前
mysql 索引的初步认识
数据库
狂奔solar1 小时前
Vanna + qwq32b 实现 text2SQL
数据库·sql
网络安全工程师老王1 小时前
clickhouse注入手法总结
数据库·clickhouse·web安全·网络安全·信息安全
!!!5251 小时前
MongoDB 新手笔记
数据库·笔记·mongodb
AWS官方合作商1 小时前
基于AWS的大模型调用场景:10大成本优化实战方案
云计算·gpu算力·aws
JIngJaneIL1 小时前
健身管理小程序|基于java微信开发健身管理小程序的系统设计与实现(源码+数据库+文档)
java·数据库·小程序·vue·毕业设计·论文·健身管理小程序
vortex52 小时前
探索 Shell 中的扩展通配符:从 Bash 到 Zsh
linux·运维·bash·shell·zsh