1.引擎:
1.1查看引擎:
mysql> show engines;
mysql> SHOW VARIABLES LIKE '%storage_engine%';
mysql> show create table t1; ---查看建表信息
1.2 临时指定引擎:
mysql> create table innodb1(id int)engine=innodb;
1.3修改默认引擎:
/etc/my.cnf
[mysqld]
default-storage-engine=INNODB ----引擎
修改已经存在的表的引擎:
mysql> alter table t2 engine=myisam;
mysql常用命令:
mysql> show warnings 查看最近一个sql语句产生的错误警告,看其他的需要看.err日志
mysql> show processlist 显示系统中正在运行的所有进程。
mysql> show errors 查看最近一个sql语句产生的错误信息
2.字符集设置
2.1 临时设置字符集
mysql> create database db1 CHARACTER SET = utf8;mysql> create table t1(id int(10)) CHARACTER SET = utf8;
查看当前使用的字符集
mysql> SHOW VARIABLES LIKE 'character_set_%';
2.2永久修改字符集设置:(5.7/ 5.5版本)
[mysqld]
character-set-server=utf8mb4 #设置MySQL服务器的默认字符集为utf8mb4
collation-server=utf8mb4_unicode_ci #设置服务器的默认排序规则
[client]
default-character-set = utf8mb4 #所有客户端工具的默认字符集设置
[mysql]
default-character-set=utf8mb4 #专门针对mysql命令行客户端的字符集设置
3.慢查:
查看是否设置成功:mysql> show variables like '%query%';
当连接数的数值过小会经常出现ERROR 1040: Too many connections错误。
这是是查询数据库当前设置的最大连接数
mysql> show variables like '%max_connections%';
强制限制mysql资源设置:
# vim /etc/my.cnf
max_connections = 1024 并发连接数,根据实际情况设置连接数。
connect_timeout= 5 单位秒 ----超时时间,默认30秒
wait_timeout=10 终止空闲时间超过10秒的链接,避免长连接
max_connect_errors=10 10次连接失败就锁定,使用flush hosts 解锁,或mysqladmin flush-hosts -uroot -p'密码'解锁
优化方案
1.开启慢查询 ---分析sql语句,找到影响效率的 SQL
2.创建索引 --提升数据的检索效率
4.innodb引擎:
innodb-buffer-pool-size //缓存 InnoDB 数据和索引的内存缓冲区的大小
innodb-buffer-pool-size=# ----值
这个值设得越高,访问表中数据需要得磁盘 I/O 越少。在一个专用的数据库服务器上,你可以设置这个参数达机器物理内存大小的 80%。
# vim /etc/my.cnf
innodb-buffer-pool-size=2G
5.查看mysql基本状态
[root@mysql-master ~]# mysqladmin status -uroot -p'Liuliu@123!'
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Uptime: 23963 Threads: 12 Questions: 1365 Slow queries: 0 Opens: 117 Flush tables: 1 Open tables: 110 Queries per second avg: 0.056uptime 启动时间
Threads 线程数量,(打开的会话)
Questions 所有查询的数量
Slow queries 慢查询是否打开
Opens 服务器已经打开的数据库表的数量
flush tables 执行清空表的缓存次数
Open_tables 通过命令打开的表的数量
Queries per second avg:select语句平均查询时间log_queries_not_using_indexes = 1 # 记录未使用索引的查询
sync_binlog=1 #控制 Binlog 刷盘频率数据安全:设为 1(每次提交同步)性能优先:设为 0(由系统决定
mysqldumpslow -s t /var/log/mysql/slow.log # 慢查询日志分析工具
6.mysql监控项
监控项 含义
磁盘空间 实例的空间占用历史趋势,单位GB
连接数 当前总连接数
CPU使用率 mysql实例CPU使用率(占操作系统总数)
数据库的连接数
mysql的主从同步状态
主从延迟时间
进程、端口等