DB-MySQL-Summary
常用命令
1 | #连接mysql |
mysql配置my.cnf,添加挂在卷 - /dockerdata/manager/mysqldata/config:/etc/mysql/conf.d
然后在挂在卷创建配置文件,添加配置my.cnf
文件名字随便
1 | [mysql] |
备份与恢复
1 | #备份数据库 manage geoserver两个 |
参考 MySql数据库备份与恢复——使用mysqldump 导入与导出方法总结
主从库
- 修改主从配置库的配置文件
主库配置:
1 | [mysqld] |
从数据库配置:
1 | [mysqld] |
启动服务
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38mysql-master:
restart: always
image: mysql:5.7.18
environment:
MYSQL_ROOT_PASSWORD: admin
volumes:
- /data/v-yinfu/mysql/master/data:/var/lib/mysql
- /data/v-yinfu/mysql/master/config:/etc/mysql/conf.d
ports:
- target: 3306
published: 14036
protocol: tcp
mode: host
deploy:
replicas: 1
restart_policy:
condition: on-failure
placement:
constraints: [node.hostname == VM_16_8_centos]
mysql-slave:
restart: always
image: mysql:5.7.18
environment:
MYSQL_ROOT_PASSWORD: admin
volumes:
- /data/v-yinfu/mysql/slave/data:/var/lib/mysql
- /data/v-yinfu/mysql/slave/config:/etc/mysql/conf.d
ports:
- target: 3306
published: 14037
protocol: tcp
mode: host
deploy:
replicas: 1
restart_policy:
condition: on-failure
placement:
constraints: [node.hostname == VM_16_13_centos]授权
进入主库容器执行
mysql -uroot -p
1
2
3
4
5#ip为从库ip,设置为只有从库可以访问
GRANT REPLICATION SLAVE ON *.* TO 'app_sync'@'172.16.16.13' IDENTIFIED BY 'password';
GRANT ALL ON *.* TO 'app_root'@'%' IDENTIFIED BY 'password';
GRANT SELECT ON *.* TO 'app_read'@'%' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;然后执行
show master status;
1
2
3
4
5
6+------------------+----------+--------------+--------------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+--------------------------+-------------------+
| mysql-bin.000003 | 1164 | shenqics | information_schema,mysql | |
+------------------+----------+--------------+--------------------------+-------------------+
1 row in set (0.00 sec)进入从库容器执行
mysql -uroot -p
1
2
3
4
5
6
7stop slave;
change master to master_host='172.16.16.8',master_port=14036,master_user='app_sync',master_password='password',master_log_file='mysql-bin.000003', master_log_pos=1164;
start slave;
show slave status;
#授权
GRANT SELECT ON *.* TO 'app_read'@'%' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;执行
show slave status\G;
检查是否这两个为yes1
2Slave_IO_Running: Connecting
Slave_SQL_Running: Yes重新注册
1
2
3
4
5
6stop slave;
change master to master_host='172.16.16.8',master_port=14036,master_user='app_sync',master_password='admin',master_log_file='mysql-bin.000005', master_log_pos=361;
start slave;
#清除log,执行start slave报错时
reset slave;
start slave;再次执行第6步检查
mysql创建用户命令详解
创建用户
GRANT 权限 ON 数据库.表名 TO '用户'@'主机' IDENTIFIED BY '密码'
- 权限:all,select,等
- 主机:指定ip地址访问、localhost或127.0.0.1(本地访问)、%(任意主机均可访问)
- 密码:为空时则不需要密码
eg:
1 | GRANT REPLICATION SLAVE ON *.* TO 'app_sync'@'172.16.16.13' IDENTIFIED BY 'admin'; |
删除用户drop user test@'172.16.16.13';
HikariCP连接池优化
HikariCP作为springboot连接池,在性能压测时,连接时间成指数递增。
1 | #该属性用于控制连接在池中的最大生存时间,超过该时间强制逐出,当前正在使用的连接不会强制逐出。 |
数据库配置
1 | > show variables like '%timeout%'; |
常见错误
Too many connections
症状,不断重启运行springboot并访问,出现如下错误1
2
3
4
5
62018-04-05 21:48:56.824 ERROR 6838 --- [ main] com.xhzg.xhzg.XhzgApplicationTests : nested exception is org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection, message from server: "Too many connections"
### The error may exist in com/xhzg/xhzg/mapper/UserMapper.java (best guess)
### The error may involve com.xhzg.xhzg.mapper.UserMapper.loadUserByUsername
### The error occurred while executing a query
### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection, message from server: "Too many connections"解决:快速解决重启mysql释放
Threads_connected
连接数,或者等待一会儿,也会慢慢释放连接数,另一种更改max_connections
最大连接数启动mysql添加参数--ulimit nofile=65536:65536
参考:
Docker容器中MySQL最大连接数被限制为214的解决方案
[Increasing mysql max_connections to 1024 in a docker container]
错误
1
[Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'information_schema.PROFILING.SEQ' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
解决:
删除配置文件
sql_mode=ONLY_FULL_GROUP_BY
这个属性值1
2mysql>SELECT @@sql_mode;
mysql>SELECT @@GLOBAL.sql_mode;navicat客户端,连接mysql 8.0以上报错,提示授权啥的错误
设置主从模式时,使用用户
'app_sync'@'172.16.16.13'
连接时提示,以及一直Slave_IO_Running: Connecting
1
ERROR 1045 (28000): Access denied for user 'app_sync'@'10.255.0.2' (using password: YES)
解决:部署时设置host模式
原因:非host模式连接时,访问客户端ip是内部ip不是host的ip
错误
1
java.sql.SQLException: Incorrect string value: '\xF0\x9F\x90\xB6' for column 'UserNickname' at row 1
解决:
在mysql配置文件添加后,重启
1
2
3
4
5
6
7
8
9
10[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
[mysqld]
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci修改数据表的编码
ALTER TABLE TABLE_NAME CONVERT TO CHARACTER SET utf8mb4;
修改数据库连接
jdbc:mysql://localhost:3306/"+DATABASENAME+"?useunicode=true&characterEncoding=utf8
方式一:去掉参数
&characterEncoding=utf8
和useunicode=true
方式二(建议):添加
autoReconnect=true