• 2-MySQL的基本操作记录


    1 数据库相关

    1. -- --------------------表相关的----------
    2. -- 查看字符集
    3. show variables like '%character%';
    4. show databases;
    5. # 创建数据库
    6. create database test2;
    7. # 删除数据库
    8. drop database test2;
    9. show databases;
    10. #查看当前使用的数据库
    11. select database();

    2 用户相关

    1. -- --------------------用户授权相关的----------
    2. #切换数据库
    3. use test;
    4. select user();
    5. # 会报错:[42000][1064] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'identified by 'user12345'' at line
    6. # mysql8.0版本为新版本,需要先创建用户再分配权限,不能和旧版本一样
    7. # MySQL5.7版本可以使用
    8. grant all on test.* to 'user1'@'%' identified by 'user12345';
    9. FLUSH PRIVILEGES;
    10. # mysql8.0版本上需要使用:
    11. # 1、创建新用户
    12. create user 'user1'@'%' identified by 'user12345';
    13. # 2、刷新权限
    14. flush privileges;
    15. # 3、赋予权限,%标识不限制主机
    16. grant all on test.* to 'user1'@'%';
    17. # 4、刷新权限
    18. flush privileges;
    19. # 5、查看用户
    20. select host,user,plugin,authentication_string from mysql.user;
    21. # 收回权限
    22. revoke insert on test.* from 'user1'@'%';
    23. # 删除用户
    24. drop user 'user1'@'%';
    25. # 查看权限,all包含的权限有:SELECT,INSERT, UPDATE, DELETE, CREATE, DROP, REFERENCES, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, EVENT, TRIGGER
    26. show grants for 'user1'@'%';

    3 数据表相关

    1. -- --------------------表相关的----------
    2. CREATE TABLE business_logs (
    3. id BIGINT PRIMARY KEY AUTO_INCREMENT,
    4. timestamp DATETIME NOT NULL,
    5. user_id INT NOT NULL,
    6. action VARCHAR(255) NOT NULL,
    7. description TEXT,
    8. ip_address VARCHAR(50),
    9. log_level VARCHAR(20)
    10. );
    11. -- 查看建表语句
    12. show create table business_logs;
    13. -- 查看所有表
    14. show tables;
    15. -- 查看表结构
    16. desc business_logs;
    17. select *
    18. from business_logs;
    19. -- 插入数据到表中
    20. insert into business_logs (timestamp, user_id, action, description) values (sysdate(), 1, 'cap', '查询成功');
    21. insert into business_logs (timestamp, user_id, action, description) values (sysdate(), 2, 'del', '删除成功');
    22. insert into business_logs (timestamp, user_id, action, description) values (sysdate(), 1, 'add', '添加成功');
    23. -- 更新表
    24. update business_logs set action = 'delete', description = '删除成功' where id = 1;
    25. -- 删除表中的数据
    26. delete from business_logs where id = 1;
    27. -- 查询语句
    28. /*
    29. 查询关键字:
    30. select
    31. 字段列表
    32. from
    33. 表名
    34. where
    35. 查询条件
    36. group by
    37. 分组字段
    38. having
    39. 分组后条件字段
    40. order by
    41. 排序字段
    42. limit
    43. 分页参数
    44. */

    3.1 聚合函数

    聚合函数(count、sum、max、min、avg)使用

    1. -- 删除表
    2. drop table if exists user;
    3. -- 创建表
    4. create table user (
    5. id int primary key auto_increment,
    6. name varchar(20) not null,
    7. age int not null,
    8. city varchar(20) default null
    9. );
    10. -- 插入数据
    11. insert into user (name, age, city) values ('张三', 20, '北京'), ('李四', 22, '上海'), ('王五', 20, '北京');
    12. select * from user;
    13. -- 查询各个城市的人数
    14. select city, count(*) as usr_num from user group by city;
    15. -- 查询各个城市最小的年龄
    16. select city, min(age) as age from user group by city;
    17. -- 查询平均年龄
    18. select avg(age) as age from user;
    19. -- 查询年龄总和
    20. select sum(age) as sum_age from user;

    注意:所有的null值不参与聚合函数的运算

    3.2 分组查询

    基本语法

    select 字段 from 表名 [where 条件] group by 分组字段 [having 分组后过滤条件]

    where和having的区别

    • 执行时机不同:where是分组之前进行过滤,不满足where条件的数据不参与分组,而having是分组之后对结果进行过滤
    • 判断条件不同:where不能对聚合函数进行判断,而having可以
    1. insert into user (name, age, city) values ('张三2', 20, '北京'), ('李四2', 22, '上海'), ('王五2', 20, '北京');
    2. insert into user (name, age, city) values ('夏明', 30, '北京'), ('李红', 32, '北京'), ('张飞', 56, '西安');
    3. -- 查询年龄小于等于30的人员
    4. select * from user where age <= 30;
    5. -- 查询各个城市中,年龄小于30的人数
    6. select city, count(*) as user_num from user where age <= 30 group by city;
    7. -- 查询年龄小于等于30的人员,按照城市进行分组,获取人员数量大于3的城市
    8. select city, count(*) as user_num from user where age <= 30 group by city having user_num > 3;

    注意:

    • 执行顺序:where > 聚合函数 > having
    • 分组之后,查询的字段一般为聚合函数和分组字段,其他字段不能查询

    3.3 排序查询

    语法

    select 字段 from 表名 order by 字段1 排序1, 字段2 排序2;

    • 支持多字段排序
    • 排序方式
      • ASC:升序(默认值)
      • DESC:降序
    1. -- 根据年龄进行排序
    2. select * from user order by age;
    3. -- 根据年龄进行排序,年龄相同时根据id倒序排序
    4. select * from user order by age, id desc;

    3.4 分页查询

    使用 limit 关键字

    语法

    select 字段 from 表名 limit 起始索引, 查询记录数量;

    1. -- 查询第1页,每页显示5
    2. select * from user limit 0,5;
    3. -- 查询第2页,每页显示5
    4. select * from user limit 5,5;

    4 其他命令

    4.1 查看是否忽略大小写,1为忽略

    show global variables like '%lower_case%';

    4.2 查看主从状态

    查看主服务状态,在主从配置时,查看主服务信息时使用

    show master status;

    查看从服务状态,在主从配置时,查看从服务状态时使用

    show slave status;

    使用示例:

    1. mysql> show slave status \G
    2. *************************** 1. row ***************************
    3. Slave_IO_State: Waiting for source to send event
    4. Master_Host: 10.0.24.10
    5. Master_User: slave
    6. Master_Port: 3306
    7. Connect_Retry: 60
    8. Master_Log_File: binlog.000007
    9. Read_Master_Log_Pos: 2050
    10. Relay_Log_File: 5d5ab079a223-relay-bin.000004
    11. Relay_Log_Pos: 546
    12. Relay_Master_Log_File: binlog.000007
    13. Slave_IO_Running: Yes
    14. Slave_SQL_Running: Yes
    15. Replicate_Do_DB:
    16. Replicate_Ignore_DB:
    17. Replicate_Do_Table:
    18. Replicate_Ignore_Table:
    19. Replicate_Wild_Do_Table:
    20. Replicate_Wild_Ignore_Table:
    21. Last_Errno: 0
    22. Last_Error:
    23. Skip_Counter: 0
    24. Exec_Master_Log_Pos: 2050
    25. Relay_Log_Space: 1574
    26. Until_Condition: None
    27. Until_Log_File:
    28. Until_Log_Pos: 0
    29. Master_SSL_Allowed: No
    30. Master_SSL_CA_File:
    31. Master_SSL_CA_Path:
    32. Master_SSL_Cert:
    33. Master_SSL_Cipher:
    34. Master_SSL_Key:
    35. Seconds_Behind_Master: 0
    36. Master_SSL_Verify_Server_Cert: No
    37. Last_IO_Errno: 0
    38. Last_IO_Error:
    39. Last_SQL_Errno: 0
    40. Last_SQL_Error:
    41. Replicate_Ignore_Server_Ids:
    42. Master_Server_Id: 1
    43. Master_UUID: 60a0cb5e-1a2f-11ee-8244-0242ac110003
    44. Master_Info_File: mysql.slave_master_info
    45. SQL_Delay: 0
    46. SQL_Remaining_Delay: NULL
    47. Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
    48. Master_Retry_Count: 86400
    49. Master_Bind:
    50. Last_IO_Error_Timestamp:
    51. Last_SQL_Error_Timestamp:
    52. Master_SSL_Crl:
    53. Master_SSL_Crlpath:
    54. Retrieved_Gtid_Set:
    55. Executed_Gtid_Set:
    56. Auto_Position: 0
    57. Replicate_Rewrite_DB:
    58. Channel_Name:
    59. Master_TLS_Version:
    60. Master_public_key_path:
    61. Get_master_public_key: 0
    62. Network_Namespace:
    63. 1 row in set, 1 warning (0.00 sec)

    4.3 查看字符集

    1. -- 查看字符集
    2. show variables like '%character%';

    4.4 查看连接数

    1. -- 获取最大数据库连接数
    2. show variables like 'max_connections';
    3. -- 获取当前数据库连接数
    4. show status like 'threads_connected';
    5. -- 获取最大数据库连接数和当前数据库连接数
    6. select @@max_connections as maxconnections, count(*) as currentconnections from information_schema.processlist;

    4.5 查看当前进程

    1. show processlist;
    2. # 找出所有执行时间超过 5 分钟的线程,拼凑出 kill 语句,方便后面查杀
    3. select concat('kill ', id, ';')
    4. from information_schema.processlist
    5. where Command != 'Sleep' and Time > 300 order by Time desc;

    关注我,我们一起学习。

     

  • 相关阅读:
    Unity⭐️Win和Mac安卓打包环境配置
    uniapp app 导出excel 表格
    Flink之状态TTL机制
    Leetcode2937. 使三个字符串相等
    ArcGIS水文分析工具
    Python loglog()函数
    DBA 数据库管理 . 内连接.外链接
    Mysql之视图、索引【第五篇】
    HTML基础
    C++ atomic 和 memory ordering
  • 原文地址:https://blog.csdn.net/wmdkanh/article/details/133981159