标签: MySQL

  • MySQL8 GTID双主配置

    MySQL8 GTID双主配置

    记录一下MySQL8中配置GTID双主的方式。

    需要添加以下配置信息:

    vim /etc/my.cnf
    # 添加
    [mysqld]
    # 两台服务器的server-id不能一致
    server-id=1
    gtid_mode=on
    enforce-gtid-consistency=true
    

    具体使用到的SQL语句:

    # 创建一个账号用于另一台主机复制数据
    # create user 'slave'@'0.0.0.0' identified by 'this is your password';
    
    # 授权
    grant REPLICATION SLAVE on *.* to 'slave'@'0.0.0.0';
    
    # 查看主机状态
    SHOW MASTER STATUS;
    
    # 以下为另一台服务器执行内容
    CHANGE REPLICATION SOURCE to
        SOURCE_HOST = '0.0.0.0',
        SOURCE_PORT = 3306,
        SOURCE_USER = 'slave',
        SOURCE_PASSWORD = 'this is your password';
    
    # 停止并重置复制
    STOP REPLICA;
    reset REPLICA;
    
    # 开始复制并查看复制状态
    START REPLICA;
    SHOW REPLICA STATUS;
    
  • MySQL给已存在的主键字段添加自增AUTO_INCREMENT

    MySQL给已存在的主键字段添加自增AUTO_INCREMENT

    每次都记不起来,记录一下…

    # 添加自增约束
    alter table table_name modify column COLUMN_NAME COLUMN_TYPE auto_increment;
    # 配置自增起始值
    alter table table_name auto_increment=10000;
    
  • MySQL创建分区表

    MySQL创建分区表

    背景:一个记录表,类似日志的信息,查询大量集中在某个用户个人的数据,分区需要尽量保证一个人的数据在一个分区里。因此采用通过user_id进行hash分区的方式。

    -- 将分区字段添加为主键
    alter table logs modify column id int not null;
    alter table logs drop primary key;
    alter table logs add primary key(id, user_id);
    alter table logs modify column id int not null auto_increment;
    
    -- 创建带分区的表
    CREATE TABLE `logs_withs_partitions` (
      `id` int NOT NULL AUTO_INCREMENT,
      `user_id` int NOT NULL,
      ...
      PRIMARY KEY (`id`,`user_id`)
    ) PARTITION BY HASH(user_id) PARTITIONS 5;
    -- 将数据复制到带分区的表
    insert into logs_withs_partitions
    select * from logs;
    
    -- 重命名表
    rename table logs to logs_without_partitions;
    rename table logs_withs_partitions to logs;
    
    -- 删除不带分区的表
    drop table logs_without_partitions;
    
  • MySQL多层级树形结构表的搜索查询优化

    MySQL多层级树形结构表的搜索查询优化

    业务中有思维导图的功能,涉及到大量的树形结构搜索、查询相关的功能,使用场景上查询量远高于增删改操作,记录一下当前的解决方案。

    一、表结构

    简化的表结构类似

    create table nodes (
      id int primary key auto_increment,
      name varchar(255) not null default '' comment '节点名称',
      parent_id int not null default 0 comment '上级节点',
    
      index nodes_parent_id_index (parent_id),
      index nodes_name_index (name)
    );
    

    二、当前解决方案

    更新表结构:

    -- 添加字段
    alter table nodes add column path text not null comment '节点路径';
    
    -- 创建索引
    create index nodes_path_index on nodes(path);
    
    -- 更新历史数据
    update nodes current
    left join nodes parent on current.parent_id = parent.id
    set path = ifnull(concat(parent.path, ',', current.parent_id), '0');
    
    -- 插入更新后执行
    update nodes current
    left join nodes parent on current.parent_id = parent.id
    set path = ifnull(concat(parent.path, ',', current.parent_id), '0');
    where current.id = 198;
    
    -- 级联删除
    delete from nodes where id = 198;
    delete from nodes where (path like '0,5,198,%' and parent_id = 198);
    

    1. 查询ID为“5”的节点的所有子级、孙子级中name包含“搜索词”的记录

    更新表后的查询方式:

    -- 查询父级节点记录,获取到父级的path
    select * from nodes where id = 5;
    
    -- 通过父级path进行模糊查询
    select * from nodes where (parent_id = 5 or path like '0,5,%') and name like '%搜索词%';
    

    可以创建一个触发器,在插入、修改数据时,更新子级的path。

    2. 查询ID为“5”的节点的所有父级

    -- 获取当前节点
    select * from nodes where id = 5;
    
    -- 使用当前节点的path查询所有父级
    select * from nodes where find_in_set(id, '0,5');
    
    -- 或者也可以使用in
    select * from nodes where id in (5);
    

    因为有缓存,所以都尽量使用的简单查询,不使用缓存可以使用子查询。

  • MySQL常用SQL语句

    MySQL常用SQL语句

    记录一些常用的MySQL语句,方便查找翻阅。

    (更多…)

  • MySQL8 GTID双主配置

    MySQL8 GTID双主配置

    记录一下MySQL8中配置GTID双主的方式。

    (更多…)

  • MySQL给已存在的主键字段添加自增AUTO_INCREMENT

    MySQL给已存在的主键字段添加自增AUTO_INCREMENT

    每次都记不起来,记录一下…

    # 添加自增约束
    alter table table_name modify column COLUMN_NAME COLUMN_TYPE auto_increment;
    # 配置自增起始值
    alter table table_name auto_increment=10000;
  • MySQL多层级树形结构表的搜索查询优化

    业务中有思维导图的功能,涉及到大量的树形结构搜索、查询相关的功能,使用场景上查询量远高于增删改操作,记录一下当前的解决方案。

    (更多…)

  • MySQL创建分区表相关

    背景:一个记录表,类似日志的信息,查询大量集中在某个用户个人的数据,分区需要尽量保证一个人的数据在一个分区里。因此采用通过user_id进行hash分区的方式。

    (更多…)

  • MySQL配置及优化

    一、配置项及意义

    配置项 意义 建议
    innodb_buffer_pool_size 控制 InnoDB 存储引擎的内存缓存池大小 总内存(非专用服务器可以使用希望分配给数据库的内存量)的 50% 到 80%
    innodb_buffer_pool_chunk_size  InnoDB 缓冲池大小调整操作的块大小,默认值为 128MB innodb_buffer_pool_size = innodb_buffer_pool_chunk_size * innodb_buffer_pool_instances
    innodb_buffer_pool_instances 决定了 InnoDB 缓冲池(Buffer Pool)的实例数量 设置为 CPU 核心数的较小倍数,超过 1GB 时可以适量增加,最高不超过1000
    innodb_buffer_pool_in_core_file 从核心文件中排除缓冲池页面,参考15.8.3.7 从核心文件中排除缓冲池页面_MySQL 8.0 参考手册 off
    innodb_flush_log_at_trx_commit 定义了事务提交时,InnoDB 如何处理未刷入(flush)的重做日志(redo log),默认值为1,设置为0/2,在操作系统崩溃或断电时可能会丢失最后一秒的事务数据 对于需要高可靠性和数据完整性的系统设置为 1,对于性能要求更高,且可以接受一定数据丢失风险的系统设置为 0 或 2。
    innodb_change_buffering 控制着 InnoDB 执行变更缓冲(Change Buffering)的程度 none
    innodb_log_buffer_size 设置InnoDB日志缓冲区的大小 64m
    innodb_flush_method 决定了 InnoDB 如何将数据和日志刷新(flush)到磁盘 O_DIRECT
    innodb_purge_threads 定义了用于回收不再需要的 UNDO 日志的 Purge 线程的数量 cpu > 4 ? 4 : 1