Linux基础(day54)

时间:2022-04-27
本文章向大家介绍Linux基础(day54),主要内容包括13.1 设置更改root密码、设置更改root密码、13.2 连接mysql、13.3 mysql常用命令、mysql常用命令、创建库、扩展、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

13.1 设置更改root密码

设置更改root密码目录概要

  • /usr/local/mysql/bin/mysql -uroot
  • 更改环境变量PATH,增加mysql绝对路径
  • mysqladmin -uroot password '123456'
  • mysql -uroot -p123456
  • 密码重置
  • vi /etc/my.cnf//增加skip-grant
  • 重启mysql服务 /etc/init.d/mysqld restart
  • mysql -uroot
  • use mysql;
  • update user set password=password('aminglinux') where user='root';

设置更改root密码

  • root用户是mysql的超级管理员用户,和linux系统的root用户类似,不过和Linux的不一样
  • 默认mysql的 root 用户密码是空的,直接就可以连接上去,不需要输入密码,但是不安全,所以就需要设置一个密码
  • 为了方便使用mysql服务,将mysql目录加入到环境变量里
  1. 打开系统,查看mysql是否启动
[root@hanfeng ~]# ps aux |grep mysql
root      1659  0.0  0.1 115392  1712 ?        S    21:29   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/hanfeng.pid
mysql     2260  0.0 45.3 973548 458428 ?       Sl   21:29   0:02 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/hanfeng.err --pid-file=/data/mysql/hanfeng.pid
root      2386  0.0  0.0 112676   984 pts/0    R+   22:06   0:00 grep --color=auto mysql
[root@hanfeng ~]# 
  1. 若是没有启动mysql的话,将mysql启动起来
/etc/init.d/mysqld start
  1. 在启动mysql后,使用mysql -uroot命令,但是mysql命令会提示不存在,因为安装的mysql是在/usr/local/mysql/bin/mysql,而这个目录并不在环境变量PATH里面,所以它会报错
[root@hanfeng ~]# mysql -uroot
-bash: mysql: 未找到命令
[root@hanfeng ~]# ls /usr/local/mysql/bin/mysql
/usr/local/mysql/bin/mysql
[root@hanfeng ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@hanfeng ~]# 
  1. 若想要这个命令直接运行,需要把PATH做一个更改
[root@hanfeng ~]# export PATH=$PATH:/usr/local/mysql/bin/
[root@hanfeng ~]# 
  1. 这时再来使用mysql -uroot命令就会发现可以使用了
  • 退出mysql输入 quit 即可
[root@hanfeng ~]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 1
Server version: 5.6.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

mysql> quit
Bye
[root@hanfeng ~]# 
  1. 若想要变量永久生效,还需要将export PATH=$PATH:/usr/local/mysql/bin/ 放入到 /etc/profile
[root@hanfeng ~]# vim /etc/profile

将内容放到配置文件的最后面,使命令永久生效
export PATH=$PATH:/usr/local/mysql/bin/

保存退出
  1. 假设若是没有运行 export PATH=$PATH:/usr/local/mysql/bin/ 命令,那也不能运行mysql,因为变量还没有生效,想要这个变量生效,在配置文件中加入命令后,还需要执行source /etc/profile 命令,重新加载
[root@hanfeng ~]# source /etc/profile
[root@hanfeng ~]# 
  1. 一般是使用mysql -uroot -p命令
  • -p,表示指定密码
  1. 密码为空的时候,直接回车就可进入到mysql,并可以在其中操作一些mysql的一些行为
[root@hanfeng ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 2
Server version: 5.6.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

mysql> quit
Bye
[root@hanfeng ~]# 
  1. 退出mysql,输入 quit 即可
  2. 设置mysql密码,命令为mysqladmin -uroot passwd 'hanfeng.1' 在 ' ' 为密码
[root@hanfeng ~]# mysqladmin -uroot password 'hanfeng.1'
Warning: Using a password on the command line interface can be insecure.
[root@hanfeng ~]# 
  1. 在设置密码的时候,会看到有输出信息,但这不是报错信息,这是告诉你 你现在密码在当前命令行显示出来了,这样不太安全
  2. 这时在想直接登录mysql,就会提示需要输入密码了
[root@hanfeng ~]# mysql -uroot
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@hanfeng ~]# 
  1. 在使用-p,并输入密码就可以正常的进入到mysql命令行了
[root@hanfeng ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 7
Server version: 5.6.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

mysql> quit
Bye
[root@hanfeng ~]# 

知道mysql的root密码,去更改密码

  1. 若是这时知道mysql密码,去修改mysql密码,看到输出的提示信息不用去理会
  • 格式
    • mysqladmin -uroot -p'hanfeng.1' password 'hanfeng'
[root@hanfeng ~]# mysqladmin -uroot -p'hanfeng.1' password 'hanfeng'
Warning: Using a password on the command line interface can be insecure.
[root@hanfeng ~]# 
  1. 指定新密码去登录,当然也可以不明文指定密码,知道-p回车,输入密码登录也行
[root@hanfeng ~]# mysql -uroot -p 'hanfeng'
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root@hanfeng ~]# mysql -uroot -p'hanfeng'
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 13
Server version: 5.6.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

mysql> quit
Bye
[root@hanfeng ~]# 
  1. 在明文指定密码的时候,密码可以加单引号,也可以不加单引号,建议加上单引号,防止密码有特殊符号的存在——>(若是不加单引号,而密码中又有特殊符号,就有可能会不识别)

不知道mysql的root密码,去更改密码

  1. 在不知道mysql的root用户密码的时候,先去更改 /etc/my.cnf 下配置文件中加入skip-grant
  • skip-grant ,表示忽略授权,也就是说操作mysql的时候不需要用户名和密码了,能直接登录
[root@hanfeng ~]# vim /etc/my.cnf

在[mysqld]下面
加入一行
skip-grant

保存退出
  1. 在更改配置文件后,重启mysql服务
[root@hanfeng ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL.. SUCCESS! 
[root@hanfeng ~]# 
  1. 这时候在输入mysql -uroot ,会发现直接进入mysql,而不需要密码了
[root@hanfeng ~]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 1
Server version: 5.6.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

mysql> 
  1. 在登录进mysql后,还需要更改一个表,因为用户名和密码是存在于一个mysql库里面的,使用 use mysql; 切换库,在切换到mysql库里面,然后去更改一个存用户名密码的user表
  • use mysql; 切换库
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> 
  1. 查看user表,输入select * from user; 命令,会看到输出乱七八糟的乱码,里面存放的就是用户名和密码,还有权限和授权等信息
mysql> select * from user;
+-----------+------+-------------------------------------------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+------------+--------------+------------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+-----------------------+-----------------------+------------------+
| Host      | User | Password                                  | Select_priv | Insert_priv | Update_priv | Delete_priv | Create_priv | Drop_priv | Reload_priv | Shutdown_priv | Process_priv | File_priv | Grant_priv | References_priv | Index_priv | Alter_priv | 

等等等,都是乱码
  1. 查看password表,会看到密码都是加密的
mysql> select password from user where user=’root’;
ERROR 1054 (42S22): Unknown column '’root’' in 'where clause'
mysql> select password from user where user='root';
+-------------------------------------------+
| password                                  |
+-------------------------------------------+
| *406D1994F8340A1442C5090388944CCB985BA3DE |
|                                           |
|                                           |
|                                           |
+-------------------------------------------+
4 rows in set (0.00 sec)

mysql> 
  1. 更改user表,使用update user set password=password('aminglinux') where user='root'; 命令
  • update user set password=password('aminglinux') where user='root';
    • 密码字段 函数 //用于加密密码 高亮部分:为条件语句
mysql> update user set password=password('aminglinux') where user='root';
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4  Changed: 4  Warnings: 0

mysql> 
  1. 提示说4行修改完毕,即使有些行是空的
  2. 这样密码就更改成功了,输入quit退出数据库即可
mysql> quit
Bye
  1. 再去 /etc/my.cnf 配置文件中删除免授权配置,即删除skip-grant——>若是不删除,那么之后所有的用户都不需要输入密码,就可以登录进去,这样安全性太低
[root@hanfeng ~]# vim /etc/my.cnf

在[mysqld]下面删除刚添加的
删除skip-grant

保存退出
  1. 重启mysql服务
[root@hanfeng ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL.. SUCCESS! 
[root@hanfeng ~]# 
  1. 重启完之后,再用新的密码测试下,会看到新的密码可以登录
[root@hanfeng ~]# mysql -uroot -paminglinux
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 1
Server version: 5.6.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

mysql> quit
Bye
[root@hanfeng ~]# 
  1. 这样就是成功更改mysql密码

13.2 连接mysql

连接mysql

  • 本地连接——>即使没有指定,但默认使用sock连接,使用/tmp/mysql.sock连接
    • mysql -uroot -p123456 //输入用户名和密码连接本机
  • 使用ip端口连接远程机器
    • mysql -uroot -p123456 -h[远程mysql主机IP] -P[端口]
[root@hf-01 ~]# mysql -uroot -phanfeng -h127.0.0.1 -P3306
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 8
Server version: 5.6.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

mysql> QUIT
Bye
[root@hf-01 ~]# 
  • 使用sock方式连接(只适合在本机使用)如为指定IP就用sock访问
    • mysql -uroot -p123456 -S/tmp/mysql.sock
[root@hf-01 ~]# mysql -uroot -phanfeng -S/tmp/mysql.sock
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 9
Server version: 5.6.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

mysql> quit
Bye
[root@hf-01 ~]# 
  • 连接mysql之后执行命令(常用于shell脚本)
    • mysql -uroot -p123456 -e “show databases”
      • show databases //列出所有数据库
[root@hf-01 ~]# mysql -uroot -phanfeng -e 'show databases'
Warning: Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
[root@hf-01 ~]# 

13.3 mysql常用命令

mysql常用命令目录概要

  • 查询库 show databases;
  • 切换库 use mysql;
  • 查看库里的表 show tables;
  • 查看表里的字段 desc tb_name;
  • 查看建表语句 show create table tb_nameG;
  • 查看当前用户 select user();
  • 查看当前使用的数据库 select database();
  • 创建库 create database db1;
  • 创建表 use db1; create table t1(id int(4), name char(40));
  • 查看当前数据库版本 select version();
  • 查看数据库状态 show status;
  • 查看各参数 show variables; show variables like 'max_connect%';
  • 修改参数 set global max_connect_errors=1000;
  • 查看队列 show processlist; show full processlist;

mysql常用命令

  • mysql内部命令和linux系统命令不通用
  • 库是由表组成,表是由字段组成
  • 在mysql 中使用的命令需要使用分号 ; 结尾,否则会出问题
  1. 登录到mysql
[root@hf-01 ~]# mysql -uroot -p'hanfeng'
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 11
Server version: 5.6.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

mysql> 
  1. 查看数据库 show databases;
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

mysql> 
  1. 切换库 use mysql; ——>切换到哪一个库下面
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> 
  1. 列出所有的表 show tables;
mysql> show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
| func                      |
| general_log               |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| innodb_index_stats        |
| innodb_table_stats        |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| servers                   |
| slave_master_info         |
| slave_relay_log_info      |
| slave_worker_info         |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
28 rows in set (0.00 sec)

mysql> 
  1. 查看表里的字段 desc tb_name;
  2. 例子
    • desc user; //查看user表里的所有字段;需要进入相关的库以后才能查看对应的表
  3. 查看建表语句 show create table tb_nameG;——>G 表示 竖排显示
  4. 例子
    • show create table userG;
  5. 查看当前用户 select user();
mysql> select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)

mysql> 
  • 这里的localhost 表示主机名,会反解析你的IP所属主机名
  1. 在mysql里面使用 方向键 可以查看之前使用的命令,和shell的命令类似,并且也有记录命令历史的功能,默认的缓存文件在 / 下,名字为“.mysql_history”
  • 支持ctrl+l 清屏
  1. 查看当前使用的数据库 select database();
mysql> select database();
+------------+
| database() |
+------------+
| mysql      |
+------------+
1 row in set (0.00 sec)

mysql> 

创建库

  1. 创建库 create database db1;
mysql> create database db1;
Query OK, 1 row affected (0.00 sec)

mysql> 
  1. 查看db1库
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db1                |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

mysql> 
  1. 切换到db1库下面去
mysql> use db1;
Database changed
mysql> 
  1. 创建表 create table t1(id int(4), name char(40));——>定义 id 和 name ,并用反引好括起来
mysql> create table t1(`id` int(4), `name` char(40));
Query OK, 0 rows affected (0.03 sec)

mysql> 
  1. 查看创建的表t1,默认使用的是InnoDB 引擎
mysql> show create table t1G;
*************************** 1. row ***************************
       Table: t1
Create Table: CREATE TABLE `t1` (
  `id` int(4) DEFAULT NULL,
  `name` char(40) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

ERROR: 
No query specified

mysql> 
  1. 在数据库里面取消命令,即在命令的最前面加一个 # 号就不会生效了
  2. 删除表 drop table t1;
mysql> drop table t1;
Query OK, 0 rows affected (0.02 sec)

mysql> 
  1. 创建t1表
mysql> create table t1(`id` int(4), `name` char(40)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.01 sec)

mysql> 
  1. 查看表,会看到变成了utf8
mysql> show create table t1G;                                          
*************************** 1. row ***************************
       Table: t1
Create Table: CREATE TABLE `t1` (
  `id` int(4) DEFAULT NULL,
  `name` char(40) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.01 sec)

ERROR: 
No query specified

mysql> 
  1. 查看当前数据库版本 select version();
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.6.35    |
+-----------+
1 row in set (0.00 sec)

mysql> 
  1. 查看数据库状态 show status; 它会把常用的数据都列出来
  2. 查看各参数
  • show variables;
  • show variables like 'max_connect%'; //mysql下 % 为通配符
mysql> show variables like 'max_connect%'; 
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| max_connect_errors | 100   |
| max_connections    | 151   |
+--------------------+-------+
2 rows in set (0.00 sec)

mysql>
  1. 修改参数 set global max_connect_errors=1000; ——>仅在内存中生效
mysql> set global max_connect_errors=1000;
Query OK, 0 rows affected (0.01 sec)

mysql> show variables like 'max_connect%';
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| max_connect_errors | 1000  |
| max_connections    | 151   |
+--------------------+-------+
2 rows in set (0.00 sec)

mysql> 
  • 若想重启依然生效,那需要修改配置文件/etc/my.cnf
    • 在[mysqld]里面定义
  1. 查看队列
  • show processlist; //查看库的状况,比如,那些用户在连,做了些什么操作,是否锁表
mysql> show processlist; 
+----+------+-----------+------+---------+------+-------+------------------+
| Id | User | Host      | db   | Command | Time | State | Info             |
+----+------+-----------+------+---------+------+-------+------------------+
| 14 | root | localhost | db1  | Query   |    0 | init  | show processlist |
+----+------+-----------+------+---------+------+-------+------------------+
1 row in set (0.00 sec)

mysql> 
  • show full processlist; //查看到的对列,最后一个会非常完成的显示出来
mysql> show full processlist;
+----+------+-----------+------+---------+------+-------+-----------------------+
| Id | User | Host      | db   | Command | Time | State | Info                  |
+----+------+-----------+------+---------+------+-------+-----------------------+
| 14 | root | localhost | db1  | Query   |    0 | init  | show full processlist |
+----+------+-----------+------+---------+------+-------+-----------------------+
1 row in set (0.00 sec)

mysql> 

扩展

一. mysql5.7 root密码更改

  • mysql5.7root有默认密码,必须重设密码后,才能进行mysql的操作,以下是设置root密码的步骤:
  1. 查看默认密码
[root@localhost src]# cat /root/.mysql_secret

# The random password set for the root userat Fri Jan 10 20:00:34 2014 (local time): aJqZsA2m
  • 这里的aJqZsA2m就是生成的root随机密码啦
  1. 登录mysql
[root@localhost src]# mysql -u root -p

Enter password:
  • 输入上面的密码aJqZsA2m登录,如果你没有把mysql的路径加到path里,那就用绝对路径,mysql -u root -p还可以写成mysql -uroot -paJqZsA2m
  1. 更改密码
mysql> SET PASSWORD  FOR 'root'@localhost = PASSWORD('123456');
Query OK, 0 rows affected (0.17 sec)

二. myisam 和innodb引擎对比

三. mysql 配置详解

四. mysql调优

  • MySQL调优可以从几个方面来做:
  1. 架构层:
  • 做从库,实现读写分离;
  1. 系统层次:
  • 增加内存;
  • 给磁盘做raid0或者raid5以增加磁盘的读写速度;
  • 可以重新挂载磁盘,并加上noatime参数,这样可以减少磁盘的i/o;
  1. MySQL本身调优:
  • (1) 如果未配置主从同步,可以把bin-log功能关闭,减少磁盘i/o
  • (2) 在my.cnf中加上skip-name-resolve,这样可以避免由于解析主机名延迟造成mysql执行慢
  • (3) 调整几个关键的buffer和cache。调整的依据,主要根据数据库的状态来调试。如何调优可以参考5.
  1. 应用层次:
  • 查看慢查询日志,根据慢查询日志优化程序中的SQL语句,比如增加索引
  1. 调整几个关键的buffer和cache
    • 1). key_buffer_size 首先可以根据系统的内存大小设定它,大概的一个参考值:1G以下内存设定128M;2G/256M; 4G/384M;8G/1024M;16G/2048M.这个值可以通过检查状态值Key_read_requests和 Key_reads,可以知道key_buffer_size设置是否合理。比例key_reads / key_read_requests应该尽可能的低,至少是1:100,1:1000更好(上述状态值可以使用SHOW STATUS LIKE ‘key_read%’获得)。注意:该参数值设置的过大反而会是服务器整体效率降低!
    • 2). table_open_cache 打开一个表的时候,会临时把表里面的数据放到这部分内存中,一般设置成1024就够了,它的大小我们可以通过这样的方法来衡量: 如果你发现 open_tables等于table_cache,并且opened_tables在不断增长,那么你就需要增加table_cache的值了(上述状态值可以使用SHOW STATUS LIKE ‘Open%tables’获得)。注意,不能盲目地把table_cache设置成很大的值。如果设置得太高,可能会造成文件描述符不足,从而造成性能不稳定或者连接失败。
    • sort_buffer_size 查询排序时所能使用的缓冲区大小,该参数对应的分配内存是每连接独占!如果有100个连接,那么实际分配的总共排序缓冲区大小为100 × 4 = 400MB。所以,对于内存在4GB左右的服务器推荐设置为4-8M。
    • read_buffer_size 读查询操作所能使用的缓冲区大小。和sort_buffer_size一样,该参数对应的分配内存也是每连接独享!
    • join_buffer_size 联合查询操作所能使用的缓冲区大小,和sort_buffer_size一样,该参数对应的分配内存也是每连接独享!
    • myisam_sort_buffer_size 这个缓冲区主要用于修复表过程中排序索引使用的内存或者是建立索引时排序索引用到的内存大小,一般4G内存给64M即可。
    • query_cache_size MySQL查询操作缓冲区的大小,通过以下做法调整:SHOW STATUS LIKE ‘Qcache%’; 如果Qcache_lowmem_prunes该参数记录有多少条查询因为内存不足而被移除出查询缓存。通过这个值,用户可以适当的调整缓存大小。如果该值非常大,则表明经常出现缓冲不够的情况,需要增加缓存大小;Qcache_free_memory:查询缓存的内存大小,通过这个参数可以很清晰的知道当前系统的查询内存是否够用,是多了,还是不够用,我们可以根据实际情况做出调整。一般情况下4G内存设置64M足够了。
    • thread_cache_size 表示可以重新利用保存在缓存中线程的数,参考如下值:1G —> 8 2G —> 16 3G —> 32 >3G —> 64 除此之外,还有几个比较关键的参数:
    • thread_concurrency 这个值设置为cpu核数的2倍即可
    • wait_timeout 表示空闲的连接超时时间,默认是28800s,这个参数是和interactive_timeout一起使用的,也就是说要想让wait_timeout 生效,必须同时设置interactive_timeout,建议他们两个都设置为10
    • max_connect_errors 是一个MySQL中与安全有关的计数器值,它负责阻止过多尝试失败的客户端以防止暴力破解密码的情况。与性能并无太大关系。为了避免一些错误我们一般都设置比较大,比如说10000
    • max_connections 最大的连接数,根据业务请求量适当调整,设置500足够
    • max_user_connections 是指同一个账号能够同时连接到mysql服务的最大连接数。设置为0表示不限制。通常我们设置为100足够

五. 同学分享的亲身mysql调优经历