Linux基礎(day54)

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_name\G;
  • 查看當前用戶 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;html

  2. 例子mysql

    • desc user; //查看user表裏的全部字段;須要進入相關的庫之後才能查看對應的表
  3. 查看建表語句 show create table tb_name\G;——>G 表示 豎排顯示linux

  4. 例子web

    • show create table user\G;
  5. 查看當前用戶 select user();sql

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 t1\G;
*************************** 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 t1\G;                                          
*************************** 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; 它會把經常使用的數據都列出來shell

  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密碼更改vim

  • 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設置成很大的值。若是設置得過高,可能會形成文件描述符不足,從而形成性能不穩定或者鏈接失敗。
      1. sort_buffer_size 查詢排序時所能使用的緩衝區大小,該參數對應的分配內存是每鏈接獨佔!若是有100個鏈接,那麼實際分配的總共排序緩衝區大小爲100 × 4 = 400MB。因此,對於內存在4GB左右的服務器推薦設置爲4-8M。
      1. read_buffer_size 讀查詢操做所能使用的緩衝區大小。和sort_buffer_size同樣,該參數對應的分配內存也是每鏈接獨享!
      1. join_buffer_size 聯合查詢操做所能使用的緩衝區大小,和sort_buffer_size同樣,該參數對應的分配內存也是每鏈接獨享!
      1. myisam_sort_buffer_size 這個緩衝區主要用於修復表過程當中排序索引使用的內存或者是創建索引時排序索引用到的內存大小,通常4G內存給64M便可。
      1. query_cache_size MySQL查詢操做緩衝區的大小,經過如下作法調整:SHOW STATUS LIKE ‘Qcache%’; 若是Qcache_lowmem_prunes該參數記錄有多少條查詢由於內存不足而被移除出查詢緩存。經過這個值,用戶能夠適當的調整緩存大小。若是該值很是大,則代表常常出現緩衝不夠的狀況,須要增長緩存大小;Qcache_free_memory:查詢緩存的內存大小,經過這個參數能夠很清晰的知道當前系統的查詢內存是否夠用,是多了,仍是不夠用,咱們能夠根據實際狀況作出調整。通常狀況下4G內存設置64M足夠了。
      1. thread_cache_size 表示能夠從新利用保存在緩存中線程的數,參考以下值:1G —> 8 2G —> 16 3G —> 32 >3G —> 64 除此以外,還有幾個比較關鍵的參數:
      1. thread_concurrency 這個值設置爲cpu核數的2倍便可
      1. wait_timeout 表示空閒的鏈接超時時間,默認是28800s,這個參數是和interactive_timeout一塊兒使用的,也就是說要想讓wait_timeout 生效,必須同時設置interactive_timeout,建議他們兩個都設置爲10
      1. max_connect_errors 是一個MySQL中與安全有關的計數器值,它負責阻止過多嘗試失敗的客戶端以防止暴力破解密碼的狀況。與性能並沒有太大關係。爲了不一些錯誤咱們通常都設置比較大,好比說10000
      1. max_connections 最大的鏈接數,根據業務請求量適當調整,設置500足夠
      1. max_user_connections 是指同一個帳號可以同時鏈接到mysql服務的最大鏈接數。設置爲0表示不限制。一般咱們設置爲100足夠

五. 同窗分享的親身mysql調優經歷

相關文章
相關標籤/搜索