分类目录归档:Mysql

[ 数据库字段长度截取 ]


可以,SQL 层面可以直接截取 body 字段的前 N 个字符,避免拉取全部内容再截断。 不同数据库有不同的字符串截取函数:

MySQL 用 LEFT(body, 200) PostgreSQL 用 SUBSTRING(body FROM 1 FOR 200)

golang

    sql := fmt.Sprintf(`
        SELECT 
            id,
            title,
            LEFT(body, 300) AS body,
            created_time,
            last_mod_ti

< Read more >

[ mysql修改事务隔离级别 ]


mysql> select @@tx_isolation;
+-----------------+
| @@tx_isolation  |
+-----------------+
| REPEATABLE-READ |
+-----------------+
1 row in set, 1 warning (0.00 sec)

mysql> set session transaction isolation level read committed;
Query OK, 0 rows affected (0.00 sec)
  • 与乐观锁配合使用READ-COMMITTED 读取

< Read more >

[ mysql开启binlog ]


my.cnf

[mysqld]
port=3306
server-id=1
log-bin=mysql-bin
binlog_format=row
binlog-do-db=shopping_mall_main

[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8mb4
[client]
# 设置mysql客户端连接服务端时默认使用的端口
port=3306
default-character-set=utf8mb4

docker-compose.yml

version: '3'

services:
  db:
   

< Read more >

[ Mysql测试用 ]


INSERT INTO `tb_user` (`password`, `last_login`, `is_superuser`, `username`, `first_name`, `last_name`, `email`, `is_staff`, `is_active`, `date_joined`, `create_time`, `update_time`, `version`, `delete_token`, `phone`, `gender`, `token`, `status`)
VALUES ('pbkdf2_sha256$390000$GUWtpz0MfJtNvRGzVI

< Read more >

[ Mysql8用户连接配置 ]


  1. 先使用mysql数据库 use mysql;

  2. 运行远程连接 GRANT ALL ON . TO 'root'@'%'; GRANT REPLICATION SLAVE ON . TO 'root'@'%';

  3. 查看所允许的访问地址

  4. 修改所允许的访问地址,即可 update user set host='%' where user='root';

  5. 查看密码加密方式 select host, user, plugin from user;

  6. 修改密码的加密方式 alter user 'root'@'%' identified with mysql_native_passwor

< Read more >

[ Mysql事务 ]


事务嵌套的两种方法

参考事务操作

-- 第一个事务 --
BEGIN;
    update yyy;
        -- 第两个事务 ---
        begin;
            update xxx;
        commit;
    update zzz;
commit;

注意事项:

正常情况下mysql不存在事务的嵌套 当第一个事务执行, 遇到了第二个事务begin等代码 就会自动把第一个事务提交,去执行第二个事务 执行完第二个事务, 后面的代码相当于是没有事务了 导致割裂

场景1

  1. 融入外界的事务

    相当于把内部事务的begin, commit删除了 相

< Read more >

[ Mysql数据库备份 ]


基本操作

mysqldump -u root -p[密码] --databases 库名1 [库名2] ... > /备份路径/备份文件名.sql   
#导出的就是数据库脚本文件

例:
mysqldump -u root -p --databases liu > /opt/kgc.sql       #备份一个kgc库
mysqldump -u root -p --databases mysql li > /opt/mysql-kgc.sql    #备份mysql与 kgc两个库

尝试使用sh脚本备份docker里的mysql

# 保留10天数据,
# mysql8

< Read more >

[ Mysql多对多SQL写法 ]


多对多(权限abrc)

表结构

1. 用户表 user

2. 用户&角色-中间表 user_role

3. 角色表 role

4. 角色&权限-中间表 role_power

5. 权限表 power

数据库查询**

使用内join查询(5连表)

  • user左连
  • user_role连
  • role连
  • role_power连
  • power
select `user`.id as user_id, `user`.username as 用户名, `role`.name as 角色, `power`.name as 权限
from `user`  
    left join (`user

< Read more >

[ Mysql慢查询日志配置 ]


慢查询日志配置

1. 开启功能

查询是否开启
SHOW VARIABLES LIKE ‘slow_query%’;

查询超时时间
SHOW VARIABLES LIKE ‘long_query_time%’;

方式一:配置文件开启(永久有效)

将 log_slow_queries 选项和 long_query_time 选项加入到配置文件的 [mysqld] 组中。格式如下:

[mysqld]
show_query_log=on/off  # 开启或关闭
log-slow-queries=dir/filename
long_query_time=n       # 忍耐时间
log_ou

< Read more >