1.简述DDL,DML,DCL,DQL,并且说明mysql各个关键字查询时候的先后顺序
DDL 定义语言:CREATE创建 DROP删除 ALTER修改
DML操作语言: INSERT增加 DELETE删除 UPDATE改
DQL查询语言: SELECT查
DCL控制语言: GRANT授权 REVOKE取消授权
在SQL语句中每个关键字都会按照顺序往下执行,而每一步操作,会生成一个虚拟表,最后的虚拟表就是最终结果。
FROM : 对FROM左边的表和右边的表计算笛卡尔积,产生虚表VT1;
ON : 对虚拟表VT1进行ON筛选,只有那些符合条件的行才会被记录在虚拟表VT2中;
JOIN :如果是OUT JOIN,那么将保留表中(如左表或者右表)未匹配的行作为外部行添加到虚拟表VT2中,从而产生虚拟表VT3;
WHERE :对虚拟表VT3进行WHERE条件过滤,只有符合的记录才会被放入到虚拟表VT4;
GROUP BY:根据GROUP BY子句中的列,对虚拟表VT4进行分组操作,产生虚拟表VT5;
CUBE|ROLLUP:对虚拟表VT5进行CUBE或者ROLLUP操作,产生虚拟表VT6;
HAVING :对虚拟表VT6进行 HAVING 条件过滤,只有符合的记录才会被插入到虚拟表VT7中;
SELECT :执行SELECT操作,选择指定的列,插入到虚拟表VT8中;
DISTINCT :对虚拟表VT8中的记录进行去重,产生虚拟表VT9;
ORDER BY :将虚拟表VT9中的记录按照进行排序操作,产生虚拟表VT10;
LIMIT :取出指定行的记录,产生虚拟表VT11,并将结果返回。
2. 自行设计10个sql查询语句,需要用到关键字[GROUP BY/HAVING/ORDER BY/LIMIT],至少同时用到两个
- order by 和 limit 排序后提取前三个
(root@localhost) [hellodb]> select * from students order by age limit 3;
+-------+-------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+-------------+-----+--------+---------+-----------+
| 14 | Lu Wushuang | 17 | F | 3 | NULL |
| 8 | Lin Daiyu | 17 | F | 7 | NULL |
| 19 | Xue Baochai | 18 | F | 6 | NULL |
+-------+-------------+-----+--------+---------+-----------+
3 rows in set (0.01 sec)
- group by 和 having 分组求平均值
(root@localhost) [hellodb]> select gender,avg(age) from students group by gender having gender='F';
+--------+----------+
| gender | avg(age) |
+--------+----------+
| F | 19.0000 |
+--------+----------+
1 row in set (0.00 sec)
- group by 和 order by 分组排序
(root@localhost) [hellodb]> select classid,count(*) 数量 from students group by classid order by 数量;
+---------+--------+
| classid | 数量 |
+---------+--------+
| 5 | 1 |
| NULL | 2 |
| 2 | 3 |
| 7 | 3 |
| 1 | 4 |
| 4 | 4 |
| 3 | 4 |
| 6 | 4 |
+---------+--------+
8 rows in set (0.00 sec)
- group by和order by 分组显示并排序
(root@localhost) [hellodb]> select gender,classid,avg(age) from students where classid is not null group by gender,classid order by classid,classid;
+--------+---------+----------+
| gender | classid | avg(age) |
+--------+---------+----------+
| M | 1 | 21.5000 |
| F | 1 | 19.5000 |
| M | 2 | 36.0000 |
| M | 3 | 26.0000 |
| F | 3 | 18.3333 |
| M | 4 | 24.7500 |
| M | 5 | 46.0000 |
| F | 6 | 20.0000 |
| M | 6 | 23.0000 |
| F | 7 | 18.0000 |
| M | 7 | 23.0000 |
+--------+---------+----------+
11 rows in set (0.00 sec)
- like 和 order by
(root@localhost) [hellodb]> select * from students where name like'x%' order by classid;
+-------+-------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+-------------+-----+--------+---------+-----------+
| 24 | Xu Xian | 27 | M | NULL | NULL |
| 16 | Xu Zhu | 21 | M | 1 | NULL |
| 22 | Xiao Qiao | 20 | F | 1 | NULL |
| 3 | Xie Yanke | 53 | M | 2 | 16 |
| 7 | Xi Ren | 19 | F | 3 | NULL |
| 19 | Xue Baochai | 18 | F | 6 | NULL |
+-------+-------------+-----+--------+---------+-----------+
6 rows in set (0.00 sec)
- not null 和order by
(root@localhost) [hellodb]> select * from students where classid is not null order by StuID;
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
| 1 | Shi Zhongyu | 22 | M | 2 | 3 |
| 2 | Shi Potian | 22 | M | 1 | 7 |
| 3 | Xie Yanke | 53 | M | 2 | 16 |
| 4 | Ding Dian | 32 | M | 4 | 4 |
| 5 | Yu Yutong | 26 | M | 3 | 1 |
| 6 | Shi Qing | 46 | M | 5 | NULL |
| 7 | Xi Ren | 19 | F | 3 | NULL |
| 8 | Lin Daiyu | 17 | F | 7 | NULL |
| 9 | Ren Yingying | 20 | F | 6 | NULL |
| 10 | Yue Lingshan | 19 | F | 3 | NULL |
| 11 | Yuan Chengzhi | 23 | M | 6 | NULL |
| 12 | Wen Qingqing | 19 | F | 1 | NULL |
| 13 | Tian Boguang | 33 | M | 2 | NULL |
| 14 | Lu Wushuang | 17 | F | 3 | NULL |
| 15 | Duan Yu | 19 | M | 4 | NULL |
| 16 | Xu Zhu | 21 | M | 1 | NULL |
| 17 | Lin Chong | 25 | M | 4 | NULL |
| 18 | Hua Rong | 23 | M | 7 | NULL |
| 19 | Xue Baochai | 18 | F | 6 | NULL |
| 20 | Diao Chan | 19 | F | 7 | NULL |
| 21 | Huang Yueying | 22 | F | 6 | NULL |
| 22 | Xiao Qiao | 20 | F | 1 | NULL |
| 23 | Ma Chao | 23 | M | 4 | NULL |
+-------+---------------+-----+--------+---------+-----------+
23 rows in set (0.00 sec)
- order 和 limit
(root@localhost) [hellodb]> select * from students where classid is not null order by StuID limit 3;
+-------+-------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+-------------+-----+--------+---------+-----------+
| 1 | Shi Zhongyu | 22 | M | 2 | 3 |
| 2 | Shi Potian | 22 | M | 1 | 7 |
| 3 | Xie Yanke | 53 | M | 2 | 16 |
+-------+-------------+-----+--------+---------+-----------+
3 rows in set (0.00 sec)
3. xtrabackup备份和还原数据库练习
master机器
[root@10_0_0_30 ~]$yum -y install percona-xtrabackup-80-8.0.33-28.1.el8.x86_64.rpm
[root@10_0_0_30 ~]$xtrabackup -uroot -p123456 --backup --target-dir=/backup/base
[root@10_0_0_30 /backup/base]$scp -r /backup/ 10.0.0.134:/
还原机器
[root@server ~]# ls /backup/ #查看复制过来的backup
base
[root@server ~]yum -y install mysql-server #安装MYSQLD
[root@server ~] yum -y install lrzsz #安装传输工具
[root@server ~] yum -y install percona-xtrabackup-80-8.0.33-28.1.el8.x86_64.rpm #安装xtrabackup工具
[root@server ~] xtrabackup --prepare --target-dir=/backup/base #整理还原事务
[root@server mysql] xtrabackup --copy-back -target-dir=/backup/base #还原
[root@server mysql] chown -R mysql:mysql /var/lib/mysql #授权
[root@server mysql] service mysqld start #重启
Redirecting to /bin/systemctl start mysqld.service
root@server mysql]# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| hellodb |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
4. 实现mysql主从复制,主主复制和半同步复制
master:151 slaver:134
1.主从复制
确认主上数据库
mysql root@(none):(none)> show databases
+--------------------+
| Database |
+--------------------+
| hellodb |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
修改master主节点的配置
[root@151 ~]# cat /etc/my.cnf
#
# This group is read both both by the client and the server
# use it for options that affect everything
#
[client-server]
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
[client]
user=root
password=123456
[mysqld]
server_id=151
log-bin=/data/mysql/log/mysql-bin
log_error=/data/mysql/log/error.log
innodb-flush-log-at-trx-commit=2
general_log = ON
slow_query_log = ON
long_query_time = 3
重启mysqld
[root@151 ~]# systemctl restart mysqld
创建复制用户并授权
mysql root@(none):(none)> create user 'slaver134'@'10.0.0.%' identified by '123456';
grant replication slave on *.* to 'slaver134'@'10.0.0.%';
Query OK, 0 rows affected (0.01 sec)
配置从节点
[root@134 ~]# cat /etc/my.cnf
#
# This group is read both both by the client and the server
# use it for options that affect everything
#
[client-server]
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
[client]
user=root
password=123456
[mysqld]
server_id=134
log-bin = /data/mysql/log/mysql.bin
read_only=ON
创建binlog文件夹并且授权后重启mysqld
[root@134 ~]# mkdir -p /data/mysql/log/
[root@134 ~]# chown mysql:mysql /data/mysql/log/
[root@134 ~]# ll /data/mysql/
total 0
drwxr-xr-x 2 mysql mysql 6 Jan 5 13:50 log
[root@134 ~]# systemctl restart mysqld
查看主151上的binlog文件位置
mysql root@(none):(none)> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000006 | 157 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set
Time: 0.009s
修改CHANGE MASTER TO并启动sql线程:
mysql> CHANGE MASTER TO
-> MASTER_HOST='10.0.0.151',
-> MASTER_USER='slaver134',
-> MASTER_PASSWORD='123456',
-> MASTER_PORT=3306,
-> MASTER_LOG_FILE='mysql-bin.000006',
-> MASTER_LOG_POS=157;
Query OK, 0 rows affected, 9 warnings (0.03 sec)
mysql> start slave;
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Master_Host: 10.0.0.151
Master_User: slaver134
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000006
Read_Master_Log_Pos: 157
Relay_Log_File: 134-relay-bin.000002
Relay_Log_Pos: 326
Relay_Master_Log_File: mysql-bin.000006
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
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: 157
Relay_Log_Space: 534
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: 151
Master_UUID: 42900a2b-a146-11ee-ba4c-000c29e3468a
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)
确认主 从服务器线程情况:
master:151
mysql root@(none):(none)> show processlist;
+----+-----------------+------------------+--------+-------------+------+-----------------------------------------------------------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-----------------+------------------+--------+-------------+------+-----------------------------------------------------------------+------------------+
| 5 | event_scheduler | localhost | <null> | Daemon | 1325 | Waiting on empty queue | <null> |
| 8 | root | localhost | <null> | Query | 0 | init | show processlist |
| 12 | slaver134 | 10.0.0.134:36136 | <null> | Binlog Dump | 178 | Source has sent all binlog to replica; waiting for more updates | <null> |
+----+-----------------+------------------+--------+-------------+------+-----------------------------------------------------------------+------------------+
3 rows in set
Time: 0.005s
slaver:134
mysql> show processlist;
+----+-----------------+-----------------+------+---------+------+----------------------------------------------------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-----------------+-----------------+------+---------+------+----------------------------------------------------------+------------------+
| 5 | event_scheduler | localhost | NULL | Daemon | 822 | Waiting on empty queue | NULL |
| 8 | root | localhost | NULL | Query | 0 | init | show processlist |
| 15 | system user | connecting host | NULL | Connect | 214 | Waiting for source to send event | NULL |
| 16 | system user | | NULL | Query | 214 | Replica has read all relay log; waiting for more updates | NULL |
| 17 | system user | | NULL | Connect | 214 | Waiting for an event from Coordinator | NULL |
| 18 | system user | | NULL | Connect | 214 | Waiting for an event from Coordinator | NULL |
| 19 | system user | | NULL | Connect | 214 | Waiting for an event from Coordinator | NULL |
| 20 | system user | | NULL | Connect | 214 | Waiting for an event from Coordinator | NULL |
+----+-----------------+-----------------+------+---------+------+----------------------------------------------------------+------------------+
8 rows in set (0.00 sec)
测试主从复制
151上创建test1数据库
mysql root@(none):(none)> create database test1;
Query OK, 1 row affected
Time: 0.003s
mysql root@(none):(none)> show databases;
+--------------------+
| Database |
+--------------------+
| hellodb |
| information_schema |
| mysql |
| performance_schema |
| sys |
| test1 |
+--------------------+
6 rows in set
Time: 0.005s
134上查看:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| hellodb |
| information_schema |
| mysql |
| performance_schema |
| sys |
| test1 |
+--------------------+
6 rows in set (0.00 sec)
3.半同步复制
master:151 slave1:134 slave2:145
master安装半同步模块
mysql root@(none):(none)> install plugin rpl_semi_sync_master soname 'semisync_master.so';
Query OK, 0 rows affected
Time: 0.001s
mysql root@(none):(none)> show plugins;
+---------------------------------+----------+--------------------+--------------------+---------+
| Name | Status | Type | Library | License |
+---------------------------------+----------+--------------------+--------------------+---------+
| binlog | ACTIVE | STORAGE ENGINE | <null> | GPL |
| mysql_native_password | ACTIVE | AUTHENTICATION | <null> | GPL |
| sha256_password | ACTIVE | AUTHENTICATION | <null> | GPL |
| caching_sha2_password | ACTIVE | AUTHENTICATION | <null> | GPL |
| sha2_cache_cleaner | ACTIVE | AUDIT | <null> | GPL |
| daemon_keyring_proxy_plugin | ACTIVE | DAEMON | <null> | GPL |
| CSV | ACTIVE | STORAGE ENGINE | <null> | GPL |
| MEMORY | ACTIVE | STORAGE ENGINE | <null> | GPL |
| InnoDB | ACTIVE | STORAGE ENGINE | <null> | GPL |
| INNODB_TRX | ACTIVE | INFORMATION SCHEMA | <null> | GPL |
| INNODB_CMP | ACTIVE | INFORMATION SCHEMA | <null> | GPL |
| INNODB_CMP_RESET | ACTIVE | INFORMATION SCHEMA | <null> | GPL |
| INNODB_CMPMEM | ACTIVE | INFORMATION SCHEMA | <null> | GPL |
| INNODB_CMPMEM_RESET | ACTIVE | INFORMATION SCHEMA | <null> | GPL |
| INNODB_CMP_PER_INDEX | ACTIVE | INFORMATION SCHEMA | <null> | GPL |
| INNODB_CMP_PER_INDEX_RESET | ACTIVE | INFORMATION SCHEMA | <null> | GPL |
| INNODB_BUFFER_PAGE | ACTIVE | INFORMATION SCHEMA | <null> | GPL |
| INNODB_BUFFER_PAGE_LRU | ACTIVE | INFORMATION SCHEMA | <null> | GPL |
| INNODB_BUFFER_POOL_STATS | ACTIVE | INFORMATION SCHEMA | <null> | GPL |
| INNODB_TEMP_TABLE_INFO | ACTIVE | INFORMATION SCHEMA | <null> | GPL |
| INNODB_METRICS | ACTIVE | INFORMATION SCHEMA | <null> | GPL |
| INNODB_FT_DEFAULT_STOPWORD | ACTIVE | INFORMATION SCHEMA | <null> | GPL |
| INNODB_FT_DELETED | ACTIVE | INFORMATION SCHEMA | <null> | GPL |
| INNODB_FT_BEING_DELETED | ACTIVE | INFORMATION SCHEMA | <null> | GPL |
| INNODB_FT_CONFIG | ACTIVE | INFORMATION SCHEMA | <null> | GPL |
| INNODB_FT_INDEX_CACHE | ACTIVE | INFORMATION SCHEMA | <null> | GPL |
| INNODB_FT_INDEX_TABLE | ACTIVE | INFORMATION SCHEMA | <null> | GPL |
| INNODB_TABLES | ACTIVE | INFORMATION SCHEMA | <null> | GPL |
| INNODB_TABLESTATS | ACTIVE | INFORMATION SCHEMA | <null> | GPL |
| INNODB_INDEXES | ACTIVE | INFORMATION SCHEMA | <null> | GPL |
| INNODB_TABLESPACES | ACTIVE | INFORMATION SCHEMA | <null> | GPL |
| INNODB_COLUMNS | ACTIVE | INFORMATION SCHEMA | <null> | GPL |
| INNODB_VIRTUAL | ACTIVE | INFORMATION SCHEMA | <null> | GPL |
| INNODB_CACHED_INDEXES | ACTIVE | INFORMATION SCHEMA | <null> | GPL |
| INNODB_SESSION_TEMP_TABLESPACES | ACTIVE | INFORMATION SCHEMA | <null> | GPL |
| MyISAM | ACTIVE | STORAGE ENGINE | <null> | GPL |
| MRG_MYISAM | ACTIVE | STORAGE ENGINE | <null> | GPL |
| PERFORMANCE_SCHEMA | ACTIVE | STORAGE ENGINE | <null> | GPL |
| TempTable | ACTIVE | STORAGE ENGINE | <null> | GPL |
| ARCHIVE | ACTIVE | STORAGE ENGINE | <null> | GPL |
| BLACKHOLE | ACTIVE | STORAGE ENGINE | <null> | GPL |
| FEDERATED | DISABLED | STORAGE ENGINE | <null> | GPL |
| ngram | ACTIVE | FTPARSER | <null> | GPL |
| mysqlx_cache_cleaner | ACTIVE | AUDIT | <null> | GPL |
| mysqlx | ACTIVE | DAEMON | <null> | GPL |
| rpl_semi_sync_master | ACTIVE | REPLICATION | semisync_master.so | GPL |
+---------------------------------+----------+--------------------+--------------------+---------+
修改master配置文件
[root@151 ~]# cat /etc/my.cnf
#
# This group is read both both by the client and the server
# use it for options that affect everything
#
[client-server]
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
[client]
user=root
password=123456
[mysqld]
server_id=151
log-bin=/data/mysql/log/mysql-bin
log_error=/data/mysql/log/error.log
innodb-flush-log-at-trx-commit=2
general_log = ON
slow_query_log = ON
long_query_time = 3
rpl_semi_sync_master_enabled=on
rpl_semi_sync_master_timeout=3000
[root@151 ~]# systemctl restart mysqld
查看半同步服务
mysql root@(none):(none)> select @@rpl_semi_sync_master_enabled;
+--------------------------------+
| @@rpl_semi_sync_master_enabled |
+--------------------------------+
| 1 |
+--------------------------------+
1 row in set
Time: 0.008s
slave1服务器配置
slave1 安装半同步模块
mysql> install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
Query OK, 0 rows affected, 1 warning (0.02 sec)
mysql> show plugins;
+---------------------------------+----------+--------------------+-------------------+---------+
| Name | Status | Type | Library | License |
+---------------------------------+----------+--------------------+-------------------+---------+
| binlog | ACTIVE | STORAGE ENGINE | NULL | GPL |
| mysql_native_password | ACTIVE | AUTHENTICATION | NULL | GPL |
| sha256_password | ACTIVE | AUTHENTICATION | NULL | GPL |
| caching_sha2_password | ACTIVE | AUTHENTICATION | NULL | GPL |
| sha2_cache_cleaner | ACTIVE | AUDIT | NULL | GPL |
| daemon_keyring_proxy_plugin | ACTIVE | DAEMON | NULL | GPL |
| CSV | ACTIVE | STORAGE ENGINE | NULL | GPL |
| MEMORY | ACTIVE | STORAGE ENGINE | NULL | GPL |
| InnoDB | ACTIVE | STORAGE ENGINE | NULL | GPL |
| INNODB_TRX | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_CMP | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_CMP_RESET | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_CMPMEM | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_CMPMEM_RESET | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_CMP_PER_INDEX | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_CMP_PER_INDEX_RESET | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_BUFFER_PAGE | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_BUFFER_PAGE_LRU | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_BUFFER_POOL_STATS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_TEMP_TABLE_INFO | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_METRICS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_FT_DEFAULT_STOPWORD | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_FT_DELETED | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_FT_BEING_DELETED | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_FT_CONFIG | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_FT_INDEX_CACHE | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_FT_INDEX_TABLE | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_TABLES | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_TABLESTATS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_INDEXES | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_TABLESPACES | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_COLUMNS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_VIRTUAL | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_CACHED_INDEXES | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_SESSION_TEMP_TABLESPACES | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| MyISAM | ACTIVE | STORAGE ENGINE | NULL | GPL |
| MRG_MYISAM | ACTIVE | STORAGE ENGINE | NULL | GPL |
| PERFORMANCE_SCHEMA | ACTIVE | STORAGE ENGINE | NULL | GPL |
| TempTable | ACTIVE | STORAGE ENGINE | NULL | GPL |
| ARCHIVE | ACTIVE | STORAGE ENGINE | NULL | GPL |
| BLACKHOLE | ACTIVE | STORAGE ENGINE | NULL | GPL |
| FEDERATED | DISABLED | STORAGE ENGINE | NULL | GPL |
| ngram | ACTIVE | FTPARSER | NULL | GPL |
| mysqlx_cache_cleaner | ACTIVE | AUDIT | NULL | GPL |
| mysqlx | ACTIVE | DAEMON | NULL | GPL |
| rpl_semi_sync_slave | ACTIVE | REPLICATION | semisync_slave.so | GPL |
+---------------------------------+----------+--------------------+-------------------+---------+
46 rows in set (0.00 sec)
修改slave1配置文件
[root@134 ~]# cat /etc/my.cnf
#
# This group is read both both by the client and the server
# use it for options that affect everything
#
[client-server]
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
[client]
user=root
password=123456
[mysqld]
server_id=134
log-bin = /data/mysql/log/mysql.bin
read_only=ON
rpl_semi_sync_slave_enabled=on
[root@134 ~]# systemctl restart mysqld
查看半同步服务
mysql> select @@rpl_semi_sync_slave_enabled;
+-------------------------------+
| @@rpl_semi_sync_slave_enabled |
+-------------------------------+
| 1 |
+-------------------------------+
1 row in set (0.00 sec)
slave2服务器配置
slave2 安装半同步模块
mysql> install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> show plugins;
+---------------------------------+----------+--------------------+-------------------+---------+
| Name | Status | Type | Library | License |
+---------------------------------+----------+--------------------+-------------------+---------+
| binlog | ACTIVE | STORAGE ENGINE | NULL | GPL |
| mysql_native_password | ACTIVE | AUTHENTICATION | NULL | GPL |
| sha256_password | ACTIVE | AUTHENTICATION | NULL | GPL |
| caching_sha2_password | ACTIVE | AUTHENTICATION | NULL | GPL |
| sha2_cache_cleaner | ACTIVE | AUDIT | NULL | GPL |
| daemon_keyring_proxy_plugin | ACTIVE | DAEMON | NULL | GPL |
| CSV | ACTIVE | STORAGE ENGINE | NULL | GPL |
| MEMORY | ACTIVE | STORAGE ENGINE | NULL | GPL |
| InnoDB | ACTIVE | STORAGE ENGINE | NULL | GPL |
| INNODB_TRX | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_CMP | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_CMP_RESET | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_CMPMEM | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_CMPMEM_RESET | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_CMP_PER_INDEX | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_CMP_PER_INDEX_RESET | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_BUFFER_PAGE | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_BUFFER_PAGE_LRU | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_BUFFER_POOL_STATS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_TEMP_TABLE_INFO | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_METRICS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_FT_DEFAULT_STOPWORD | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_FT_DELETED | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_FT_BEING_DELETED | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_FT_CONFIG | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_FT_INDEX_CACHE | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_FT_INDEX_TABLE | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_TABLES | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_TABLESTATS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_INDEXES | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_TABLESPACES | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_COLUMNS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_VIRTUAL | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_CACHED_INDEXES | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_SESSION_TEMP_TABLESPACES | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| MyISAM | ACTIVE | STORAGE ENGINE | NULL | GPL |
| MRG_MYISAM | ACTIVE | STORAGE ENGINE | NULL | GPL |
| PERFORMANCE_SCHEMA | ACTIVE | STORAGE ENGINE | NULL | GPL |
| TempTable | ACTIVE | STORAGE ENGINE | NULL | GPL |
| ARCHIVE | ACTIVE | STORAGE ENGINE | NULL | GPL |
| BLACKHOLE | ACTIVE | STORAGE ENGINE | NULL | GPL |
| FEDERATED | DISABLED | STORAGE ENGINE | NULL | GPL |
| ngram | ACTIVE | FTPARSER | NULL | GPL |
| mysqlx_cache_cleaner | ACTIVE | AUDIT | NULL | GPL |
| mysqlx | ACTIVE | DAEMON | NULL | GPL |
| rpl_semi_sync_slave | ACTIVE | REPLICATION | semisync_slave.so | GPL |
+---------------------------------+----------+--------------------+-------------------+---------+
46 rows in set (0.01 sec)
修改slave2配置文件
[root@145 /]# cat /etc/my.cnf
#
# This group is read both both by the client and the server
# use it for options that affect everything
#
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
[client]
user=root
password=123456
[mysqld]
server-id=145
log_bin
read-only
rpl_semi_sync_slave_enabled=on
[root@145 /]# systemctl restart mysqld
查看半同步服务
mysql> select @@rpl_semi_sync_slave_enabled;
+-------------------------------+
| @@rpl_semi_sync_slave_enabled |
+-------------------------------+
| 1 |
+-------------------------------+
1 row in set (0.00 sec)
5. 用mycat实现mysql的读写分离(在主从同步的基础上实现)
主:131 从:130 mycat:132
1.配置主从服务器131 和130
[root@131 ~]# cat /etc/my.cnf
#
# This group is read both both by the client and the server
# use it for options that affect everything
#
[client-server]
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
[client]
user=root
password=123456
[mysqld]
server-id=131
log-bin= /data/mysql/log/mysql-bin
[root@130 /]# cat /etc/my.cnf
#
# This group is read both both by the client and the server
# use it for options that affect everything
#
[client-server]
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
[client]
user=root
password=123456
[mysqld]
server_id=130
log-bin = /data/mysql/log/mysql.bin
#read_only=ON
mycat 132配置
1.安装jiava
yum -y install java
[root@129 ~]# java -version
openjdk version "1.8.0_392"
OpenJDK Runtime Environment (build 1.8.0_392-b08)
OpenJDK 64-Bit Server VM (build 25.392-b08, mixed mode)
[root@132 /]# mkdir /apps
[root@132 ~]# tar xvf Mycat-server-1.6.7.6-release-20210303094759-linux.tar.gz -C /apps
root@132 ~]# ls /apps/mycat/
bin catlet conf lib logs version.txt
2.配置mycat环境变量
[root@132 ~]# echo 'PATH=/apps/mycat/bin:$PATH' > /etc/profile.d/mycat.sh
[root@132 ~]# source /etc/profile.d/mycat.sh
[root@132 ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
3.启动mycat
[root@132 ~]# file /apps/mycat/bin/mycat
/apps/mycat/bin/mycat: POSIX shell script, ASCII text executable
[root@132 ~]# mycat
Usage: /apps/mycat/bin/mycat { console | start | stop | restart | status | dump }
[root@132 ~]# mycat start
Starting Mycat-server...
[root@132 ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 1 127.0.0.1:32000 0.0.0.0:*
LISTEN 0 128 *:9066 *:*
LISTEN 0 50 *:33941 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 50 *:1984 *:*
LISTEN 0 50 *:33505 *:*
LISTEN 0 128 *:8066 *:*
[root@132 ~]# #tail /apps/mycat/logs/wrapper.log
[root@132 ~]# tail /apps/mycat/logs/wrapper.log
STATUS | wrapper | 2024/01/07 13:34:38 | --> Wrapper Started as Daemon
STATUS | wrapper | 2024/01/07 13:34:38 | Launching a JVM...
INFO | jvm 1 | 2024/01/07 13:34:38 | Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org
INFO | jvm 1 | 2024/01/07 13:34:38 | Copyright 1999-2006 Tanuki Software, Inc. All Rights Reserved.
INFO | jvm 1 | 2024/01/07 13:34:38 |
INFO | jvm 1 | 2024/01/07 13:34:41 | MyCAT Server startup successfully. see logs in logs/mycat.log
4.链接mycat
[root@132 ~]# mysql -uroot -p123456 -h 192.168.29.132 -P8066
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.29-mycat-1.6.7.6-release-20210303094759 MyCat Server (OpenCloudDB)
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+----------+
| DATABASE |
+----------+
| TESTDB |
+----------+
1 row in set (0.00 sec)
修改mycat配置文件
1.修改mycat 端口号将默认的8066改为3306
[root@132 ~]# vim /apps/mycat/conf/server.xml
<property name="processorBufferPoolType">0</property>
<property name="serverPort">3306</property
[root@132 ~]# mycat restart
Stopping Mycat-server...
Stopped Mycat-server.
Starting Mycat-server...
[root@132 ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 1 127.0.0.1:32000 0.0.0.0:*
LISTEN 0 128 *:3306 *:*
LISTEN 0 128 *:9066 *:*
LISTEN 0 50 *:44819 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 50 *:1984 *:*
LISTEN 0 50 *:37409 *:*
2.配置master131 创建mycat映射账号并授权
mysql> create user 'mycat'@'192.168.29.%' identified by '123456';
Query OK, 0 rows affected (0.01 sec)
mysql> grant all on hellodb.* to 'mycat'@'192.168.29.%';
Query OK, 0 rows affected (0.00 sec)
3.修改mycat132 schema 映射配置
[root@132 ~]# cat schema.xml
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1">
</schema>
<dataNode name="dn1" dataHost="localhost1" database="hellodb" />
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="1"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="host1" url="192.168.29.131:3306" user="mycat" password="123456"> #master 131
<readHost host="host2" url="192.168.29.130:3306" user="mycat" password="123456" /> #slave 130
</writeHost>
</dataHost>
</mycat:schema>
验证mycat
1.master 131和 slave 130开启通用日志
[root@131 ~]# cat /etc/my.cnf
#
# This group is read both both by the client and the server
# use it for options that affect everything
#
[client-server]
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
[client]
user=root
password=123456
[mysqld]
server-id=131
log-bin= /data/mysql/log/mysql-bin
general_log
[root@131 ~]# systemctl restart mysqld
[root@130 /]# cat /etc/my.cnf
#
# This group is read both both by the client and the server
# use it for options that affect everything
#
[client-server]
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
[client]
user=root
password=123456
[mysqld]
server_id=130
log-bin = /data/mysql/log/mysql.bin
#read_only=ON
general_log
[root@130 /]# systemctl restart mysqld
2.在mycat132上查询和修改:
mysql> select * from teachers;
+-----+---------------+-----+--------+
| TID | Name | Age | Gender |
+-----+---------------+-----+--------+
| 1 | Song Jiang | 45 | M |
| 2 | Zhang Sanfeng | 94 | M |
| 3 | Miejue Shitai | 77 | F |
| 4 | Lin Chaoying | 93 | F |
+-----+---------------+-----+--------+
4 rows in set (0.00 sec)
mysql> update teachers set age=60 where TID=1;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from teachers;
+-----+---------------+-----+--------+
| TID | Name | Age | Gender |
+-----+---------------+-----+--------+
| 1 | Song Jiang | 60 | M |
| 2 | Zhang Sanfeng | 94 | M |
| 3 | Miejue Shitai | 77 | F |
| 4 | Lin Chaoying | 93 | F |
+-----+---------------+-----+--------+
4 rows in set (0.00 sec)
3.分别在master131和slave130的通用日志上比较
[root@131 ~]# cat /var/lib/mysql/131.log
2024-01-07T07:03:59.325794Z 8 Query select user()
2024-01-07T07:04:08.356940Z 10 Query update teachers set age=60 where TID=1
2024-01-07T07:04:09.318810Z 12 Query select user()
2024-01-07T07:04:19.322645Z 11 Query select user()
[root@130 /]# cat /var/lib/mysql/130.log
2024-01-07T07:04:09.327203Z 14 Query select user()
2024-01-07T07:04:13.258357Z 17 Query select * from teachers
2024-01-07T07:04:19.330838Z 16 Query select user()
结论mycat实现读写分离成功
6. 实现openvpn部署,并且测试通过,输出博客或者自己的文档存档。
环境配置共4台主机
openvpn eth0NAT模式:192.168.29.130 eth1仅主机模式:192.168.87.128
web1仅主机模式: 192.168.87.129
web2仅主机模式: 192.168.87.130
windows11
7、mysql如何实现崩溃后恢复?
MySQL数据库在崩溃后可以通过以下几种方式来实现恢复:
1.启用二进制日志(Binary Log):MySQL提供了二进制日志功能,它记录了所有数据库操作语句的日志。在崩溃后,可以使用二进制日志进行恢复。需要确保在MySQL配置文件中启用了二进制日志,并定期备份二进制日志文件。
2.使用事务日志(InnoDB存储引擎):如果使用的是InnoDB存储引擎,它提供了事务日志(也称为重做日志)功能。事务日志记录了正在进行中的事务操作,包括对表的修改。在崩溃后,MySQL可以使用事务日志进行恢复。
3.备份与恢复:定期备份MySQL数据库是非常重要的。使用数据库备份工具,如mysqldump命令行工具或第三方xtrabackup备份工具,可以将数据库备份到另一个存储位置。在崩溃后,可以使用备份文件来还原数据库。
4.使用故障转移与复制功能:MySQL支持主从复制和主主复制功能。通过设置主从复制或主主复制,可以将数据复制到多个服务器上。当主服务器崩溃时,可以使用其中一个从服务器继续提供服务。
8、myisam和innodb各自在什么场景使用?
MyISAM和InnoDB是MySQL数据库中两种常见的存储引擎。
MyISAM适用于以下场景:
对于读密集型应用:MyISAM在处理大量的SELECT操作时性能较好,因为它使用了表级锁定而不是行级锁定,这意味着在读取数据时不会出现锁冲突。
需要全文搜索功能:MyISAM支持全文索引,使得在文本数据上进行高效的全文搜索成为可能。
插入和查询的比例较低:由于MyISAM会在执行写操作时锁定整个表,所以在插入和查询的比例较低的应用中,效果更好。
InnoDB适用于以下场景:
对于写密集型应用:InnoDB在处理大量的INSERT和UPDATE操作时性能较好,因为它使用了行级别的锁定,可以避免锁冲突,提高并发性能。
需要事务支持:InnoDB是MySQL的默认事务存储引擎,支持ACID(原子性、一致性、隔离性和持久性)事务,可以确保数据的完整性和一致性。
强调数据的安全性:InnoDB支持外键约束和崩溃恢复机制,可以保证数据的安全性和稳定性。