云计算第二阶段---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 root@192.168.88.51:/root/
[root@mysql50 ~]# scp /bakdir/mysql.tar.gz root@192.168.88.51:/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  root@192.168.88.51:/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 也是同样的安装配置操作................................

步骤一:数据增量备份(在mysql50主机 完成增量备份练习)

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

--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  root@192.168.88.51:/opt/
//拷贝增量备份文件
[root@mysql50 ~]# scp --r  /new*  root@192.168.88.51:/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
  2. mysql> select count(*) from tarena.salary where date=20230710;

差异备份

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

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

步骤一:

周一完全备份

  1. [root@mysql50 ~]# xtrabackup --host=127.0.0.1 --user=root --password=123 --backup --target-dir=/allbak --datadir=/var/lib/mysql
  2. //插入新数据 (可以插入多行)
  3. 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. //插入新数据 (可以插入多行)
  3. 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 root@192.168.88.51:/root/ 周一完全备份
  2. [root@mysql50 ~]# scp --r /dir2 root@192.168.88.51:/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 \
  2. --incremental-dir=/root/dir2

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

清空数据库目录

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

拷贝数据

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

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

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

重启数据库服务

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

连接服务查看数据

  1. [root@mysql51 ~]# mysql -uroot -p123
  2. mysql> select count(*) from tarena.salary where date=20230810;

二.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 root@192.168.88.50:/root/

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

//查看日志

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

//执行日志恢复数据

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

//连接服务查看数据

  1. [root@mysql50 ~]# mysql -uroot -p123 -e 'select * from gamedb.t1'
  2. mysql: [Warning] Using a password on the command line interface can be insecure.
  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. [mysqld]
  3. server-id=53
  4. log-bin=mysql53
  5. :wq
  6. [root@mysql53 ~]# systemctl restart mysqld

2)用户授权

  1. [root@mysql53 ~]# mysql
  2. mysql> create user repluser@"%" identified by "123";
  3. 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. [mysqld]
  3. server-id=54
  4. :wq
  5. [root@mysql54 ~]# systemctl restart mysqld

2)登陆服务指定主服务器信息 #和主机信息对应上

  1. [root@mysql54 ~]# mysql
  2. mysql> change master to
  3. master_host="0.0.0.0", #任何用户都可以访问
  4. master_user="repluser",
  5. master_password="123",
  6. master_log_file="mysql53.000001",
  7. 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 database gamedb; 建库
  3. mysql> create table gamedb.user(name char(10) , class char(3)); 建表
  4. mysql> create user dc@"%" identified by "123"; 创建用户
  5. mysql> grant select,insert,update,delete on gamedb.* to dc@"%" ; 授予权限
  1. slave服务器查看库、表、用户 #和master的用户对应上
  1. [root@mysql54 ~]# mysql 连接服务
  2. mysql> show databases; 查看库
  3. mysql> desc gamedb.user; 查看表头
  4. mysql> select user from mysql.user where user="dc"; 查看用户
  5. mysql> show grants for dc@"%" ; 查看权限

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

  1. [root@mysql50 ~]# mysql -h192.168.88.54 -usf -p123
  2. Mysql> select * from gamedb.user;
  3. +------+-------+
  4. | name | class |
  5. +------+-------+
  6. | yaya | nsd |
  7. +------+-------+
  8. 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

//安装解压命令

  1. [root@mycat50 upload]# which unzip || yum -y install unzip

//安装mycat

  1. [root@mycat50 upload]# unzip mycat2-install-template-1.21.zip
  2. [root@mycat50 upload]# mv mycat /usr/local/

//安装依赖

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

//修改权限

  1. [root@mycat50 upload]# chmod u+x /usr/local/mycat/

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

  1. [root@mycat58 ~]# vim /usr/local/mycat/conf/users/root.user.json
  2. {
  3. "dialect":"mysql",
  4. "ip":null,
  5. "password":"123", 密码
  6. "transactionType":"proxy",
  7. "username":"mycat" 用户名
  8. }
  9. :wq

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

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

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

启动mycat服务

复制代码
  1. //查看帮助
  2. [root@mycat50 ~]# /usr/local/mycat/bin/mycat help
  3. Usage: /usr/local/mycat/bin/mycat { console | start | stop | restart | status | dump }
  4. //启动服务
  5. [root@mycat50 ~]# /usr/local/mycat/bin/mycat start
  6. Starting mycat2...
  7. //半分钟左右 能看到端口
  8. [root@mycat50 ~]# netstat -utnlp | grep 8066
  9. tcp6 0 0 :::8066 :::* LISTEN 57015/java
  10. [root@mycat50 ~]#

连接mycat服务

  1. [root@mycat50 ~]# mysql -h127.0.0.1 -P8066 -umycat -p123
  2. mysql> show databases;
  3. +--------------------+
  4. | `Database` |
  5. +--------------------+
  6. | information_schema |
  7. | mysql |
  8. | performance_schema |
  9. +--------------------+
  10. 3 rows in set (0.11 sec)
  11. 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. prototypeDs.datasource.json rhost54.datasource.json whost53.datasource.json
  98. [root@mycat50 conf]#

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

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

//在master服务器添加

  1. [root@mysql53 ~]# mysql
  2. mysql> create user qqa@"%" identified by "123456";
  3. mysql> grant all on *.* to qqa@"%";
  4. mysql>exit

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

  1. [root@mysql54 ~]# mysql -e 'select user , host from mysql.user where user="qqa"'
  2. +------+------+
  3. | user | host |
  4. +------+------+
  5. | plja | % |
  6. +------+------+
  7. [root@mysql54 ~]#

3)创建集群

//连接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/
  2. prototype.cluster.json rwcluster.cluster.json

4)指定主机角色

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

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

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

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

5)修改读策略

#类似负载均衡设置

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

//重启mycat服务

  1. [root@mycat50 ~]# /usr/local/mycat/bin/mycat restart
  2. Stopping mycat2...
  3. Stopped mycat2.
  4. Starting mycat2...

步骤四:测试配置

思路如下:

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

具体操作如下:

//连接mycat服务

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

//创建存储数据的库

  1. mysql> create database testdb;
  2. mysql> exit
  3. Bye

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

复制代码
  1. [root@mycat50~]# vim /usr/local/mycat/conf/schemas/testdb.schema.json
  2. {
  3. "customTables":{},
  4. "globalTables":{},
  5. "normalProcedures":{},
  6. "normalTables":{},
  7. "schemaName":"testdb",
  8. "targetName":"rwcluster", 添加此行(之前创建的集群名rwcluster)
  9. "shardingTables":{},
  10. "views":{}
  11. }
  12. :wq

//重启mycat服务

  1. [root@mycat50 ~]# /usr/local/mycat/bin/mycat restart
  2. Stopping mycat2...
  3. Stopped mycat2.
  4. Starting mycat2...

测试读写分离

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

复制代码
  1. [root@mysql54 ~]# mysql -e 'insert into testdb.user values ("yayaA","654321")'
  2. [root@mysql54~]# mysql -e 'select * from testdb.user'
  3. +-------+----------+
  4. | name | password |
  5. +-------+----------+
  6. | yaya | 123456 |
  7. | yayaA | 654321 |
  8. +-------+----------+
  9. [root@mysql57 ~]#

//主服务器数据不变

复制代码
  1. [root@mysql53 ~]# mysql -e 'select * from testdb.user'
  2. +------+----------+
  3. | name | password |
  4. +------+----------+
  5. | yaya | 123456 |
  6. +------+----------+

//binlog日志偏移量不变

复制代码
  1. [root@mysql53 ~]# mysql -e 'show master status'
  2. +----------------+----------+--------------+------------------+-------------------+
  3. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  4. +----------------+----------+--------------+------------------+-------------------+
  5. | mysql56.000002 | 4514 | | | |
  6. +----------------+----------+--------------+------------------+-------------------+
  7. [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. //安装解压命令
  4. [root@mycat63 ~]# which unzip || yum -y install unzip
  5. //安装mycat
  6. [root@mycat63 ~]# unzip mycat2-install-template-1.21.zip
  7. [root@mycat63 ~]# mv mycat /usr/local/
  8. //安装依赖
  9. [root@mycat63 ~]# cp mycat2-1.21-release-jar-with-dependencies.jar /usr/local/mycat/lib/
  10. //修改权限
  11. [root@mycat63 ~]# chmod -R 777 /usr/local/mycat/

3)定义客户端连接时使用的用户:

复制代码
  1. [root@mycat63 ~]# vim /usr/local/mycat/conf/users/root.user.json
  2. {
  3. "dialect":"mysql",
  4. "ip":null,
  5. "password":"123", 密码
  6. "transactionType":"proxy",
  7. "username":"mycat" 用户名
  8. }
  9. :wq

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

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

  2. {

  3. "dbType":"mysql",

  4. "idleTimeout":60000,

  5. "initSqls":[],

  6. "initSqlsGetConnection":true,

  7. "instanceType":"READ_WRITE",

  8. "maxCon":1000,

  9. "maxConnectTimeout":3000,

  10. "maxRetryCount":5,

  11. "minCon":1,

  12. "name":"prototypeDs",

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

  14. "type":"JDBC",

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

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

  17. "weight":0

  18. }

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

  20. //创建qq用户

  21. [root@mycat63 ~]# mysql

  22. mysql> create user plj@"%" identified by "123"; 创建用户

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

  24. mysql> exit

  25. Bye

6)启动mycat服务

  1. //查看命令帮助
  2. [root@mycat63 ~]# /usr/local/mycat/bin/mycat help
  3. Usage: /usr/local/mycat/bin/mycat { console | start | stop | restart | status | dump }
  4. //启动mycat服务
  5. [root@mycat63 ~]# /usr/local/mycat/bin/mycat start
  6. Starting mycat2...
  7. //半分钟左右 能看到端口
  8. [root@mycat63 ~]# netstat -utnalp | grep 8066
  9. tcp6 0 0 :::8066 :::* LISTEN 57015/java
  10. [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> create user qq@"%" identified by "123456";
  3. Mysql> grant all on *.* to qq@"%";

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

复制代码
  1. [root@mysql61 ~]# mysql
  2. mysql> create user qq@"%" identified by "123456";
  3. 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 ~]#

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

复制代码
  1. [root@mysql62 ~]# mysql -e 'select user from mysql.user where user="qq"'
  2. +------+
  3. | user |
  4. +------+
  5. | qq|
  6. +------+
  7. [root@host62 ~]#

步骤七:创建集群

连接mycat服务

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

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

复制代码
  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. c0.cluster.json c1.cluster.json prototype.cluster.json
  3. [root@mycat63 ~]#

三、测试配置

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

步骤一:建库

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

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

2)建库

复制代码
  1. mysql> create database tarena;
  2. mysql> exit
  1. 配置文件存放位置
复制代码
  1. [root@mycat63 ~]# ls /usr/local/mycat/conf/schemas/tarena.schema.json
  2. /usr/local/mycat/conf/schemas/tarena.schema.json
  3. [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

2)建表

复制代码
  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. +--------------------+
  3. | Database |
  4. +--------------------+
  5. | information_schema |
  6. | mysql |
  7. | performance_schema |
  8. | sys |
  9. | tarena |
  10. | tarena_0 |
  11. +--------------------+
  12. [root@mysql59 ~]# mysql -e 'use tarena_0 ; show tables' //看表
  13. +--------------------+
  14. | Tables_in_tarena_0 |
  15. +--------------------+
  16. | employees_0 |
  17. +--------------------+
  18. [root@host61 ~]#

4)在60主机查看

复制代码
  1. [root@mysql60 ~]# mysql -e 'show databases' 看库
  2. +--------------------+
  3. | Database |
  4. +--------------------+
  5. | information_schema |
  6. | mysql |
  7. | performance_schema |
  8. | sys |
  9. | tarena |
  10. | tarena_0 |
  11. +--------------------+
  12. [root@mysql60 ~]# mysql -e 'use tarena_0 ; show tables' 看表
  13. +--------------------+
  14. | Tables_in_tarena_0 |
  15. +--------------------+
  16. | employees_0 |
  17. +--------------------+
  18. [root@host62 ~]#

5)在61主机查看库和表

复制代码
  1. [root@mysql61 ~]# mysql -e 'show databases' 看库
  2. +--------------------+
  3. | Database |
  4. +--------------------+
  5. | information_schema |
  6. | mysql |
  7. | performance_schema |
  8. | sys |
  9. | tarena |
  10. | tarena_1 |
  11. +--------------------+
  12. [root@mysql61 ~]# mysql -e 'use tarena_1;show tables' 看表
  13. +--------------------+
  14. | Tables_in_tarena_1 |
  15. +--------------------+
  16. | employees_1 |
  17. +--------------------+
  18. [root@host63 ~]#

6)在62主机查看库和表

复制代码
  1. [root@mysql62 ~]# mysql -e 'show databases' 看库
  2. +--------------------+
  3. | Database |
  4. +--------------------+
  5. | information_schema |
  6. | mysql |
  7. | performance_schema |
  8. | sys |
  9. | tarena |
  10. | tarena_1 |
  11. +--------------------+
  12. [root@mysql62 ~]# mysql -e 'use tarena_1;show tables' 看表
  13. +--------------------+
  14. | Tables_in_tarena_1 |
  15. +--------------------+
  16. | employees_1 |
  17. +--------------------+
  18. [root@host64 ~]#

存储数据

复制代码
  1. [root@client50 ~]# mysql -h192.168.88.63 -P8066 -umycat --p654321
  2. mysql> insert into tarena.employees values (9,"jim","1","jim@163.com");
  3. mysql> insert into tarena.employees values (8,"tom","3","tom@QQ.com");
  4. mysql> insert into tarena.employees values (7,"lucy","2","lucy@QQ.com");
  5. mysql> insert into tarena.employees values (6,"john","2","john@QQ.com");

查看数据

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

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

复制代码
  1. 查看master服务器数据
  2. [root@mysql59 ~]# mysql -e 'select * from tarena_0.employees_0'
  3. +-------------+------+---------+----------+
  4. | employee_id | name | dept_id | mail |
  5. +-------------+------+---------+----------+
  6. | 6 | jim | 2 | jim@QQ.com |
  7. | 8 | tom | 3 | tom@QQ.com |
  8. +-------------+------+---------+----------+
  9. [root@mysql59 ~]#
  10. 查看slave服务器数据
  11. [root@mysql60 ~]# mysql -e 'select * from tarena_0.employees_0'
  12. +-------------+------+---------+----------+
  13. | employee_id | name | dept_id | mail |
  14. +-------------+------+---------+----------+
  15. | 6 | jim | 2 | jim@QQ.com |
  16. | 8 | tom | 3 | tom@QQ.com |
  17. +-------------+------+---------+----------+
  18. [root@mysql60 ~]#

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

复制代码
  1. 查看master服务器数据
  2. [root@mysql61 ~]# mysql -e 'select * from tarena_1.employees_1'
  3. +-------------+------+---------+-----------+
  4. | employee_id | name | dept_id | mail |
  5. +-------------+------+---------+-----------+
  6. | 7 | lucy | 2 | lucy@QQ.com |
  7. | 9 | john | 1 | john@163.com |
  8. +-------------+------+---------+-----------+
  9. [root@mysql61 ~]#
  10. 查看slave服务器数据
  11. [root@mysql62 ~]# mysql -e 'select * from tarena_1.employees_1'
  12. +-------------+------+---------+-----------+
  13. | employee_id | name | dept_id | mail |
  14. +-------------+------+---------+-----------+
  15. | 7 | lucy | 2 | lucy@QQ.com |
  16. | 9 | john | 1 | john@163.com |
  17. +-------------+------+---------+-----------+
  18. [root@mysql62 ~]#

练习ER表

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

1)连接mycat服务建表

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

2)建表

复制代码
  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. +--------------------+
  4. | Tables_in_tarena_0 |
  5. +--------------------+
  6. | employees_0 |
  7. | salary_0 |
  8. +--------------------+
  9. [root@mysql59 ~]#
  10. 在c1集群master服务器查看表
  11. [root@mysql61 ~]# mysql -e 'use tarena_1;show tables'
  12. +--------------------+
  13. | Tables_in_tarena_1 |
  14. +--------------------+
  15. | employees_1 |
  16. | salary_1 |
  17. +--------------------+
  18. [root@mysql61~]#

5)客户端连接mycat服务并插入数据

复制代码
  1. [root@client50 ~]# mysql -h192.168.88.63 -P8066 -umycat --p654321
  2. mysql> insert into tarena.salary values(6,20230110,20000,2000);
  3. mysql> insert into tarena.salary values(7,20230210,25000,2500);
  4. mysql> insert into tarena.salary values(8,20230310,30000,3000);
  5. mysql> insert into tarena.salary values(9,20230410,35000,3500);

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

复制代码
  1. [root@mysql59 ~]# mysql -e 'select * from tarena_0.employees_0'
  2. +-------------+------+---------+----------+
  3. | employee_id | name | dept_id | mail |
  4. +-------------+------+---------+----------+
  5. | 6 | C | 2 | c@QQ.com |
  6. | 8 | B | 3 | B@QQ.com |
  7. +-------------+------+---------+----------+
  8. [root@mysql60 ~]# mysql -e 'select * from tarena_0.salary_0'
  9. +-------------+------------+-------+-------+
  10. | employee_id | p_date | basic | bonus |
  11. +-------------+------------+-------+-------+
  12. | 6 | 2023-01-10 | 20000 | 2000 |
  13. | 8 | 2023-03-10 | 30000 | 3000 |
  14. +-------------+------------+-------+-------+
  15. [root@mysql61 ~]# mysql -e 'select * from tarena_1.employees_1'
  16. +-------------+------+---------+-----------+
  17. | employee_id | name | dept_id | mail |
  18. +-------------+------+---------+-----------+
  19. | 7 | C | 2 | c@QQ.com |
  20. | 9 | a | 1 | a@163.com |
  21. +-------------+------+---------+-----------+
  22. [root@mysql62 ~]# mysql -e 'select * from tarena_1.salary_1'
  23. +-------------+------------+-------+-------+
  24. | employee_id | p_date | basic | bonus |
  25. +-------------+------------+-------+-------+
  26. | 7 | 2023-02-10 | 25000 | 2500 |
  27. | 9 | 2023-04-10 | 35000 | 3500 |
  28. +-------------+------------+-------+-------+
  29. [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. +---------+-----------+
  3. | dept_id | dept_name |
  4. +---------+-----------+
  5. | 1 | 开发部 |
  6. | 2 | 运维部 |
  7. | 3 | 测试部 |
  8. +---------+-----------+
  9. [root@mysql60 ~]# mysql -e 'select * from tarena.dept'
  10. +---------+-----------+
  11. | dept_id | dept_name |
  12. +---------+-----------+
  13. | 1 | 开发部 |
  14. | 2 | 运维部 |
  15. | 3 | 测试部 |
  16. +---------+-----------+
  17. [root@mysql61 ~]# mysql -e 'select * from tarena.dept'
  18. +---------+-----------+
  19. | dept_id | dept_name |
  20. +---------+-----------+
  21. | 1 | 开发部 |
  22. | 2 | 运维部 |
  23. | 3 | 测试部 |
  24. +---------+-----------+
  25. [root@mysql62 ~]# mysql -e 'select * from tarena.dept'
  26. +---------+-----------+
  27. | dept_id | dept_name |
  28. +---------+-----------+
  29. | 1 | 开发部 |
  30. | 2 | 运维部 |
  31. | 3 | 测试部 |
相关推荐
计算机科研之友(Friend)2 分钟前
计算机视觉(一)—— 特刊推荐
数据库·人工智能·计算机网络·搜索引擎·计算机视觉
NineData5 分钟前
K1计划100%收购 MariaDB; TDSQL成为腾讯云核心战略产品; Oracle@AWS/Google/Azure发布
数据库·oracle·腾讯云·mariadb·azure·amazon·tdsql
金牌服务刘11 分钟前
如何选择合适的数据报表工具?
数据库·微服务·信息可视化·容器·数据挖掘·数据分析·负载均衡
一个风轻云淡19 分钟前
TDSQL数据库介绍
数据库
Gabriel_wei33 分钟前
宝塔面板FTP连接时“服务器发回了不可路由的地址。使用服务器地址代替。”
运维·服务器·ftp·宝塔
丁总学Java1 小时前
mysql-搭建主从复制
数据库·mysql·adb
大鹅i3 小时前
Grafana面板-linux主机详情(使用标签过滤主机监控)
linux·运维·grafana
阿里云大数据AI技术3 小时前
阿里云 EMR StarRocks 在七猫的应用和实践
大数据·阿里云·云计算·emr
Flying_Fish_roe3 小时前
AWS Lambda 与 Java
java·云计算·aws
程序者王大川3 小时前
【物联网】时序数据库InfluxDB解析及1.x版本与2.x版本区别详解
java·数据库·物联网·数据采集·时序数据库·数据·工业互联网平台