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);
#可以多次增量备份.后面几天的数据,看看效果.
步骤二:练习数据增量恢复
增量恢复数据步骤:
- 准备恢复数据
- 合并数据
- 清空数据库目录
- 拷贝数据
- 修改数据库目录所有者/组用户为mysql
- 重启数据库服务
把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
#清空数据库目录,测试数据情况
清空数据库目录
- [root@mysql51 ~]# rm -rf /var/lib/mysql/*
拷贝数据
#拷贝的是,上一部分合并在 fullback 的目录
- [root@mysql51 ~]# xtrabackup --copy-back --target-dir=/fullbak
修改数据库目录所有者/组用户为mysql
- [root@mysql51 ~]# chown -R mysql:mysql /var/lib/mysql
重启数据库服务
- [root@mysql51 ~]# systemctl restart mysqld
连接服务查看数据
- [root@mysql51 ~]# mysql -uroot -p123
- mysql> select count(*) from tarena.salary where date=20230710;
差异备份
差异备份:备份完全备份后 ,新产生的数据。
#使用场景广泛,备份速度快,节省数据库空间。
步骤一:
周一完全备份
- [root@mysql50 ~]# xtrabackup --host=127.0.0.1 --user=root --password=123 --backup --target-dir=/allbak --datadir=/var/lib/mysql
- //插入新数据 (可以插入多行)
- mysql> insert into tarena.salary(date,employee_id,basic,bonus)values("20230810",18,25000,8000);
#周二差异备份,备份周一备份后新产生的数据
- [root@mysql50 ~]# xtrabackup --host=127.0.0.1 --user=root --password=123 --backup --target-dir=/dir2 --incremental-basedir=/allbak --datadir=/var/lib/mysql
- //插入新数据 (可以插入多行)
- mysql> insert into tarena.salary(date,employee_id,basic,bonus)values("20230810",18,25000,8000);
步骤二:练习差异恢复
差异恢复数据步骤:
- 准备恢复数据
- 合并数据
- 清空数据库目录
- 拷贝数据
- 修改数据库目录所有者/组用户为mysql
- 重启数据库服务
具体操作如下:
把MySQL50的备份文件拷贝给MySQL51
- [root@mysql50 ~]# scp --r /allbak root@192.168.88.51:/root/ 周一完全备份
- [root@mysql50 ~]# scp --r /dir2 root@192.168.88.51:/root/ #周二的差异备份
准备恢复数据
- [root@mysql51 ~]# xtrabackup --prepare --apply-log-only --target-dir=/root/allbak
合并数据
#将差异备份的的周一周二内容合并
- [root@mysql51 ~]# xtrabackup --prepare --apply-log-only --target-dir=/root/allbak \
- --incremental-dir=/root/dir2
#清空数据库目录,并测试
清空数据库目录
- [root@mysql51 ~]# rm -rf /var/lib/mysql/*
拷贝数据
- [root@mysql51 ~]# xtrabackup --copy-back --target-dir=/root/allbak
修改数据库目录所有者/组用户为mysql
- [root@mysql51 ~]# chown -R mysql:mysql /var/lib/mysql
重启数据库服务
- [root@mysql51 ~]# systemctl restart mysqld
连接服务查看数据
- [root@mysql51 ~]# mysql -uroot -p123
- mysql> select count(*) from tarena.salary where date=20230810;
二.binlog日志
环境准备
- yum --y install mysql mysql-server
- 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
登陆服务
- [root@mysql52 ~]# mysql
查看日志信息
Mysql> show master status ;
- +----------------+----------+--------------+------------------+-------------------+
- | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
- +----------------+----------+--------------+------------------+-------------------+
- | mysql52.000001 | 156 | | | |
- +----------------+----------+--------------+------------------+-------------------+
说明:默认日志文件容量大于1G时会自动创建新的日志文件,在日志文件没写满时,执行的所有写命令都会保存到当前使用的日志文件里。
注意点:
//只要服务重启就会创建新日志
/完全备份后创建新的日志文件,创建的日志个数和备份库的个数一致
步骤四:练习日志相关命令的使用
sql//查看已有的日志文件 mysql> show binary logs; 日志文件名 日志大小(字节) 加密(no/yes) +----------------+-----------+-----------+ | Log_name | File_size | Encrypted | +----------------+-----------+-----------+ | mysql52.000001 | 201 | No | +----------------+-----------+-----------+ 7 rows in set (0.00 sec)
//查看正在使用的日志
sqlmysql> show master status; +----------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +----------------+----------+--------------+------------------+-------------------+ | mysql52.000002 | 156 | | | | +----------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
//插入记录
sqlmysql> 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)
//删除日志文件名之前的所有日志文件
mysql> purge master logs to "mysql52.000003";
//删除所有日志文件,并重新创建日志文件mysql> reset master;
步骤五:使用日志恢复数据
1)在mysql52主机执行如下操:
//重置日志
- mysql> reset master;
//查看日志
- mysql> show master status;
- +----------------+----------+--------------+------------------+-------------------+
- | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
- +----------------+----------+--------------+------------------+-------------------+
- | mysql52.000001 | 156 | | | |
- +----------------+----------+--------------+------------------+-------------------+
- 1 row in set (0.00 sec)
//建库
- mysql> create database gamedb;
//建表
mysql> create table gamedb.t1(name char(10),class char(3));
//插入记录
- mysql> insert into gamedb.t1 values ("yaya","nsd");
- mysql> insert into gamedb.t1 values ("yaya","nsd");
- mysql> insert into gamedb.t1 values ("yaya","nsd");
//查看表记录
- mysql> select * from gamedb.t1;
- +------+-------+
- | name | class |
- +------+-------+
- | yaya | nsd |
- | yaya | nsd |
- | yaya | nsd |
- +------+-------+
- 3 rows in set (0.00 sec)
- mysql> exit
//把日志文件拷贝给恢复数据的服务器,比如 mysql50
sql[root@mysql52 ~]# scp /mylog/mysql52.000001 root@192.168.88.50:/root/
2)在MySQL50 使用日志恢复数据
//查看日志
[root@mysql50 ~]# ls /root/mysql52.000001
- /root/mysql52.000001
//执行日志恢复数据
- [root@mysql50 ~]# mysqlbinlog /root/mysql52.000001 | mysql -uroot -p123
//连接服务查看数据
- [root@mysql50 ~]# mysql -uroot -p123 -e 'select * from gamedb.t1'
- mysql: [Warning] Using a password on the command line interface can be insecure.
- +------+-------+
- | name | class |
- +------+-------+
- | yaya | nsd |
- | yaya | nsd |
- | yaya | nsd |
- +------+-------+
DBA day06
环境准备:
#有同步工具的,可以把两台虚拟机一起装包启动服务。
一、MySQL主从同步
原理:
形式介绍
第一种是老大和小弟的关系;第二种是两兄弟,第三种是多保险,多个数据备份的数据库,避免出现意外数据库宕机,单一从数据库恢复难度大的问题。
还有主--从---从关系。通俗来说,就是老大的老大,和小弟的小弟的关系。
好比 :老大下完命令,老二传达,最后老二的小弟执行命令。
理论结束,时间开始。(*^▽^*)
步骤一:把192.168.88.53配置为master服务器
- [root@mysql53 ~]# vim /etc/my.cnf.d/mysql-server.cnf
- [mysqld]
- server-id=53
- log-bin=mysql53
- :wq
- [root@mysql53 ~]# systemctl restart mysqld
2)用户授权
- [root@mysql53 ~]# mysql
- mysql> create user repluser@"%" identified by "123";
- mysql> grant replication slave on *.* to repluser@"%";
查看日志信息
- mysql> show master status;
- +----------------+----------+--------------+------------------+-------------------+
- | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
- +----------------+----------+--------------+------------------+-------------------+
- | mysql53.000001 | 667 | | | |
- +----------------+----------+--------------+------------------+-------------------+
- 1 row in set (0.00 sec)
步骤二:把192.168.88.54配置为slave服务器
1)指定server-id 并重启数据库服务
- [root@mysql54 ~]# vim /etc/my.cnf.d/mysql-server.cnf
- [mysqld]
- server-id=54
- :wq
- [root@mysql54 ~]# systemctl restart mysqld
2)登陆服务指定主服务器信息 #和主机信息对应上
- [root@mysql54 ~]# mysql
- mysql> change master to
- master_host="0.0.0.0", #任何用户都可以访问
- master_user="repluser",
- master_password="123",
- master_log_file="mysql53.000001",
- master_log_pos=667;
//启动slave进程
- mysql> start slave ;
//查看状态信息
#主要看IO/SQL 线程 是否为yes状态
sqlmysql> 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服务器建库 、建表、添加用户 、授权
- [root@mysql53 ~]# mysql 连接服务
- mysql> create database gamedb; 建库
- mysql> create table gamedb.user(name char(10) , class char(3)); 建表
- mysql> create user dc@"%" identified by "123"; 创建用户
- mysql> grant select,insert,update,delete on gamedb.* to dc@"%" ; 授予权限
- 在slave服务器查看库、表、用户 #和master的用户对应上
- [root@mysql54 ~]# mysql 连接服务
- mysql> show databases; 查看库
- mysql> desc gamedb.user; 查看表头
- mysql> select user from mysql.user where user="dc"; 查看用户
- mysql> show grants for dc@"%" ; 查看权限
3)客户端连接从服务器查看数据
- [root@mysql50 ~]# mysql -h192.168.88.54 -usf -p123
- Mysql> select * from gamedb.user;
- +------+-------+
- | name | class |
- +------+-------+
- | yaya | nsd |
- +------+-------+
- Mysql>
二、数据读写分离
在主从结构基础上,增加读写分离功能。
初步结构是 1个MySQL53个主,1个MySQL54从服务器,1个mysql50客户端服务器。
#就不去配置多的虚拟机测试环境了,免得你们看不过来。但是主从同步配置得弄懂。
#mycat 属于中间件软件的一种。
配置mycat服务器
#在客户端服务器上,下载并配置mycat
//安装jdk
- [root@mycat50 upload]# yum -y install java-1.8.0-openjdk.x86_64
//安装解压命令
- [root@mycat50 upload]# which unzip || yum -y install unzip
//安装mycat
- [root@mycat50 upload]# unzip mycat2-install-template-1.21.zip
- [root@mycat50 upload]# mv mycat /usr/local/
//安装依赖
- [root@mycat50 upload]# cp mycat2-1.21-release-jar-with-dependencies.jar /usr/local/mycat/lib/
//修改权限
- [root@mycat50 upload]# chmod u+x /usr/local/mycat/
3)定义客户端连接mycat服务使用用户及密码:
- [root@mycat58 ~]# vim /usr/local/mycat/conf/users/root.user.json
- {
- "dialect":"mysql",
- "ip":null,
- "password":"123", 密码
- "transactionType":"proxy",
- "username":"mycat" 用户名
- }
- :wq
4)定义连接的数据库服务器
- [root@mycat50 ~]# vim /usr/local/mycat/conf/datasources/prototypeDs.data
- {
- "dbType":"mysql",
- "idleTimeout":60000,
- "initSqls":[],
- "initSqlsGetConnection":true,
- "instanceType":"READ_WRITE",
- "maxCon":1000,
- "maxConnectTimeout":3000,
- "maxRetryCount":5,
- "minCon":1,
- "name":"prototypeDs",
- "password":"123", #密码
- "type":"JDBC",
- "url":"jdbc:mysql://localhost:3306/mysql?useUnicode=true&serverTimezone=Asia/Shanghai&characterEncoding=UTF-8", 连接本机的数据库服务
- "user":"dc", #用户名
- "weight":0
- }
- :wq
#启动 mysql服务 #创建mysql时用户与授权与配置的一致。
启动mycat服务
- //查看帮助
- [root@mycat50 ~]# /usr/local/mycat/bin/mycat help
- Usage: /usr/local/mycat/bin/mycat { console | start | stop | restart | status | dump }
- //启动服务
- [root@mycat50 ~]# /usr/local/mycat/bin/mycat start
- Starting mycat2...
- //半分钟左右 能看到端口
- [root@mycat50 ~]# netstat -utnlp | grep 8066
- tcp6 0 0 :::8066 :::* LISTEN 57015/java
- [root@mycat50 ~]#
连接mycat服务
- [root@mycat50 ~]# mysql -h127.0.0.1 -P8066 -umycat -p123
- mysql> show databases;
- +--------------------+
- | `Database` |
- +--------------------+
- | information_schema |
- | mysql |
- | performance_schema |
- +--------------------+
- 3 rows in set (0.11 sec)
- 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" }*/;
- //查看数据源
- mysql> /*+mycat:showDataSources{}*/ \G
- *************************** 1. row ***************************
- NAME: whost53
- USERNAME: dca
- PASSWORD: 123
- MAX_CON: 1000
- MIN_CON: 1
- EXIST_CON: 0
- USE_CON: 0
- MAX_RETRY_COUNT: 5
- MAX_CONNECT_TIMEOUT: 30000
- DB_TYPE: mysql
- URL: jdbc:mysql://192.168.88.53:3306?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true
- WEIGHT: 0
- INIT_SQL:
- INIT_SQL_GET_CONNECTION: true
- INSTANCE_TYPE: READ_WRITE
- IDLE_TIMEOUT: 60000
- DRIVER: {
- CreateTime:"2023-06-02 17:01:14",
- ActiveCount:0,
- PoolingCount:0,
- CreateCount:0,
- DestroyCount:0,
- CloseCount:0,
- ConnectCount:0,
- Connections:[
- ]
- }
- TYPE: JDBC
- IS_MYSQL: true
- *************************** 2. row ***************************
- NAME: rhost54
- USERNAME: dca
- PASSWORD: 123
- MAX_CON: 1000
- MIN_CON: 1
- EXIST_CON: 0
- USE_CON: 0
- MAX_RETRY_COUNT: 5
- MAX_CONNECT_TIMEOUT: 30000
- DB_TYPE: mysql
- URL: jdbc:mysql://192.168.88.54:3306?useUnicode=true&serverTimezone=Asia/Shanghai&characterEncoding=UTF-8&autoReconnect=true
- WEIGHT: 0
- INIT_SQL:
- INIT_SQL_GET_CONNECTION: true
- INSTANCE_TYPE: READ_WRITE
- IDLE_TIMEOUT: 60000
- DRIVER: {
- CreateTime:"2023-06-02 17:01:14",
- ActiveCount:0,
- PoolingCount:0,
- CreateCount:0,
- DestroyCount:0,
- CloseCount:0,
- ConnectCount:0,
- Connections:[
- ]
- }
- TYPE: JDBC
- IS_MYSQL: true
- *************************** 3. row ***************************
- NAME: prototypeDs #原型库
- USERNAME: dc
- PASSWORD: 123
- MAX_CON: 1000
- MIN_CON: 1
- EXIST_CON: 0
- USE_CON: 0
- MAX_RETRY_COUNT: 5
- MAX_CONNECT_TIMEOUT: 3000
- DB_TYPE: mysql
- URL: jdbc:mysql://localhost:3306/mysql?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true
- WEIGHT: 0
- INIT_SQL:
- INIT_SQL_GET_CONNECTION: true
- INSTANCE_TYPE: READ_WRITE
- IDLE_TIMEOUT: 60000
- DRIVER: {
- CreateTime:"2023-06-02 17:01:14",
- ActiveCount:0,
- PoolingCount:0,
- CreateCount:0,
- DestroyCount:0,
- CloseCount:0,
- ConnectCount:0,
- Connections:[
- ]
- }
- TYPE: JDBC
- IS_MYSQL: true
- 3 rows in set (0.00 sec)
- mysql>exit //断开连接
- //添加的数据源以文件的形式保存在安装目录下
- [root@mycat50 conf]# ls /usr/local/mycat/conf/datasources/
- prototypeDs.datasource.json rhost54.datasource.json whost53.datasource.json
- [root@mycat50 conf]#
#测试主从同步,与读写分离情况
2)配置数据库服务器添加qqa用户
//在master服务器添加
- [root@mysql53 ~]# mysql
- mysql> create user qqa@"%" identified by "123456";
- mysql> grant all on *.* to qqa@"%";
- mysql>exit
//在slave服务器查看是否同步成功
- [root@mysql54 ~]# mysql -e 'select user , host from mysql.user where user="qqa"'
- +------+------+
- | user | host |
- +------+------+
- | plja | % |
- +------+------+
- [root@mysql54 ~]#
3)创建集群
//连接mycat服务
- [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>
//创建的集群, 会以文件的形式保存在目录下
- [root@mycat50 conf]#ls /usr/local/mycat/conf/clusters/
- prototype.cluster.json rwcluster.cluster.json
4)指定主机角色
//修改master角色主机仅负责写访问
- [root@mycat50 ~]# vim /usr/local/mycat/conf/datasources/whost56.datasource.json
- {
- "dbType":"mysql",
- "idleTimeout":60000,
- "initSqls":[],
- "initSqlsGetConnection":true,
- "instanceType":"WRITE", #仅负责写访问
- "logAbandoned":true,
- "maxCon":1000,
- "maxConnectTimeout":30000,
- "maxRetryCount":5,
- "minCon":1,
- "name":"whost53",
- "password":"123",
- "queryTimeout":0,
- "removeAbandoned":false,
- "removeAbandonedTimeoutSecond":180,
- "type":"JDBC",
- "url":"jdbc:mysql://192.168.88.53:3306?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true",
- "user":"qqa",
- "weight":0
- }
- :wq
//修改slave角色主机仅负责读访问
- [root@mycat50 ~]# vim /usr/local/mycat/conf/datasources/rhost57.datasource.json
- {
- "dbType":"mysql",
- "idleTimeout":60000,
- "initSqls":[],
- "initSqlsGetConnection":true,
- "instanceType":"READ", #仅负责读访问
- "logAbandoned":true,
- "maxCon":1000,
- "maxConnectTimeout":30000,
- "maxRetryCount":5,
- "minCon":1,
- "name":"rhost54",
- "password":"123",
- "queryTimeout":0,
- "removeAbandoned":false,
- "removeAbandonedTimeoutSecond":180,
- "type":"JDBC",
- "url":"jdbc:mysql://192.168.88.54:3306?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true",
- "user":"qqa",
- "weight":0
- }
- :wq
5)修改读策略
#类似负载均衡设置
- [root@mycat50 ~]# vim /usr/local/mycat/conf/clusters/rwcluster.cluster.json
- {
- "clusterType":"MASTER_SLAVE",
- "heartbeat":{
- "heartbeatTimeout":1000,
- "maxRetryCount":3,
- "minSwitchTimeInterval":300,
- "showLog":false,
- "slaveThreshold":0.0
- },
- "masters":[
- "whost53"
- ],
- "maxCon":2000,
- "name":"rwcluster",
- "readBalanceType":"BALANCE_ALL_READ", #把读访问平均分配给read角色的主机
- "replicas":[
- "rhost54"
- ],
- "switchType":"SWITCH"
- }
- :wq
//重启mycat服务
- [root@mycat50 ~]# /usr/local/mycat/bin/mycat restart
- Stopping mycat2...
- Stopped mycat2.
- Starting mycat2...
步骤四:测试配置
思路如下:
- 连接mycat服务建库
- 指定存储数据使用的集群
- 连接mycat服务建表
- 客户端连接mycat服务执行select 或 insert
具体操作如下:
//连接mycat服务
- [root@mycat50~]# mysql -h127.0.0.1 -P8066 -umycat -p123
//创建存储数据的库
- mysql> create database testdb;
- mysql> exit
- Bye
//指定库存储数据使用的集群
- [root@mycat50~]# vim /usr/local/mycat/conf/schemas/testdb.schema.json
- {
- "customTables":{},
- "globalTables":{},
- "normalProcedures":{},
- "normalTables":{},
- "schemaName":"testdb",
- "targetName":"rwcluster", 添加此行(之前创建的集群名rwcluster)
- "shardingTables":{},
- "views":{}
- }
- :wq
//重启mycat服务
- [root@mycat50 ~]# /usr/local/mycat/bin/mycat restart
- Stopping mycat2...
- Stopped mycat2.
- Starting mycat2...
测试读写分离
//在slave服务器本机插入记录,使其与master服务器的数据不一样
- [root@mysql54 ~]# mysql -e 'insert into testdb.user values ("yayaA","654321")'
- [root@mysql54~]# mysql -e 'select * from testdb.user'
- +-------+----------+
- | name | password |
- +-------+----------+
- | yaya | 123456 |
- | yayaA | 654321 |
- +-------+----------+
- [root@mysql57 ~]#
//主服务器数据不变
- [root@mysql53 ~]# mysql -e 'select * from testdb.user'
- +------+----------+
- | name | password |
- +------+----------+
- | yaya | 123456 |
- +------+----------+
//binlog日志偏移量不变
- [root@mysql53 ~]# mysql -e 'show master status'
- +----------------+----------+--------------+------------------+-------------------+
- | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
- +----------------+----------+--------------+------------------+-------------------+
- | mysql56.000002 | 4514 | | | |
- +----------------+----------+--------------+------------------+-------------------+
- [root@mysql53 ~]#
DBA Day07
环境准备:
一、数据分片概述
二、部署mycat服务
分片规则
步骤一:把MySQL60配置为MySQL59的从服务器
#具体操作,看 Day06 MySQL主从同步设置
步骤二:把MySQL62配置为MySQL61的从服务器
#具体操作,看 Day06 MySQL主从同步设置
步骤三:把主机mycat63配置为mycat服务器。
安装mycat软件
- //安装jdk
- [root@mycat63 ~]# yum -y install java-1.8.0-openjdk.x86_64
- //安装解压命令
- [root@mycat63 ~]# which unzip || yum -y install unzip
- //安装mycat
- [root@mycat63 ~]# unzip mycat2-install-template-1.21.zip
- [root@mycat63 ~]# mv mycat /usr/local/
- //安装依赖
- [root@mycat63 ~]# cp mycat2-1.21-release-jar-with-dependencies.jar /usr/local/mycat/lib/
- //修改权限
- [root@mycat63 ~]# chmod -R 777 /usr/local/mycat/
3)定义客户端连接时使用的用户:
- [root@mycat63 ~]# vim /usr/local/mycat/conf/users/root.user.json
- {
- "dialect":"mysql",
- "ip":null,
- "password":"123", 密码
- "transactionType":"proxy",
- "username":"mycat" 用户名
- }
- :wq
4)定义连接的数据库服务
[root@mycat63 ~]# vim /usr/local/mycat/conf/datasources/prototypeDs.data.json
{
"dbType":"mysql",
"idleTimeout":60000,
"initSqls":[],
"initSqlsGetConnection":true,
"instanceType":"READ_WRITE",
"maxCon":1000,
"maxConnectTimeout":3000,
"maxRetryCount":5,
"minCon":1,
"name":"prototypeDs",
"password":"123", 密码
"type":"JDBC",
"url":"jdbc:mysql://localhost:3306/mysql?useUnicode=true&serverTimezone=Asia/Shanghai&characterEncoding=UTF-8", 连接本机的数据库服务
"user":"qq", #直接设置用户名,其他不用管
"weight":0
}
:wq
5)在mycat63主机运行数据库服务//创建qq用户
[root@mycat63 ~]# mysql
mysql> create user plj@"%" identified by "123"; 创建用户
mysql> grant all on *.* to qq@"%" ; 授予权限
mysql> exit
Bye
6)启动mycat服务
- //查看命令帮助
- [root@mycat63 ~]# /usr/local/mycat/bin/mycat help
- Usage: /usr/local/mycat/bin/mycat { console | start | stop | restart | status | dump }
- //启动mycat服务
- [root@mycat63 ~]# /usr/local/mycat/bin/mycat start
- Starting mycat2...
- //半分钟左右 能看到端口
- [root@mycat63 ~]# netstat -utnalp | grep 8066
- tcp6 0 0 :::8066 :::* LISTEN 57015/java
- [root@mycat63 ~]#
步骤四:连接mycat服务器
1)连接本机的mycat服务
- [root@mycat63 ~]# mysql -h127.0.0.1 -P8066 -umycat -p123
步骤五:添加数据源
- //添加MySQL59主机
- MySQL>/*+ mycat:createdatasource{
- "name":"dw0",
- "url":"jdbc:mysql://192.168.88.59:3306",
- "user":"qq",
- "password":"123"
- }*/;
- //添加MySQL60主机
- Mysql>/*+ mycat:createdatasource{
- "name":"dr0",
- "url":"jdbc:mysql://192.168.88.60:3306",
- "user":"qq",
- "password":"123"
- }*/;
- //添加MySQL61主机
- Mysql>/*+ mycat:createdatasource{
- "name":"dw1",
- "url":"jdbc:mysql://192.168.88.61:3306",
- "user":"qq",
- "password":"123"
- }*/;
- //添加MySQL62主机
- Mysql>/*+ mycat:createdatasource{
- "name":"dr1",
- "url":"jdbc:mysql://192.168.88.62:3306",
- "user":"qq",
- "password":"123"
- }*/;
- Mysql>
2)查看数据信息
- mysql> /*+mycat:showDataSources{}*/ \G
#显示所有 服务器配置情况即可
步骤六:配置数据库服务器
//在主服务器 MySQL59 主机添加qq用户
- [root@mysql59 ~]# mysql
- mysql> create user qq@"%" identified by "123456";
- Mysql> grant all on *.* to qq@"%";
//在主服务器 MySQL61 主机添加qq用户
- [root@mysql61 ~]# mysql
- mysql> create user qq@"%" identified by "123456";
- Mysql> grant all on *.* to qq@"%";
//在从服务器MySQL60 查看用户
- [root@mysql60 ~]# mysql -e 'select user from mysql.user where user="qq" '
- | user |
- +------+
- | qq |
- +------+
- [root@mysql60 ~]#
//在从服务器MySQL62 查看用户
- [root@mysql62 ~]# mysql -e 'select user from mysql.user where user="qq"'
- +------+
- | user |
- +------+
- | qq|
- +------+
- [root@host62 ~]#
步骤七:创建集群
连接mycat服务
- [root@mycat63 ~]# mysql -h127.0.0.1 -P8066 -umycat -p123
2)创建第1个集群 #不用敲,直接复制粘贴。懂得原理即可
- mysql>/*!mycat:createcluster{
- "name":"c0",
- "masters":["dw0"],
- "replicas":["dr0"]
- }*/;
3)创建第2个集群 #不用敲,直接复制粘贴。懂得原理即可
- mysql>/*!mycat:createcluster{
- "name":"c1",
- "masters":["dw1"],
- "replicas":["dr1"]
- }*/;
- Mysql>
4)查看集群信息
- mysql> /*+ mycat:showClusters{}*/ \G
- *************************** 1. 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. row ***************************
- NAME: c0
- SWITCH_TYPE: SWITCH
- MAX_REQUEST_COUNT: 2000
- TYPE: BALANCE_ALL
- WRITE_DS: dw0
- READ_DS: dw0,dr0
- WRITE_L: io.mycat.plug.loadBalance.BalanceRandom$1
- READ_L: io.mycat.plug.loadBalance.BalanceRandom$1
- AVAILABLE: true
- *************************** 3. row ***************************
- NAME: c1
- SWITCH_TYPE: SWITCH
- MAX_REQUEST_COUNT: 2000
- TYPE: BALANCE_ALL
- WRITE_DS: dw1
- READ_DS: dw1,dr1
- WRITE_L: io.mycat.plug.loadBalance.BalanceRandom$1
- READ_L: io.mycat.plug.loadBalance.BalanceRandom$1
- AVAILABLE: true
- 3 rows in set (0.03 sec)
- mysql>
5)创建的集群保存在mycat安装目录下
- [root@MySQL63 ~]# ls /usr/local/mycat/conf/clusters/
- c0.cluster.json c1.cluster.json prototype.cluster.json
- [root@mycat63 ~]#
三、测试配置
在客户端client50 连接mycat63 存储数据 ,验证mycat63的配置
步骤一:建库
1)在mycat63 连接本机的mycat服务
- [root@mycat63 ~]# mysql -h127.0.0.1 -umycat -p654321 -P8066
2)建库
- mysql> create database tarena;
- mysql> exit
- 配置文件存放位置
- [root@mycat63 ~]# ls /usr/local/mycat/conf/schemas/tarena.schema.json
- /usr/local/mycat/conf/schemas/tarena.schema.json
- [root@mycat63 ~]#
练习分片表
说明:
dbpartition by 定义分库使用的分片规则,
tbpartition by 定义分表使用的分片规则。
mod_hash 分片规则,用employee_id表头的值做取模计算
tbpartitions 表的分片数量
dbpartitions 库的分片数量
1)连接mycat服务
- [root@client50 ~]# mysql -h192.168.88.63 -P8066 -umycat --p654321
2)建表
- create table tarena.employees(
- employee_id int primary key,
- name char(10),dept_id int ,
- mail varchar(30)
- ) default charset utf8
- dbpartition BY mod_hash(employee_id) tbpartition BY mod_hash(employee_id)
- tbpartitions 1 dbpartitions 2;
3)在59主机查看库和表
- [root@mysql59 ~]# mysql -e 'show databases' //看库
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | mysql |
- | performance_schema |
- | sys |
- | tarena |
- | tarena_0 |
- +--------------------+
- [root@mysql59 ~]# mysql -e 'use tarena_0 ; show tables' //看表
- +--------------------+
- | Tables_in_tarena_0 |
- +--------------------+
- | employees_0 |
- +--------------------+
- [root@host61 ~]#
4)在60主机查看
- [root@mysql60 ~]# mysql -e 'show databases' 看库
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | mysql |
- | performance_schema |
- | sys |
- | tarena |
- | tarena_0 |
- +--------------------+
- [root@mysql60 ~]# mysql -e 'use tarena_0 ; show tables' 看表
- +--------------------+
- | Tables_in_tarena_0 |
- +--------------------+
- | employees_0 |
- +--------------------+
- [root@host62 ~]#
5)在61主机查看库和表
- [root@mysql61 ~]# mysql -e 'show databases' 看库
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | mysql |
- | performance_schema |
- | sys |
- | tarena |
- | tarena_1 |
- +--------------------+
- [root@mysql61 ~]# mysql -e 'use tarena_1;show tables' 看表
- +--------------------+
- | Tables_in_tarena_1 |
- +--------------------+
- | employees_1 |
- +--------------------+
- [root@host63 ~]#
6)在62主机查看库和表
- [root@mysql62 ~]# mysql -e 'show databases' 看库
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | mysql |
- | performance_schema |
- | sys |
- | tarena |
- | tarena_1 |
- +--------------------+
- [root@mysql62 ~]# mysql -e 'use tarena_1;show tables' 看表
- +--------------------+
- | Tables_in_tarena_1 |
- +--------------------+
- | employees_1 |
- +--------------------+
- [root@host64 ~]#
存储数据
- [root@client50 ~]# mysql -h192.168.88.63 -P8066 -umycat --p654321
- mysql> insert into tarena.employees values (9,"jim","1","jim@163.com");
- mysql> insert into tarena.employees values (8,"tom","3","tom@QQ.com");
- mysql> insert into tarena.employees values (7,"lucy","2","lucy@QQ.com");
- mysql> insert into tarena.employees values (6,"john","2","john@QQ.com");
查看数据
- mysql> select * from tarena.employees;
- +-------------+------+---------+-----------+
- | employee_id | name | dept_id | mail |
- +-------------+------+---------+-----------+
- | 6 | jim | 2 | jim@QQ.com |
- | 8 | tom | 3 | tom@QQ.com |
- | 7 | lucy | 2 | lucy@QQ.com |
- | 9 | john | 1 | john@163.com |
- +-------------+------+---------+-----------+
- 4 rows in set (2.07 sec)
查看c0集群中 数据库服务器存储的数据
- 查看master服务器数据
- [root@mysql59 ~]# mysql -e 'select * from tarena_0.employees_0'
- +-------------+------+---------+----------+
- | employee_id | name | dept_id | mail |
- +-------------+------+---------+----------+
- | 6 | jim | 2 | jim@QQ.com |
- | 8 | tom | 3 | tom@QQ.com |
- +-------------+------+---------+----------+
- [root@mysql59 ~]#
- 查看slave服务器数据
- [root@mysql60 ~]# mysql -e 'select * from tarena_0.employees_0'
- +-------------+------+---------+----------+
- | employee_id | name | dept_id | mail |
- +-------------+------+---------+----------+
- | 6 | jim | 2 | jim@QQ.com |
- | 8 | tom | 3 | tom@QQ.com |
- +-------------+------+---------+----------+
- [root@mysql60 ~]#
查看c1集群中 数据库服务器存储的数据
- 查看master服务器数据
- [root@mysql61 ~]# mysql -e 'select * from tarena_1.employees_1'
- +-------------+------+---------+-----------+
- | employee_id | name | dept_id | mail |
- +-------------+------+---------+-----------+
- | 7 | lucy | 2 | lucy@QQ.com |
- | 9 | john | 1 | john@163.com |
- +-------------+------+---------+-----------+
- [root@mysql61 ~]#
- 查看slave服务器数据
- [root@mysql62 ~]# mysql -e 'select * from tarena_1.employees_1'
- +-------------+------+---------+-----------+
- | employee_id | name | dept_id | mail |
- +-------------+------+---------+-----------+
- | 7 | lucy | 2 | lucy@QQ.com |
- | 9 | john | 1 | john@163.com |
- +-------------+------+---------+-----------+
- [root@mysql62 ~]#
练习ER表
ER表,称为关联表,表示数据逻辑上有关联性的两个或多个表,例如工资表和员工表。对于关联表,通常希望他们能够有相同的分片规则,这样在进行关联查询时,能够快速定位到同一个数据分片中。MyCat2中对于关联表,不需要有过多的声明,他可以根据分片规则自行判断。
1)连接mycat服务建表
- [root@client50 ~]# mysql -h192.168.88.63 -P8066 -umycat --p654321
2)建表
- mysql> create table tarena.salary(
- employee_id int primary key,
- p_date date , basic int , bonus int
- ) DEFAULT CHARSET=utf8
- dbpartition BY mod_hash(employee_id)
- tbpartition BY mod_hash(employee_id) tbpartitions 1;
- Query OK, 1 row affected (1.93 sec)
3)在MyCat2终端查看关联表关系。
- [root@mycat63 ~]# mysql -h127.0.0.1 -P8066 -umycat --p654321
- mysql> /*+ mycat:showErGroup{}*/ ;
- +---------+------------+-----------+
- | groupId | schemaName | tableName |
- +---------+------------+-----------+
- | 0 | tarena | employees |
- | 0 | tarena | salary |
- +---------+------------+-----------+
- 2 rows in set (0.00 sec)
- mysql>
4)在2台master服务器查看表
- 在c0集群master服务器查看表
- [root@mysql59 ~]# mysql -e 'use tarena_0 ; show tables'
- +--------------------+
- | Tables_in_tarena_0 |
- +--------------------+
- | employees_0 |
- | salary_0 |
- +--------------------+
- [root@mysql59 ~]#
- 在c1集群master服务器查看表
- [root@mysql61 ~]# mysql -e 'use tarena_1;show tables'
- +--------------------+
- | Tables_in_tarena_1 |
- +--------------------+
- | employees_1 |
- | salary_1 |
- +--------------------+
- [root@mysql61~]#
5)客户端连接mycat服务并插入数据
- [root@client50 ~]# mysql -h192.168.88.63 -P8066 -umycat --p654321
- mysql> insert into tarena.salary values(6,20230110,20000,2000);
- mysql> insert into tarena.salary values(7,20230210,25000,2500);
- mysql> insert into tarena.salary values(8,20230310,30000,3000);
- mysql> insert into tarena.salary values(9,20230410,35000,3500);
6)在4台数据库服务器本机查看
- [root@mysql59 ~]# mysql -e 'select * from tarena_0.employees_0'
- +-------------+------+---------+----------+
- | employee_id | name | dept_id | mail |
- +-------------+------+---------+----------+
- | 6 | C | 2 | c@QQ.com |
- | 8 | B | 3 | B@QQ.com |
- +-------------+------+---------+----------+
- [root@mysql60 ~]# mysql -e 'select * from tarena_0.salary_0'
- +-------------+------------+-------+-------+
- | employee_id | p_date | basic | bonus |
- +-------------+------------+-------+-------+
- | 6 | 2023-01-10 | 20000 | 2000 |
- | 8 | 2023-03-10 | 30000 | 3000 |
- +-------------+------------+-------+-------+
- [root@mysql61 ~]# mysql -e 'select * from tarena_1.employees_1'
- +-------------+------+---------+-----------+
- | employee_id | name | dept_id | mail |
- +-------------+------+---------+-----------+
- | 7 | C | 2 | c@QQ.com |
- | 9 | a | 1 | a@163.com |
- +-------------+------+---------+-----------+
- [root@mysql62 ~]# mysql -e 'select * from tarena_1.salary_1'
- +-------------+------------+-------+-------+
- | employee_id | p_date | basic | bonus |
- +-------------+------------+-------+-------+
- | 7 | 2023-02-10 | 25000 | 2500 |
- | 9 | 2023-04-10 | 35000 | 3500 |
- +-------------+------------+-------+-------+
- [root@mysql62~]#
步骤三:练习全局表
全局表 数据会插入到两个库中,并且两个库中都有全部的数据。
客户端client50 连接mycat63主机的 建表存储数据
- [root@client50 ~]# mysql -h192.168.88.63 -umycat -p654321 -P8066
建表
- mysql> create table tarena.dept(
- dept_id int,
- dept_name char(10),
- primary key(dept_id)
- )default charset utf8 broadcast;
插入记录
- mysql> insert into tarena.dept values(1,"开发部"),(2,"运维部"),(3,"测试部");
在4台数据库服务器查看数据
- [root@mysql59 ~]# mysql -e 'select * from tarena.dept'
- +---------+-----------+
- | dept_id | dept_name |
- +---------+-----------+
- | 1 | 开发部 |
- | 2 | 运维部 |
- | 3 | 测试部 |
- +---------+-----------+
- [root@mysql60 ~]# mysql -e 'select * from tarena.dept'
- +---------+-----------+
- | dept_id | dept_name |
- +---------+-----------+
- | 1 | 开发部 |
- | 2 | 运维部 |
- | 3 | 测试部 |
- +---------+-----------+
- [root@mysql61 ~]# mysql -e 'select * from tarena.dept'
- +---------+-----------+
- | dept_id | dept_name |
- +---------+-----------+
- | 1 | 开发部 |
- | 2 | 运维部 |
- | 3 | 测试部 |
- +---------+-----------+
- [root@mysql62 ~]# mysql -e 'select * from tarena.dept'
- +---------+-----------+
- | dept_id | dept_name |
- +---------+-----------+
- | 1 | 开发部 |
- | 2 | 运维部 |
- | 3 | 测试部 |