MySQL密碼相關,連線方式,常用命令
[toc]
# MySQL密碼相關,連線方式,常用命令
擴充套件
mysql5.7 root密碼更改(較5.6版本變化大) http://www.apelearn.com/bbs/thread-7289-1-1.html
myisam 和innodb引擎對比 http://www.pureweber.com/article/myisam-vs-innodb/
mysql 配置詳解: http://blog.linuxeye.com/379.html
mysql調優: http://www.aminglinux.com/bbs/thread-5758-1-1.html
同學分享的親身mysql調優經歷: http://www.apelearn.com/bbs/thread-11281-1-1.html
## 一、MySQL密碼修改
### 1. 判斷mysql是否開啟
```
[root@xavi ~]# ps aux |grep mysql
root 2544 0.0 0.0 112680 972 pts/0 S+ 10:02 0:00 grep --color=auto mysql
[root@xavi ~]# /etc/init.d/mysqld start
Starting MySQL.... SUCCESS!
[root
ps aux |grep mysql
root 2738 0.0 0.0 113268 1584 pts/0 S 10:12 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/xavi.pid
mysql 2886 1.9 24.1 975152 451864 pts/0 Sl 10:12 0:03 /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/xavi.err --pid-file=/data/mysql/xavi.pid --socket=/tmp/mysql.sock
root 2943 0.0 0.0 112680 976 pts/0 S+ 10:15 0:00 grep --color=auto mysql
```
#### 1.1 啟動sql命令,但是無法啟動,原因是mysql的命令路徑並未在環境變數$PATH內定義過
```
[root@xavi ~]# mysql -uroot
bash: mysql: 未找到命令...
[root@xavi ~]# ls /usr/local/mysql/bin/mysql
/usr/local/mysql/bin/mysql
[[email protected] ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[[email protected] ~]# export PATH=$PATH:/usr/local/mysql/bin/
[[email protected] ~]# 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> Ctrl-C -- exit!
Aborted
```
#### 1.2 如果需要將mysql路徑永久新增到環境變數中,需要進入/etc/profile進行編輯,同時source /etc/profile載入配置
```
[[email protected] ~]# vim /etc/profile
[[email protected] ~]# source /etc/profile
```
![mark](http://p0weeraap.bkt.clouddn.com/xavi2017/180322/AG74aAjm2h.png?imageslim)
### 2.給MySQL建立密碼:mysqladmin -uroot password 'xavilinux.1',然後登入
```
[[email protected] ~]# mysqladmin -uroot password 'xavilinux.1'
Warning: Using a password on the command line interface can be insecure.
[[email protected] ~]# mysql -uroot
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[[email protected] ~]# 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>
```
#### 2.1修改原密碼
```
[[email protected] ~]# mysqladmin -uroot -p'xavilinux.1' password 'xavilinux'
Warning: Using a password on the command line interface can be insecure.
```
#### 2.2登入,記得密碼的單引號,和-p後面沒有空格的
```
[[email protected] ~]# mysql -uroot -p'xavilinux'
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 10
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.
```
#### 2.3 在忘記密碼的情況下如何進入
##### a.先進入vim /etc/my.cnf,增加skip-grant,忽略授權
![mark](http://picture.xavilinux.work/xavi2017/180322/fmK5a10ble.png?imageslim)
##### b.儲存後重啟MySQL服務,進入
```
[[email protected] ~]# /etc/init.d/mysqld restart
Shutting down MySQL.... SUCCESS!
Starting MySQL. SUCCESS!
[[email protected] ~]# 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>
```
##### c.查詢密碼所在的資料表
```
use mysql ;
```
![mark](http://picture.xavilinux.work/xavi2017/180322/FcbijBL2jg.png?imageslim)
```
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> select password from user ;
+-------------------------------------------+
| password |
+-------------------------------------------+
| *254DE8C0E825F909A01A520D296E6A883FFDE4F8 |
| |
| |
| |
| |
| |
+-------------------------------------------+
6 rows in set (0.00 sec)
```
#### d.修改密碼:update user set password=password('xavilinux') where user='root' ;
```
mysql> update user set password=password('xavilinux') where user='root' ;
Query OK, 3 rows affected (0.00 sec)
Rows matched: 4 Changed: 3 Warnings: 0
```
#### e.面密碼修改成功後,修改下/etc/my.cnf檔案中,前面新增的skip-grant刪除,恢復密碼驗證,確保安全
![mark](http://picture.xavilinux.work/xavi2017/180322/E8Kf3gA7fB.png?imageslim)
#### f.重啟服務,輸入密碼登入sql
```
[[email protected] ~]# vi /etc/my.cnf
[[email protected] ~]# /etc/init.d/mysqld restart
ERROR! MySQL server PID file could not be found!
Starting MySQL... SUCCESS!
[[email protected] ~]# mysql -uroot -pxavilinux
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>
```
## 二、連線資料庫的方式
### 1.mysql -uroot -pxavilinux
```
[[email protected] ~]# mysql -uroot -pxavilinux
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.
```
### 2.mysql -uroot -pxavilinux -h127.0.0.1 -P3306
```
[[email protected] ~]# mysql -uroot -pxavilinux -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 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.
```
### 3.用socke方式連線mysql -uroot -pxavilinux -S/tmp/mysql.sock,只適合本機登入
![mark](http://picture.xavilinux.work/xavi2017/180322/Ce0e36Hhja.png?imageslim)
```
[[email protected] ~]# mysql -uroot -pxavilinux -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 3
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.
```
### mysql -uroot -pxavilinux -e "show databases"//shell腳本里面常用
```
[[email protected] ~]# mysql -uroot -pxavilinux -e "show databases"
Warning: Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
```
## 三、MySQL基本操作的常用命令
記住MySQL的操作必須進入mysql後執行
```
[[email protected] ~]# mysql -uroot -pxavilinux
```
### 3.1 查詢當前庫
mysql> show databases; //命令結尾加上分號
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
### 3.2 切換到某個資料庫
```
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
```
### 3.3 查看錶
```
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)
```
### 3.4 檢視某個表的全部欄位
```
mysql> desc db;
+-----------------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------+---------------+------+-----+---------+-------+
| Host | char(60) | NO | PRI | | |
| Db | char(64) | NO | PRI | | |
| User | char(16) | NO | PRI | | |
| Select_priv | enum('N','Y') | NO | | N | |
| Insert_priv | enum('N','Y') | NO | | N | |
| Update_priv | enum('N','Y') | NO | | N | |
| Delete_priv | enum('N','Y') | NO | | N | |
| Create_priv | enum('N','Y') | NO | | N | |
| Drop_priv | enum('N','Y') | NO | | N | |
| Grant_priv | enum('N','Y') | NO | | N | |
| References_priv | enum('N','Y') | NO | | N | |
| Index_priv | enum('N','Y') | NO | | N | |
| Alter_priv | enum('N','Y') | NO | | N | |
| Create_tmp_table_priv | enum('N','Y') | NO | | N | |
| Lock_tables_priv | enum('N','Y') | NO | | N | |
| Create_view_priv | enum('N','Y') | NO | | N | |
| Show_view_priv | enum('N','Y') | NO | | N | |
| Create_routine_priv | enum('N','Y') | NO | | N | |
| Alter_routine_priv | enum('N','Y') | NO | | N | |
| Execute_priv | enum('N','Y') | NO | | N | |
| Event_priv | enum('N','Y') | NO | | N | |
| Trigger_priv | enum('N','Y') | NO | | N | |
+-----------------------+---------------+------+-----+---------+-------+
22 rows in set (0.01 sec)
```
Filed:欄位名,type:欄位格式
### 3.5 檢視建表語句並列出,命令後加\G,目的是讓列出來的結果豎排顯示,這樣看起來更清晰;
```
mysql> show create table user\G;
*************************** 1. row ***************************
Table: user
Create Table: CREATE TABLE `user` (
`Host` char(60) COLLATE utf8_bin NOT NULL DEFAULT '',
`User` char(16) COLLATE utf8_bin NOT NULL DEFAULT '',
`Password` char(41) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL DEFAULT '',
`Select_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Insert_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Update_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Delete_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Create_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Drop_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Reload_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Shutdown_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Process_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`File_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Grant_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`References_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Index_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Alter_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Show_db_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Super_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Create_tmp_table_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Lock_tables_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Execute_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Repl_slave_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Repl_client_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Create_view_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Show_view_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Create_routine_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Alter_routine_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Create_user_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Event_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Trigger_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Create_tablespace_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`ssl_type` enum('','ANY','X509','SPECIFIED') CHARACTER SET utf8 NOT NULL DEFAULT '',
`ssl_cipher` blob NOT NULL,
`x509_issuer` blob NOT NULL,
`x509_subject` blob NOT NULL,
`max_questions` int(11) unsigned NOT NULL DEFAULT '0',
`max_updates` int(11) unsigned NOT NULL DEFAULT '0',
`max_connections` int(11) unsigned NOT NULL DEFAULT '0',
`max_user_connections` int(11) unsigned NOT NULL DEFAULT '0',
`plugin` char(64) COLLATE utf8_bin DEFAULT 'mysql_native_password',
`authentication_string` text COLLATE utf8_bin,
`password_expired` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
PRIMARY KEY (`Host`,`User`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Users and global privileges'
1 row in set (0.01 sec)
```
*不加\G
![mark](http://picture.xavilinux.work/xavi2017/180322/D5K52JHf6k.png?imageslim)
### 3.6 檢視當前是哪個使用者
```
mysql> select user();
+----------------+
| user() |
+----------------+
| [email protected] |
+----------------+
1 row in set (0.00 sec)
```
### 3.7 檢視mysql歷史指令記錄情況
ls -la
![mark](http://picture.xavilinux.work/xavi2017/180322/74F2l86hjK.png?imageslim)
```
[[email protected]i ~]# less .mysql_history
_HiStOrY_V2_
user\040mysql\040;
use\040mysql\040;
select\040*\040from\040user
select\040*\040from\040user\040;
select\040*\040from\040user\040select\040*\040from\040user;
use\040mysql
show\040databeses\040;
show\040databases;
use\040mysql;
show\040tables;
desc\040db;
shwo\040create\040teble\040user\134G;
show\040create\040teble\040user\134G;
show\040create\040table\040user\134G;
show\040create\040table\040user;
select\040user();
```
### 3.8 檢視當前所使用的資料庫
切換回來
```
[[email protected] ~]# !mys
mysql -uroot -pxavilinux
mysql> select database();
+------------+
| database() |
+------------+
| NULL |
+------------+
1 row in set (0.01 sec)
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> select database();
+------------+
| database() |
+------------+
| mysql |
+------------+
1 row in set (0.00 sec)
```
### 3.9 建立一個新庫
```
mysql> create database db1;
Query OK, 1 row affected (0.01 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| db1 |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.03 sec)
```
### 3.10 建立一個新表
```
mysql> use db1;
Database changed
mysql> create table t1(`id` int(4), `name` char(40));
Query OK, 0 rows affected (0.03 sec)
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)
```
### 3.11刪除一個表,並指定表的ENGINE和CHARSET
```
mysql> drop table t1;
Query OK, 0 rows affected (0.01 sec)
mysql> create table t1(`id` int(4), `name` char(40)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.04 sec)
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.00 sec)
```
### 3.12 檢視資料庫的版本
```
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.6.35 |
+-----------+
1 row in set (0.00 sec)
```
### 3.13 檢視MySQL的當前狀態
```
mysql> show status;
+-----------------------------------------------+-------------+
| Variable_name | Value |
+-----------------------------------------------+-------------+
| Aborted_clients | 0 |
| Aborted_connects | 0 |
| Binlog_cache_disk_use | 0 |
| Binlog_cache_use | 0 |
| Binlog_stmt_cache_disk_use | 0 |
| Binlog_stmt_cache_use | 0 |
| Bytes_received | 1248 |
| Bytes_sent | 22408 |
| Com_admin_commands | 0 |
| Com_assign_to_keycache | 0 |
```
### 3.14 檢視MySQL的引數,其中很多引數都是可以在/etc/my.cnf中定義,部分引數可線上編輯
```
mysql> show variables;
```
### 3.15 模糊查詢某引數
```
mysql> show variables like 'max_connect%';
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| max_connect_errors | 100 |
| max_connections | 151 |
+--------------------+-------+
2 rows in set (0.00 sec)
mysql> show variables like 'slow%';
+---------------------+---------------------------+
| Variable_name | Value |
+---------------------+---------------------------+
| slow_launch_time | 2 |
| slow_query_log | OFF |
| slow_query_log_file | /data/mysql/xavi-slow.log |
+---------------------+---------------------------+
3 rows in set (0.00 sec)
```
### 3.16 修改某引數
```
mysql> set global max_connect_errors=1000;
Query OK, 0 rows affected (0.02 sec)
mysql> show variables like 'max_connect_errors';
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| max_connect_errors | 1000 |
+--------------------+-------+
1 row in set (0.01 sec)
```
### 3.17 檢視當前MySQL伺服器佇列,檢視MySQL在幹什麼,是否有鎖表
```
mysql> show processlist;
+----+------+-----------+------+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+------+---------+------+-------+------------------+
| 6 | root | localhost | db1 | Query | 0 | init | show processlist |
+----+------+-----------+------+---------+------+-------+------------------+
1 row in set (0.00 sec)
mysql> show full processlist;
+----+------+-----------+------+---------+------+-------+-----------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+------+---------+------+-------+-----------------------+
| 6 | root | localhost | db1 | Query | 0 | init | show full processlist |
+----+------+-----------+------+---------+------+-------+-----------------------+
1 row in set (0.00 sec)
```
## 四、myisam 和innodb引擎對比
### MySQL儲存引擎MyISAM與InnoDB的優劣
> 使用MySQL當然會接觸到MySQL的儲存引擎,在新建資料庫和新建資料表的時候都會看到。
MySQL預設的儲存引擎是MyISAM,其他常用的就是InnoDB了。
至於到底用哪種儲存引擎比較好?這個問題是沒有定論的,需要根據你的需求和環境來衡量。所以對這兩種引擎的概念、原理、異同和各自的優劣點有了詳細的瞭解之後,再根據自己的情況選擇起來就容易多了。
![mark](http://picture.xavilinux.work/xavi2017/180322/1bfI0JAC98.png?imageslim)
![mark](http://picture.xavilinux.work/xavi2017/180322/jli1jdmiba.png?imageslim)
![mark](http://picture.xavilinux.work/xavi2017/180322/BdAdLClgmJ.png?imageslim)
> 總的來說,MyISAM和InnoDB各有優劣,各有各的使用環境。
> 但是InnoDB的設計目標是處理大容量資料庫系統,它的CPU利用率是其它基於磁碟的關係資料庫引擎所不能比的。
> 我覺得使用InnoDB可以應對更為複雜的情況,特別是對併發的處理要比MyISAM高效。同時結合memcache也可以快取SELECT來減少SELECT查詢,從而提高整體效能。