[toc]
# MySQL密码相关,链接方式,经常使用命令
扩展
mysql5.7 root密码更改(较5.6版本变化大) http://www.apelearn.com/bbs/thread-7289-1-1.html html
myisam 和innodb引擎对比 http://www.pureweber.com/article/myisam-vs-innodb/ mysql
mysql 配置详解: http://blog.linuxeye.com/379.html linux
mysql调优: http://www.aminglinux.com/bbs/thread-5758-1-1.html web
同窗分享的亲身mysql调优经历: http://www.apelearn.com/bbs/thread-11281-1-1.html sql
## 1、MySQL密码修改
### 1. 判断mysql是否开启shell
```
[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@xavi ~]# !ps
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
[root@xavi ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@xavi ~]# export PATH=$PATH:/usr/local/mysql/bin/
[root@xavi ~]# 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)vim
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加载配置
```
[root@xavi ~]# vim /etc/profile
[root@xavi ~]# source /etc/profile
```

### 2.给MySQL创建密码:mysqladmin -uroot password 'xavilinux.1',而后登入
```
[root@xavi ~]# mysqladmin -uroot password 'xavilinux.1'
Warning: Using a password on the command line interface can be insecure.
[root@xavi ~]# mysql -uroot
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@xavi ~]# 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修改原密码
```
[root@xavi ~]# mysqladmin -uroot -p'xavilinux.1' password 'xavilinux'
Warning: Using a password on the command line interface can be insecure.
```
#### 2.2登入,记得密码的单引号,和-p后面没有空格的
```
[root@xavi ~]# 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,忽略受权

##### b.保存后重启MySQL服务,进入
```
[root@xavi ~]# /etc/init.d/mysqld restart
Shutting down MySQL.... SUCCESS!
Starting MySQL. SUCCESS!
[root@xavi ~]# 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 ;
```

```
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删除,恢复密码验证,确保安全

#### f.重启服务,输入密码登入sql
```
[root@xavi ~]# vi /etc/my.cnf
[root@xavi ~]# /etc/init.d/mysqld restart
ERROR! MySQL server PID file could not be found!
Starting MySQL... SUCCESS!
[root@xavi ~]# 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>
```
## 2、链接数据库的方式
### 1.mysql -uroot -pxavilinux
```
[root@xavi ~]# 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
```
[root@xavi ~]# 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,只适合本机登入

```
[root@xavi ~]# 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脚本里面经常使用
```
[root@xavi ~]# 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 |
+--------------------+
```
## 3、MySQL基本操做的经常使用命令
记住MySQL的操做必须进入mysql后执行
```
[root@xavi ~]# 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

### 3.6 查看当前是哪一个用户
```
mysql> select user();
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
```
### 3.7 查看mysql历史指令记录状况
ls -la

```
[root@xavi ~]# 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 查看当前所使用的数据库
切换回来
```
[root@xavi ~]# !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)
```
## 4、myisam 和innodb引擎对比
### MySQL存储引擎MyISAM与InnoDB的优劣
> 使用MySQL固然会接触到MySQL的存储引擎,在新建数据库和新建数据表的时候都会看到。
MySQL默认的存储引擎是MyISAM,其余经常使用的就是InnoDB了。
至于到底用哪一种存储引擎比较好?这个问题是没有定论的,须要根据你的需求和环境来衡量。因此对这两种引擎的概念、原理、异同和各自的优劣点有了详细的了解以后,再根据本身的状况选择起来就容易多了。



> 总的来讲,MyISAM和InnoDB各有优劣,各有各的使用环境。
> 可是InnoDB的设计目标是处理大容量数据库系统,它的CPU利用率是其它基于磁盘的关系数据库引擎所不能比的。
> 我以为使用InnoDB能够应对更为复杂的状况,特别是对并发的处理要比MyISAM高效。同时结合memcache也能够缓存SELECT来减小SELECT查询,从而提升总体性能。