DBA Day05
这周的内容涉及到的是各类数据库的服务配置与数据的备份与规复操作.
环境准备:
设置 ip 地址 和主机名
安装mysql,mysql-server --->启动
- yum -y install mysql mysql-server #装mysql环境包
- systemclt start mysqld #启动服务
- exit #退出环境
复制代码 这是新开的假造机,和上节课的mysql50假造机联动.
一.备份策略
备份体系三要素:
物理备份与规复
传统的备份方式,用u盘,sd卡,光碟备份存储数据.
通常是在呆板上进行的物理盘存储备份操作.能备份多少数据,取绝于你的硬件装备的容量情况
mysql50备份数据
- [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
- [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主机规复数据
- [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拷贝的备份文件规复数据
- [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张表
- [root@mysql50 ~]# mysqldump -uroot -p123 tarena salary > /bakdir/tarena_salary.sql
- mysqldump: [Warning] Using a password on the command line interface can be insecure.
复制代码 /备份多张表
- root@mysql50 ~]# mysqldump -uroot -pN123 tarena employees departments > /bakdir/tarena_employees_deparments.sql
复制代码 //备份1个库
- [root@mysql50 ~]# mysqldump -uroot -p123 -B tarena > /bakdir/tarena.sql
复制代码 //备份多个库
注意: 备份1个或者多个数据库 加 -B 选项
- [root@mysql50 ~]# mysqldump -uroot -p123 -B studb db1 > /bakdir/studb_db1.sql
复制代码 //备份全部库
注意: 备份全部数据库 加 -A 选项
- [root@mysql50 ~]# mysqldump -uroot -p123 -A > /bakdir/allbak.sql
复制代码 查看并备份(mysql50--->mysql51)
- [root@mysql50 ~]# ls /bakdir/*.sql #查看备份情况
- [root@mysql50 ~]# scp /bakdir/*.sql root@192.168.88.51:/root/ #备份数据
复制代码 模拟数据库误删情况,规复数据.
注意: 通过之前的mysqldump 方式备份的数据,已经传到了 mysql51呆板中.
在mysql51主机连接服务 删除库
- [/code] [code][root@mysql51 ~]# mysql -uroot -p123 #连接数据库
- mysql> drop database tarena; #删除库
- mysql> exit
复制代码 /规复数据
- [root@mysql51 ~]# mysql -uroot -p123 < /root/tarena.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/binPATH #添加在文件末尾
保存退出
- [root@host50 ~ ]# source /etc/bashrc #启动
- //查看帮助信息
- [root@host50 ~ ]# man xtrabackup (按q 退出)
复制代码 ................mysql51 也是同样的安装配置操作................................
步调一:数据增量备份(在mysql50主机 完成增量备份训练)
注意: \ 是换行符,代码太长写的,两行都是代码.
--host: 指定 ip, --user: 指定用户 --password: 指定密码
周一完全备份(备份全部数据)
#指定备份日期为周一的时间段
- mysql> insert into tarena.salary(date,employee_id,
- basic,bonus)
- values("20230610",18,25000,8000);
复制代码 周二增量备份(备份周一备份后新产生的数据)
#注意增量备份 ,增长了 --incremental-basedir 选项
- [root@mysql50 ~]# xtrabackup --host=127.0.0.1 --user=root --password=123 \
- --backup --target-dir=/new2 --incremental-basedir=/fullbak --datadir=/var/lib/mysql
复制代码 //插入新数据 (可以插入多行)
- mysql> insert into tarena.salary(date,employee_id,
- basic,bonus)
- values("20230710",18,25000,8000);
复制代码 周三增量备份(备份周二备份后新产生的数据)
- //备份[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
- //拷贝完全备份文件
- [root@mysql50~]# scp –r /fullbak root@192.168.88.51:/opt/
- //拷贝增量备份文件
- [root@mysql50 ~]# scp –r /new* root@192.168.88.51:/opt/
复制代码 在MySQL51主机利用备份文件规复数据
- 准备恢复数据
- [root@mysql51 ~]# xtrabackup --prepare --apply-log-only --target-dir=/opt/fullbak
复制代码 合并数据
//将周二的增量数据拷贝到周一备份目录里
合并数据选项: --incremental-dir
- [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日志文件
- [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)
复制代码- 执行查询命令
- mysql> select count(*) from mysql.user;
- +----------+
- | count(*) |
- +----------+
- | 4 |
- +----------+
- 1 row in set (0.00 sec)
复制代码 执行写下令时, 日志偏移量会发生改变
步调二:自界说日志目录和日志名
- [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
复制代码 登岸服务
查看日志信息
Mysql> show master status ;
- +----------------+----------+--------------+------------------+-------------------+
- | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
- +----------------+----------+--------------+------------------+-------------------+
- | mysql52.000001 | 156 | | | |
- +----------------+----------+--------------+------------------+-------------------+
阐明:默认日志文件容量大于1G时会自动创建新的日志文件,在日志文件没写满时,执行的全部写下令都会保存到当前利用的日志文件里。
注意点:
//只要服务重启就会创建新日志
/完全备份后创建新的日志文件,创建的日志个数和备份库的个数一致
步调四:训练日志相干下令的利用
- //查看已有的日志文件
- mysql> show binary logs;
- 日志文件名 日志大小(字节) 加密(no/yes)
- +----------------+-----------+-----------+
- | Log_name | File_size | Encrypted |
- +----------------+-----------+-----------+
- | mysql52.000001 | 201 | No |
- +----------------+-----------+-----------+
- 7 rows in set (0.00 sec)
复制代码 //查看正在利用的日志
- 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)
复制代码 //插入记录
- mysql> insert into db1.user values("yaya");
复制代码 //查看日志文件内容
阐明:
Log_name: 日志文件名。
Pos: 下令在日志文件中的起始位置。
Event_type: 变乱范例,例如 Query、Table_map、Write_rows 等。
Server_id: 服务器 ID。
End_log_pos:下令在文件中的竣事位置,以字节为单元。
Info:执行下令信息。
- 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";
//删除全部日志文件,并重新创建日志文件
步调五:利用日志规复数据
1)在mysql52主机执行如下操:
//重置日志
- [/code] [list=1]
- [*]mysql> reset master;
- [/list] //查看日志
- [code]
复制代码
- 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
- [root@mysql52 ~]# scp /mylog/mysql52.000001 root@192.168.88.50:/root/
复制代码 2)在MySQL50 利用日志规复数据
//查看日志
- [root@mysql50 ~]# ls /root/mysql52.000001
复制代码 //执行日志规复数据
- [/code] [list=1]
- [*][root@mysql50 ~]# mysqlbinlog /root/mysql52.000001 | mysql -uroot -p123
- [/list] //连接服务查看数据
- [list=1]
- [*][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 |
- [*]+------+-------+
- [/list]
- [hr] [size=5][b]DBA day06[/b][/size]
- [b]环境准备:[/b]
- [align=center][img=805,141]https://i-blog.csdnimg.cn/direct/068fc6a2ad9f4bd2a9963c9d35a371d1.png[/img][/align]
- [size=4][align=center][img=388,58]https://i-blog.csdnimg.cn/direct/0bab662f9da7487bbcc36e69b476ffa1.png[/img][/align][/size]
- [b]#有同步工具的,可以把两台假造机一起装包启动服务。[/b]
-
- [size=4]一、MySQL主从同步[/size]
- [align=center][img=800,260]https://i-blog.csdnimg.cn/direct/da49f92f024c41568d2a32e45a62e2e2.png[/img][/align]
- [b]原理:[/b]
- [align=center][img=650,263]https://i-blog.csdnimg.cn/direct/e7429db038fa4190a6dea0dd965bd200.png[/img][/align]
- [size=4]形式介绍[/size]
- [align=center][img=997,415]https://i-blog.csdnimg.cn/direct/2a52eb1dadc04293ad71d9631de2c131.png[/img][/align]
- 第一种是老大和小弟的关系;第二种是两兄弟,第三种是多保险,多个数据备份的数据库,避免出现不测数据库宕机,单一从数据库规复难度大的问题。
- 还有主--从---从关系。通俗来说,就是老大的老大,和小弟的小弟的关系。
- [align=center][img=411,257]https://i-blog.csdnimg.cn/direct/826d830040394e00adf450d9d5c82ced.png[/img][/align]
- [align=center][align=center][img=500,270]https://i-blog.csdnimg.cn/direct/4ad8205a0bf34fe786b9de8b54da81fd.png[/img][/align][/align]
- 好比 :老大下完下令,老二传达,末了老二的小弟执行下令。
- [hr] [b]理论竣事,时间开始。(*^▽^*)[/b]
- [size=3]步调一:把192.168.88.53配置为master服务器[/size]
- [list=1]
- [*][root@mysql53 ~]# vim /etc/my.cnf.d/mysql-server.cnf
- [*][mysqld]
- [*]server-id=53
- [*]log-bin=mysql53
- [*]:wq
- [*][root@mysql53 ~]# systemctl restart mysqld
- [/list] 2)用户[b]授权[/b]
- [list=1]
- [*][root@mysql53 ~]# mysql
- [*]mysql> create user repluser@"%" identified by "123";
- [*]mysql> grant replication slave on *.* to repluser@"%";
- [/list] 查看日志信息
- [list=1]
- [*]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)
- [/list]
- [size=3]步调二:把192.168.88.54配置为slave服务器[/size]
- 1)指定server-id 并重启数据库服务
- [list=1]
- [*][root@mysql54 ~]# vim /etc/my.cnf.d/mysql-server.cnf
- [*][mysqld]
- [*]server-id=54
- [*]:wq
- [*][root@mysql54 ~]# systemctl restart mysqld
- [/list] 2)登岸服务指定主服务器信息 #和主机信息对应上
- [list=1]
- [*][root@mysql54 ~]# mysql
- [*]mysql> change master to
- [*]master_[b]host[/b]="0.0.0.0", #任何用户都可以访问
- [*]master_[b]user[/b]="repluser",
- [*]master_[b]password[/b]="123",
- [*]master_[b]log_file[/b]="mysql53.000001",
- [*]master_[b]log_pos[/b]=667;
- [/list] //启动slave进程
- [list=1]
- [*]mysql> start slave ;
- [/list] //查看状态信息
- #重要看IO/SQL 线程 是否为yes状态
- [code]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服务器建库 、建表、添加用户 、授权
- [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@"%" ; 授予权限
2) 在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服务
- [/code] [list=1]
- [*]//查看帮助
- [*][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 ~]#
- [/list] [b]连接mycat服务[/b]
- [list=1]
- [*][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>
- [/list] [size=3][b]步调三:配置读写分离[/b][/size]
- 1)添加数据源:连接mycat服务后做如下操作
- [code]//连接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服务
- [/code] [list=1]
- [*][root@mycat58 ~]# mysql -h127.0.0.1 -P8066 -umycat -p123
- [/list] [b]//创建集群[/b]
- [code]mysql>/*!mycat:createcluster{
- "name":"rwcluster",
- "masters":["whost53"],
- "replicas":["rhost54"]
- }*/ ;
- Mysql>
复制代码 //查看集群信息
- 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服务
- [/code] [list=1]
- [*][root@mycat50~]# mysql -h127.0.0.1 -P8066 -umycat -p123
- [/list] //创建存储数据的库
- [list=1]
- [*]mysql> create database testdb;
- [*]mysql> exit
- [*]Bye
- [/list] //指定库存储数据利用的集群
- [code]
复制代码
- [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服务器的数据不一样
- [/code] [list=1]
- [*][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 ~]#
- [/list] [b]//主服务器数据稳定[/b]
- [code]
复制代码
- [root@mysql53 ~]# mysql -e 'select * from testdb.user'
- +------+----------+
- | name | password |
- +------+----------+
- | yaya | 123456 |
- +------+----------+
//binlog日志偏移量稳定
- [/code] [list=1]
- [*][root@mysql53 ~]# mysql -e 'show master status'
- [*]+----------------+----------+--------------+------------------+-------------------+
- [*]| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
- [*]+----------------+----------+--------------+------------------+-------------------+
- [*]| mysql56.000002 | 4514 | | | |
- [*]+----------------+----------+--------------+------------------+-------------------+
- [*][root@mysql53 ~]#
- [/list]
- [hr] [size=5][b]DBA Day07[/b][/size]
- [b]环境准备:[/b][align=center][img=1200,276]https://i-blog.csdnimg.cn/direct/cccf2e2831f84e4e924744473802221c.png[/img][/align]
- [hr] [size=4][b]一、数据分片概述[/b][/size]
- [align=center][img=1105,553]https://i-blog.csdnimg.cn/direct/05d4e5dc3af64a1fba9f30d5ab11faa8.png[/img][/align]
- [size=4][align=center][img=922,487]https://i-blog.csdnimg.cn/direct/63cb5bd3a9d14b4880106d17824ea3f7.png[/img][/align][/size]
- [align=center][img=829,418]https://i-blog.csdnimg.cn/direct/a789617179a8407183959befd2de69e8.png[/img][/align]
- [size=4][b]二、部署mycat服务[/b][/size]
- [align=center][img=1095,445]https://i-blog.csdnimg.cn/direct/198c39a9a34a4ce38fbc3ebdbdf0e554.png[/img][/align]
- [size=3][b]分片规则[/b][/size]
- [align=center][img=1066,427]https://i-blog.csdnimg.cn/direct/acc9a7108d7c41a7a31c8b58754b4e63.png[/img][/align]
- [align=center][img=1081,490]https://i-blog.csdnimg.cn/direct/72ef5648eb994869bd37f4320ca10188.png[/img][/align]
- [size=3]步调一:把MySQL60配置为MySQL59的从服务器[/size]
- [b] #具体操作,看 Day06 MySQL主从同步设置[/b]
- [b]步调二:把MySQL62配置为MySQL61的从服务器[/b]
- [b] #具体操作,看 Day06 MySQL主从同步设置[/b]
- [size=3][b]步调三:把主机mycat63配置为mycat服务器。[/b][/size]
-
- [b]安装mycat软件[/b]
- [list=1]
- [*]//安装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/
- [/list]
- [b]3)界说客户端连接时利用的用户:[/b]
- [code]
复制代码
- [root@mycat63 ~]# vim /usr/local/mycat/conf/users/root.user.json
- {
- "dialect":"mysql",
- "ip":null,
- "password":"123", 密码
- "transactionType":"proxy",
- "username":"mycat" 用户名
- }
- :wq
4)界说连接的数据库服务
- [/code] [list=1]
- [*][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", #[b]直接设置用户名,其他不消管[/b]
- [*]"weight":0
- [*]}
- [*]:wq
- [/list] [b]5)在mycat63主机运行数据库服务[/b]
- [list=1]
- [*]//创建qq用户
- [*][root@mycat63 ~]# mysql
- [*]mysql> create user plj@"%" identified by "123"; 创建用户
- [*]mysql> grant all on *.* to qq@"%" ; 授予权限
- [*]mysql> exit
- [*]Bye
- [/list]
- [b]6)启动mycat服务[/b]
- [list=1]
- [*]//查看下令帮助
- [*][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 ~]#
- [/list] [size=3]步调四:连接mycat服务器[/size]
- 1)连接本机的mycat服务
- [list=1]
- [*][root@mycat63 ~]# mysql -h127.0.0.1 -P8066 -umycat -p123
- [/list] [size=3]步调五:添加数据源[/size]
- [list=1]
- [*][b]//添加MySQL59主机[/b]
- [*]MySQL>/*+ mycat:createdatasource{
- [*]"name":"dw0",
- [*]"url":"jdbc:mysql://192.168.88.59:3306",
- [*]"user":"qq",
- [*]"password":"123"
- [*]}*/;
- [*][b]//添加MySQL60主机[/b]
- [*]Mysql>/*+ mycat:createdatasource{
- [*]"name":"dr0",
- [*]"url":"jdbc:mysql://192.168.88.60:3306",
- [*]"user":"qq",
- [*]"password":"123"
- [*]}*/;
- [*][b]//添加MySQL61主机[/b]
- [*]Mysql>/*+ mycat:createdatasource{
- [*]"name":"dw1",
- [*]"url":"jdbc:mysql://192.168.88.61:3306",
- [*]"user":"qq",
- [*]"password":"123"
- [*]}*/;
- [*][b]//添加MySQL62主机[/b]
- [*]Mysql>/*+ mycat:createdatasource{
- [*]"name":"dr1",
- [*]"url":"jdbc:mysql://192.168.88.62:3306",
- [*]"user":"qq",
- [*]"password":"123"
- [*]}*/;
- [*]Mysql>
- [/list] 2)查看数据信息
- [list=1]
- [*]mysql> /*+mycat:showDataSources{}*/ \G
- [/list] [b]#表现全部 服务器配置情况即可[/b]
- [size=3]步调六:配置数据库服务器[/size]
- //在主服务器 MySQL59 主机添加qq用户
- [code]
复制代码
- [root@mysql59 ~]# mysql
- mysql> create user qq@"%" identified by "123456";
- Mysql> grant all on *.* to qq@"%";
//在主服务器 MySQL61 主机添加qq用户
- [/code] [list=1]
- [*][root@mysql61 ~]# mysql
- [*]mysql> create user qq@"%" identified by "123456";
- [*]Mysql> grant all on *.* to qq@"%";
- [/list] [b]//在从服务器MySQL60 查看用户[/b]
- [code]
复制代码
- [root@mysql60 ~]# mysql -e 'select user from mysql.user where user="qq" '
- | user |
- +------+
- | qq |
- +------+
- [root@mysql60 ~]#
//在从服务器MySQL62 查看用户
- [/code] [list=1]
- [*][root@mysql62 ~]# mysql -e 'select user from mysql.user where user="qq"'
- [*]+------+
- [*]| user |
- [*]+------+
- [*]| qq|
- [*]+------+
- [*][root@host62 ~]#
- [/list] [size=3][b]步调七:创建集群[/b][/size]
- 连接mycat服务
- [code]
复制代码
- [root@mycat63 ~]# mysql -h127.0.0.1 -P8066 -umycat -p123
2)创建第1个集群 #不消敲,直接复制粘贴。懂得原理即可
- [/code] [list=1]
- [*]mysql>/*!mycat:createcluster{
- [*]"name":"c0",
- [*]"masters":["dw0"],
- [*]"replicas":["dr0"]
- [*]}*/;
- [/list] [b]3)创建第2个集群 #不消敲,直接复制粘贴。懂得原理即可[/b]
- [code]
复制代码
- mysql>/*!mycat:createcluster{
- "name":"c1",
- "masters":["dw1"],
- "replicas":["dr1"]
- }*/;
- Mysql>
4)查看集群信息
- [/code] [list=1]
- [*]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>
- [/list] [b]5)创建的集群保存在mycat安装目录下[/b]
- [code]
复制代码
- [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服务
- [/code] [list=1]
- [*][root@mycat63 ~]# mysql -h127.0.0.1 -umycat -p654321 -P8066
- [/list] 2)建库
- [code]
复制代码
- mysql> create database tarena;
- mysql> exit
3) 配置文件存放位置
- [/code] [list=1]
- [*][root@mycat63 ~]# ls /usr/local/mycat/conf/schemas/tarena.schema.json
- [*]/usr/local/mycat/conf/schemas/tarena.schema.json
- [*][root@mycat63 ~]#
- [/list] [size=3][/size]
- [size=3]训练分片表[/size]
- 阐明:
- dbpartition by 界说分库利用的分片规则,
- tbpartition by 界说分表利用的分片规则。
- mod_hash 分片规则,用employee_id表头的值做取模盘算
- tbpartitions 表的分片数目
- dbpartitions 库的分片数目
- 1)连接mycat服务
- [code]
复制代码
- [root@client50 ~]# mysql -h192.168.88.63 -P8066 -umycat –p654321
2)建表
- [/code] [list=1]
- [*]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;
- [/list] 3)在59主机查看库和表
- [code]
复制代码
- [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主机查看
- [/code] [list=1]
- [*][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 ~]#
- [/list] 5)在61主机查看库和表
- [code]
复制代码
- [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主机查看库和表
- [/code] [list=1]
- [*][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 ~]#
- [/list] 存储数据
- [code]
复制代码
- [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");
查看数据
- [/code] [list=1]
- [*]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)
- [/list] 查看c0集群中 数据库服务器存储的数据
- [code]
复制代码
- 查看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集群中 数据库服务器存储的数据
- [/code] [list=1]
- [*]查看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 ~]#
- [/list] [size=3]训练ER表[/size]
- ER表,称为关联表,表现数据逻辑上有关联性的两个或多个表,例如工资表和员工表。对于关联表,通常渴望他们可以大概有相同的分片规则,这样在进行关联查询时,可以大概快速定位到同一个数据分片中。MyCat2中对于关联表,不需要有过多的声明,他可以根据分片规则自行判定。
- 1)连接mycat服务建表
- [code]
复制代码
- [root@client50 ~]# mysql -h192.168.88.63 -P8066 -umycat –p654321
2)建表
- [/code] [list=1]
- [*]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)
- [/list] 3)在MyCat2终端查看关联表关系。
- [code]
复制代码
- [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服务器查看表
- [/code] [list=1]
- [*]在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~]#
- [/list] 5)客户端连接mycat服务并插入数据
- [code]
复制代码
- [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台数据库服务器本机查看
- [/code] [list=1]
- [*][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~]#
- [/list] 步调三:训练全局表
- 全局表 数据会插入到两个库中,并且两个库中都有全部的数据。
- 客户端client50 连接mycat63主机的 建表存储数据
- [code]
复制代码
- [root@client50 ~]# mysql -h192.168.88.63 -umycat -p654321 -P8066
建表
- [/code] [list=1]
- [*]mysql> create table tarena.dept(
- [*]dept_id int,
- [*]dept_name char(10),
- [*]primary key(dept_id)
- [*])default charset utf8 broadcast;
- [/list] 插入记录
- [code]
复制代码
- mysql> insert into tarena.dept values(1,"开辟部"),(2,"运维部"),(3,"测试部");
在4台数据库服务器查看数据
[code][/code]
- [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 | 测试部 |
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |