You've already forked www.colben.cn
update
This commit is contained in:
@@ -7,68 +7,68 @@ categories: ["database"]
|
||||
---
|
||||
|
||||
## MySQL 5.7 配置文件 my.cnf
|
||||
```
|
||||
[mysqld]
|
||||
datadir = /db/mysql
|
||||
socket = /var/lib/mysql/mysql.sock
|
||||
symbolic-links = 0
|
||||
log-timestamps = SYSTEM
|
||||
slow-query-log = 1
|
||||
slow-query-log-file = /var/log/mysqld/slow.log
|
||||
long-query-time = 8
|
||||
#log-queries-not-using-indexes = 1
|
||||
log-error = /var/log/mysqld/error.log
|
||||
pid-file = /var/run/mysqld/mysqld.pid
|
||||
max-connections = 1000
|
||||
max-connect-errors = 1000
|
||||
max-user-connections = 600
|
||||
interactive-timeout = 3600
|
||||
wait-timeout = 3600
|
||||
skip-name-resolve = 1
|
||||
lower-case-table-names = 1
|
||||
default-time-zone = '+08:00'
|
||||
character-set-server = utf8mb4
|
||||
sql-mode = NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
|
||||
bind-address = 0.0.0.0
|
||||
table-open-cache = 2048
|
||||
default-storage-engine = innodb
|
||||
innodb-autoinc-lock-mode = 2
|
||||
innodb-flush-log-at-trx-commit = 0
|
||||
# 建议物理内存一半
|
||||
innodb-buffer-pool-size = 8G
|
||||
innodb-buffer-pool-instances = 8
|
||||
max-allowed-packet = 512M
|
||||
query-cache-size = 0
|
||||
query-cache-type = 0
|
||||
# 建议点分 ip 的最后一个数字
|
||||
server-id = 123
|
||||
# bin log
|
||||
#binlog-format = ROW
|
||||
#log-bin = /var/lib/mysql/mysql-bin
|
||||
#expire-logs-days = 3
|
||||
# relay log
|
||||
#read-only = 1
|
||||
#replicate-wild-do-table = db1.%
|
||||
#relay-log = /var/lib/mysql/mysql-relay-bin
|
||||
#slave-parallel-type = logical-clock
|
||||
```
|
||||
```
|
||||
[mysqld]
|
||||
datadir = /db/mysql
|
||||
socket = /var/lib/mysql/mysql.sock
|
||||
symbolic-links = 0
|
||||
log-timestamps = SYSTEM
|
||||
slow-query-log = 1
|
||||
slow-query-log-file = /var/log/mysqld/slow.log
|
||||
long-query-time = 8
|
||||
#log-queries-not-using-indexes = 1
|
||||
log-error = /var/log/mysqld/error.log
|
||||
pid-file = /var/run/mysqld/mysqld.pid
|
||||
max-connections = 1000
|
||||
max-connect-errors = 1000
|
||||
max-user-connections = 600
|
||||
interactive-timeout = 3600
|
||||
wait-timeout = 3600
|
||||
skip-name-resolve = 1
|
||||
lower-case-table-names = 1
|
||||
default-time-zone = '+08:00'
|
||||
character-set-server = utf8mb4
|
||||
sql-mode = NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
|
||||
bind-address = 0.0.0.0
|
||||
table-open-cache = 2048
|
||||
default-storage-engine = innodb
|
||||
innodb-autoinc-lock-mode = 2
|
||||
innodb-flush-log-at-trx-commit = 0
|
||||
# 建议物理内存一半
|
||||
innodb-buffer-pool-size = 8G
|
||||
innodb-buffer-pool-instances = 8
|
||||
max-allowed-packet = 512M
|
||||
query-cache-size = 0
|
||||
query-cache-type = 0
|
||||
# 建议点分 ip 的最后一个数字
|
||||
server-id = 123
|
||||
# bin log
|
||||
#binlog-format = ROW
|
||||
#log-bin = /var/lib/mysql/mysql-bin
|
||||
#expire-logs-days = 3
|
||||
# relay log
|
||||
#read-only = 1
|
||||
#replicate-wild-do-table = db1.%
|
||||
#relay-log = /var/lib/mysql/mysql-relay-bin
|
||||
#slave-parallel-type = logical-clock
|
||||
```
|
||||
|
||||
## 复制表结构
|
||||
```sql
|
||||
create table db1.t1 like db2.t2;
|
||||
create table db1.t1 select db2.t2 where 1=2;
|
||||
```
|
||||
```sql
|
||||
create table db1.t1 like db2.t2;
|
||||
create table db1.t1 select db2.t2 where 1=2;
|
||||
```
|
||||
|
||||
## 复制表结构及其数据
|
||||
```sql
|
||||
create table db1.t1 select db2.t2 [where ...]
|
||||
```
|
||||
```sql
|
||||
create table db1.t1 select db2.t2 [where ...]
|
||||
```
|
||||
|
||||
## 复制表数据
|
||||
```
|
||||
insert into db2.t2(column1, column2 ...)
|
||||
select column1, column2 ... from db1.t1 [where ...]
|
||||
```
|
||||
```
|
||||
insert into db2.t2(column1, column2 ...)
|
||||
select column1, column2 ... from db1.t1 [where ...]
|
||||
```
|
||||
|
||||
## 通过复制表文件来复制表数据
|
||||
- 在db2中创建同结构表
|
||||
@@ -98,45 +98,45 @@ categories: ["database"]
|
||||
```
|
||||
|
||||
## 设置一个表的空列自增
|
||||
```sql
|
||||
-- 删除可能存在的主键
|
||||
alter table 表名 drop primary key;
|
||||
alter table 表名 modify 列名 auto_increment primary key;
|
||||
```
|
||||
```sql
|
||||
-- 删除可能存在的主键
|
||||
alter table 表名 drop primary key;
|
||||
alter table 表名 modify 列名 auto_increment primary key;
|
||||
```
|
||||
|
||||
## 查看数据库中每个表的全部列名
|
||||
```sql
|
||||
select table_name, column_name from
|
||||
information_schema.columns
|
||||
where table_schema = '数据库名';
|
||||
```
|
||||
```sql
|
||||
select table_name, column_name from
|
||||
information_schema.columns
|
||||
where table_schema = '数据库名';
|
||||
```
|
||||
|
||||
## 查看数据库中每个表的行数
|
||||
```sql
|
||||
select table_name, table_rows
|
||||
from information_schema.tables
|
||||
where table_schema = '数据库名';
|
||||
```
|
||||
```sql
|
||||
select table_name, table_rows
|
||||
from information_schema.tables
|
||||
where table_schema = '数据库名';
|
||||
```
|
||||
|
||||
## 查看数据库中每个表的索引
|
||||
```sql
|
||||
select table_name, column_name, index_name
|
||||
from INFORMATION_SCHEMA.STATISTICS
|
||||
where table_schema = '数据库名';
|
||||
```
|
||||
```sql
|
||||
select table_name, column_name, index_name
|
||||
from INFORMATION_SCHEMA.STATISTICS
|
||||
where table_schema = '数据库名';
|
||||
```
|
||||
|
||||
## 表的部分列数据到另一个表
|
||||
```sql
|
||||
update db2.t2(column1, column2 ...) = (
|
||||
select column1, column2 from db1.t1
|
||||
where db1.t1.id = db2.t2.id);
|
||||
```
|
||||
```sql
|
||||
update db2.t2(column1, column2 ...) = (
|
||||
select column1, column2 from db1.t1
|
||||
where db1.t1.id = db2.t2.id);
|
||||
```
|
||||
|
||||
## 把语句执行结果写到文件
|
||||
```bash
|
||||
mysql -uroot -p -hsever_ip -Ddb_name
|
||||
-Ne "select ... from table_name;" > file_name
|
||||
```
|
||||
```bash
|
||||
mysql -uroot -p -hsever_ip -Ddb_name
|
||||
-Ne "select ... from table_name;" > file_name
|
||||
```
|
||||
|
||||
## 表分区
|
||||
- 查看表的分区情况
|
||||
@@ -210,47 +210,47 @@ categories: ["database"]
|
||||
```
|
||||
|
||||
## MySQL 5.7 从库多线程同步
|
||||
```sql
|
||||
stop slave;
|
||||
set global slave_parallel_type='logical_clock';
|
||||
set global slave_parallel_workers=4;
|
||||
start slave;
|
||||
show processlist;
|
||||
```
|
||||
```sql
|
||||
stop slave;
|
||||
set global slave_parallel_type='logical_clock';
|
||||
set global slave_parallel_workers=4;
|
||||
start slave;
|
||||
show processlist;
|
||||
```
|
||||
|
||||
## MySQL 5.7 提示密码复杂度不够
|
||||
```sql
|
||||
set global validate_password_policy=0;
|
||||
```
|
||||
```sql
|
||||
set global validate_password_policy=0;
|
||||
```
|
||||
|
||||
## MySQL 5.7 从库复制失败跳过指定数量的事务
|
||||
```sql
|
||||
stop slave;
|
||||
-- 跳过一个事务
|
||||
SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1;
|
||||
start slave;
|
||||
-- 修改 my.cnf
|
||||
slave-skip-errors=1062,1053,1146,1032 #跳过指定error no类型的错误
|
||||
slave-skip-errors=all #跳过所有错误
|
||||
```
|
||||
```sql
|
||||
stop slave;
|
||||
-- 跳过一个事务
|
||||
SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1;
|
||||
start slave;
|
||||
-- 修改 my.cnf
|
||||
slave-skip-errors=1062,1053,1146,1032 #跳过指定error no类型的错误
|
||||
slave-skip-errors=all #跳过所有错误
|
||||
```
|
||||
|
||||
## MySQL 5.7 查看全部任务
|
||||
```sql
|
||||
-- 分号换成 \G 显示完整 sql
|
||||
show processlist;
|
||||
show full processlist;
|
||||
SELECT command FROM information_schema.processlist;
|
||||
```
|
||||
```sql
|
||||
-- 分号换成 \G 显示完整 sql
|
||||
show processlist;
|
||||
show full processlist;
|
||||
SELECT command FROM information_schema.processlist;
|
||||
```
|
||||
|
||||
## MySQL 5.7 ssl 连接
|
||||
```
|
||||
--ssl-mode=REQUIRED
|
||||
```
|
||||
```
|
||||
--ssl-mode=REQUIRED
|
||||
```
|
||||
|
||||
## MariaDB 10.1 修改密码
|
||||
```sql
|
||||
UPDATE user SET password=password('newpassword') WHERE user='root';
|
||||
```
|
||||
```sql
|
||||
UPDATE user SET password=password('newpassword') WHERE user='root';
|
||||
```
|
||||
|
||||
## MySQL 5.7 编码
|
||||
- 查看
|
||||
@@ -272,9 +272,9 @@ categories: ["database"]
|
||||
```
|
||||
|
||||
## MySQL 5.7 升级数据库管理表结构
|
||||
```bash
|
||||
mysql_upgrade -u root -p
|
||||
```
|
||||
```bash
|
||||
mysql_upgrade -u root -p
|
||||
```
|
||||
|
||||
## MySQL 5.7 误删 root 后恢复 root 账户
|
||||
- 停止 mysql 服务
|
||||
@@ -385,10 +385,10 @@ categories: ["database"]
|
||||
- Using Flesort 依靠索引顺序达不到排序效果,需额外排序
|
||||
|
||||
## 统计 insert、delete、update 和 select 次数
|
||||
```sql
|
||||
show global status where Variable_name in
|
||||
('com_insert', 'com_delete', 'com_update', 'com_select');
|
||||
```
|
||||
```sql
|
||||
show global status where Variable_name in
|
||||
('com_insert', 'com_delete', 'com_update', 'com_select');
|
||||
```
|
||||
|
||||
## csv 文件
|
||||
- 导出
|
||||
|
Reference in New Issue
Block a user