MySQL记录

MySQL记录

建库授权

1
2
3
4
CREATE DATABASE IF NOT EXISTS gunslite DEFAULT CHARSET utf8mb4 COLLATE utf8mb4_general_ci;
CREATE USER 'gunslite'@'%' IDENTIFIED BY 'gunslite123';
GRANT ALL privileges ON gunslite.* TO 'gunslite'@'%';
flush privileges;

字符集相关

  • 查看表字段字符集

    1
    SHOW FULL COLUMNS FROM process_model;
  • 更改表字段字符集

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    --单字段
    ALTER TABLE model_category CHANGE id id VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
    --全表
    ALTER TABLE model_category CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;


    --批量修改
    SELECT
    CONCAT("ALTER TABLE `", TABLE_NAME,"` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;")
    AS target_tables
    FROM INFORMATION_SCHEMA.TABLES
    WHERE TABLE_SCHEMA="oa"
    AND TABLE_TYPE="BASE TABLE"

查询优化分析

  1. explain

    1
    explain sql

    性能由高到低

    system>const>eq_ref>ref>fulltext>ref_or_null>index_merge>unique_subquery>index_subquery>range>index>ALL

    Ref:https://dev.mysql.com/doc/refman/8.0/en/explain-output.html

    Column JSON Name Meaning
    id select_id The SELECT identifier
    select_type None The SELECT type
    table table_name The table for the output row
    partitions partitions The matching partitions
    type access_type The join type
    possible_keys possible_keys The possible indexes to choose
    key key The index actually chosen
    key_len key_length The length of the chosen key
    ref ref The columns compared to the index
    rows rows Estimate of rows to be examined
    filtered filtered Percentage of rows filtered by table condition
    Extra None Additional information
  2. profile

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    # 会话级别开启
    set profiling = 1;
    # 开启最大记录数
    set profiling_history_size = 100;
    # 执行sql
    SELECT *
    from app_model
    ORDER BY sort ASC;

    # 查看记录
    show profiles;
    # 查询某一条语句的记录
    show profile all for query 405;
    # 关闭记录
    set profiling = 0;

    show processlist;

Ref:

https://tech.meituan.com/2014/06/30/mysql-index.html

https://blog.csdn.net/everda/article/details/77476716?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase

https://www.cnblogs.com/hubavyn/p/4359315.html

https://mengkang.net/1124.html

连接配置

  1. 连接失败

    Error (2013): Lost connection to MySQL server at waiting for initial communication pack

    client:172.17.150.60 宿主机:172.17.150.61 docker0:182.18.0.1/21

    • 原因分析:
      mysql 开启了 DNS 的反向解析功能,这样 mysql 对连接的客户端会进行 DNS 主机名查找。
      mysql 处理客户端解析过程:
      1)当 mysql 的 client 连过来的时候,服务器会主动去查 client 的域名。
      2)首先查找 /etc/hosts 文件,搜索域名和 IP 的对应关系。
      3)如果 hosts 文件没有,则查找 DNS 设置,进行 DNS 反向解析,直到 timeout 连接失败。
      mysql 的 DNS 反向解析:
      1)mysql 接收到连接请求后,获得的是客户端的 ip,为了更好的匹配 mysql.user 里的权限记录(某些是用 hostname 定义的)。
      2)如果 mysql 服务器设置了 dns 服务器,并且客户端 ip 在 dns 上并没有相应的 hostname,那么这个过程很慢,导致连接等待。

    • 解决方案:
      1)把 client 的 ip 写在 mysql 服务器的/etc/hosts 文件里,随便给个名字做主机映射即可。
      2)在 my.cnf 配置文件中的[mysqld]区域添加 skip-name-resolve,即跳过 mysql 连接的 DNS 反向解析功能,这样能很好地提高 mysql 性能。在这种情况下,就只能使用 MySQL 授权表中的 IP 来连接 mysql 服务了。
      对于第一种方法显然比较笨,也不实用!强烈推荐第二种方法,添加 skip-name-resolve 选项可以禁用 dns 解析,这样的话,就不能在 mysql 的授权表中使用主机名了,只能使用 IP。
      另外:
      如果在 my.cnf 文件中配置了 bind-address 地址绑定的地址(说明别的机器远程只能通过这个绑定的本机地址来连接 mysql),可以将其注释掉。
      例如:
      bind-address = 127.0.0.1 //说明只能在本机连接 mysql,并且通过-h 127.0.0.1 或 localhost,在远程是无法连接这个 mysql 的!

    • my.cnf 配置的几个参数:
      skip-name-resolve 跳过 DNS 反向解析过程.(这样就不能使用主机名连接 mysql 了,只能使用 ip 连接)
      skip-grant-tables 跳过授权表(当 mysql 登陆密码忘记时的解决办法)
      skip-networking 跳过 TCP/IP 连接
      skip-host-cache 禁用主机名缓存;要想清除主机名缓存,执行 FLUSH HOSTS 语句或执行 mysqladmin flush-hosts 命令

    Ref:

    https://www.cnblogs.com/kevingrace/p/6196885.html

作者

Heng.Wang

发布于

2020-06-22

更新于

2023-09-20

许可协议

CC BY-NC-SA 4.0

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×