1. 程式人生 > >十三周四次課

十三周四次課

linux

十三周四次課(3月22日)
13.1 設置更改root密碼
13.2 連接mysql
13.3 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@tianqi-01 vhost]# ps aux |grep mysql

root 838 0.0 0.1 115388 1680 ? S 20:05 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/tianqi-01.pid

mysql 998 0.0 46.0 981248 460236 ? Sl 20:05 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/tianqi-01.err --pid-file=/data/mysql/tianqi-01.pid --socket=/tmp/mysql.sock

root 4175 0.0 0.0 112664 984 pts/0 S+ 21:58 0:00 grep --color=auto mysql

[root@tianqi-01 vhost]#

2.若是沒有啟動mysql的話,將mysql啟動起來


[root@tianqi-01 ~]# /etc/init.d/mysqld start

Starting MySQL [ OK ]

[root@tianqi-01 ~]# 180321 22:06:42 mysqld_safe A mysqld process already exists

[root@tianqi-01 ~]#

[root@tianqi-01 ~]# !ps

ps aux |grep mysql

root 838 0.0 0.1 115388 1680 ? S 20:05 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/tianqi-01.pid

mysql 998 0.0 46.0 981248 460236 ? Sl 20:05 0:06 /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/tianqi-01.err --pid-file=/data/mysql/tianqi-01.pid --socket=/tmp/mysql.sock

root 4418 0.0 0.0 112660 984 pts/1 R+ 22:08 0:00 grep --color=auto mysql

[root@tianqi-01 ~]#

3.在啟動mysql後,使用mysql -uroot命令,但是mysql命令會提示不存在,因為安裝的mysql是在/usr/local/mysql/bin/mysql,而這個目錄並不在環境變量PATH裏面,所以它會報錯

[root@tianqi-01 ~]# mysql -uroot

-bash: mysql: command not found

[root@tianqi-01 ~]# ls /usr/local/mysql/bin/mysql

/usr/local/mysql/bin/mysql

[root@tianqi-01 ~]# echo $PATH

/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

[root@tianqi-01 ~]#

4.若想要這個命令直接運行,需要把PATH做一個更改

[root@tianqi-01 ~]# export PATH=$PATH:/usr/local/mysql/bin/

[root@tianqi-01 ~]#

5.這時再來使用mysql -uroot命令就會發現可以使用了

  • 退出mysql輸入 quit 即可

[root@tianqi-01 ~]# 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@tianqi-01 ~]#

6.若想要變量永久生效,還需要將export PATH=$PATH:/usr/local/mysql/bin/ 放入到 /etc/profile

//將內容放到配置文件的最後面,使命令永久生效

export PATH=$PATH:/usr/local/mysql/bin/

保存退出

7.假設若是沒有運行 export PATH=$PATH:/usr/local/mysql/bin/ 命令,那也不能運行mysql,因為變量還沒有生效,想要這個變量生效,在配置文件中加入命令後,還需要執行source /etc/profile 命令,重新加載

[root@tianqi-01 ~]# source /etc/profile

[root@tianqi-01 ~]#

8.一般是使用mysql -uroot -p命令

  • -p,表示指定密碼

9.密碼為空的時候,直接回車就可進入到mysql,並可以在其中操作一些mysql的一些行為

[root@tianqi-01 ~]# 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@tianqi-01 ~]#


10.退出mysql,輸入 quit 即可

11.設置mysql密碼,命令為mysqladmin -uroot passwd 'tianqi.1' 在 ' ' 為密碼

[root@tianqi-01 ~]# mysqladmin -uroot password 'tianqi.1'

Warning: Using a password on the command line interface can be insecure.

[root@tianqi-01 ~]#

12.在設置密碼的時候,會看到有輸出信息,但這不是報錯信息,這是告訴你你現在密碼在當前命令行顯示出來了,這樣不×××全

13.這時在想直接登錄mysql,就會提示需要輸入密碼了


[root@tianqi-01 ~]# mysql -uroot

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

14.再使用-p,並輸入密碼就可以正常的進入到mysql命令行了

[root@tianqi-01 ~]# 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@tianqi-01 ~]#

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

1.若是這時知道mysql密碼,去修改mysql密碼,看到輸出的提示信息不用去理會

  • 格式

    • mysqladmin -uroot -p'hanfeng.1' password 'hanfeng'


[root@tianqi-01 ~]# mysqladmin -uroot -p'tianqi.1' password 'tianqi.2'

Warning: Using a password on the command line interface can be insecure.

[root@tianqi-01 ~]#

2.指定新密碼去登錄,當然也可以不明文指定密碼,直接-p回車,輸入密碼登錄也行

[root@tianqi-01 ~]# mysql -uroot -p'tianqi.2'

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@tianqi-01 ~]#

[root@tianqi-01 ~]# mysql -uroot -p

Enter password:

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>

3.在明文指定密碼的時候,密碼可以加單引號,也可以不加單引號,建議加上單引號,防止密碼有特殊符號的存在——>(若是不加單引號,而密碼中又有特殊符號,就有可能會不識別)

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

1.在不知道mysql的root用戶密碼的時候,先去更改 /etc/my.cnf 下配置文件中加入skip-grant

  • skip-grant ,表示忽略授權,也就是說操作mysql的時候不需要用戶名和密碼了,能直接登錄

[root@tianqi-01 ~]# vim /etc/my.cnf

//在[mysqld]下面

加入一行

skip-grant

保存退出

2.在更改配置文件後,重啟mysql服務

[root@tianqi-01 ~]# /etc/init.d/mysqld restart

Shutting down MySQL... [ OK ]

Starting MySQL....... [ OK ]

[root@tianqi-01 ~]#

3.這時候再輸入mysql -uroot ,會發現直接進入mysql,而不需要密碼了

[root@tianqi-01 ~]# 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@tianqi-01 ~]#

4.在登錄進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>

5.查看user表,輸入select * from user; 命令,會看到輸出亂七八糟的亂碼,裏面存放的就是用戶名和密碼,還有權限和授權等信息

mysql> select * from user;

………………

| Host | User | Password

………………

//省略指出都是亂碼

6.查看password表,會看到密碼都是加密的

mysql> select password from user where user='root';

+-------------------------------------------+

| password |

+-------------------------------------------+

| *1D6E44B005B8029764C395EE17EBA018CB2DD9A2 |

| |

| |

| |

+-------------------------------------------+

4 rows in set (0.05 sec)


mysql>

7.更改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.10 sec)

Rows matched: 4 Changed: 4 Warnings: 0


mysql>

8.提示說4行修改完畢,即使有些行是空的

9.這樣密碼就更改成功了,輸入quit退出數據庫即可

mysql> quit

Bye

[root@tianqi-01 ~]#

10.再去 /etc/my.cnf 配置文件中刪除免授權配置,即刪除skip-grant——>若是不刪除,那麽之後所有的用戶都不需要輸入密碼,就可以登錄進去,這樣安全性太低

[root@tianqi-01 ~]# vim /etc/my.cnf

//在[mysqld]下面刪除剛添加的

skip-grant

保存退出

11.重啟mysql服務

[root@tianqi-01 ~]# /etc/init.d/mysqld restart

Shutting down MySQL.. [ OK ]

Starting MySQL....... [ OK ]

[root@tianqi-01 ~]#

12.重啟完之後,再用新的密碼測試下,會看到新的密碼可以登錄

[root@tianqi-01 ~]# 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@tianqi-01 ~]#

13.這樣就是成功更改mysql密碼

13.2 連接mysql

連接mysql

  • 本地連接——>即使沒有指定,但默認使用sock連接,使用/tmp/mysql.sock連接

    • mysql -uroot -p123456 //輸入用戶名和密碼連接本機

  • 使用ip端口連接遠程機器

    • mysql -uroot -p123456 -h[遠程mysql主機IP] -P[端口]

[root@tianqi-01 ~]# mysql -uroot -paminglinux -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 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@tianqi-01 ~]#

  • 使用sock方式連接(只適合在本機使用)如為指定IP就用sock訪問

  • mysql -uroot -p123456 -S/tmp/mysql.sock

[root@tianqi-01 ~]# mysql -uroot -paminglinux -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 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.


mysql> quit

Bye

[root@tianqi-01 ~]#

  • 連接mysql之後執行命令(常用於shell腳本

  • mysql -uroot -p123456 -e “show databases”

    • show databases //列出所有數據庫

[root@tianqi-01 ~]# mysql -uroot -paminglinux -e “show databases”

Warning: Using a password on the command line interface can be insecure.

+--------------------+

| Database |

+--------------------+

| information_schema |

| mysql |

| performance_schema |

| test |

+--------------------+

[root@tianqi-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@tianqi-01 ~]# 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 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>


2.查看數據庫 show databases;

mysql> show databases;

+--------------------+

| Database |

+--------------------+

| information_schema |

| mysql |

| performance_schema |

| test |

+--------------------+

4 rows in set (0.00 sec)


mysql>

3.切換庫 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>

4.列出所有的表 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.01 sec)


mysql>


5.查看表裏的字段 desc tb_name;

6.例子

  • desc user; //查看user表裏的所有字段;需要進入相關的庫以後才能查看對應的表

//庫是由表組成的,表是由字段組成的

mysql> desc user;

+------------------------+-----------------------------------+------+-----+-----------------------+-------+

| Field | Type | Null | Key | Default | Extra |

+------------------------+-----------------------------------+------+-----+-----------------------+-------+

| Host | char(60) | NO | PRI | | |

| User | char(16) | NO | PRI | | |

| Password | char(41) | NO | | | |

| 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 | |

| Reload_priv | enum('N','Y') | NO | | N | |

| Shutdown_priv | enum('N','Y') | NO | | N | |

| Process_priv | enum('N','Y') | NO | | N | |

| File_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 | |

| Show_db_priv | enum('N','Y') | NO | | N | |

| Super_priv | enum('N','Y') | NO | | N | |

| Create_tmp_table_priv | enum('N','Y') | NO | | N | |

| Lock_tables_priv | enum('N','Y') | NO | | N | |

| Execute_priv | enum('N','Y') | NO | | N | |

| Repl_slave_priv | enum('N','Y') | NO | | N | |

| Repl_client_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 | |

| Create_user_priv | enum('N','Y') | NO | | N | |

| Event_priv | enum('N','Y') | NO | | N | |

| Trigger_priv | enum('N','Y') | NO | | N | |

| Create_tablespace_priv | enum('N','Y') | NO | | N | |

| ssl_type | enum('','ANY','X509','SPECIFIED') | NO | | | |

| ssl_cipher | blob | NO | | NULL | |

| x509_issuer | blob | NO | | NULL | |

| x509_subject | blob | NO | | NULL | |

| max_questions | int(11) unsigned | NO | | 0 | |

| max_updates | int(11) unsigned | NO | | 0 | |

| max_connections | int(11) unsigned | NO | | 0 | |

| max_user_connections | int(11) unsigned | NO | | 0 | |

| plugin | char(64) | YES | | mysql_native_password | |

| authentication_string | text | YES | | NULL | |

| password_expired | enum('N','Y') | NO | | N | |

+------------------------+-----------------------------------+------+-----+-----------------------+-------+

43 rows in set (0.03 sec)


mysql>

//Field就是代表了字段


7.查看建表語句 show create table tb_name\G;——>G 表示豎排顯示

8.例子

  • show create table user\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.00 sec)


mysql>

//這樣的結果比上面的表示還要詳細

9.查看當前用戶 select user();

mysql> select user();

+----------------+

| user() |

+----------------+

| root@localhost |

+----------------+

1 row in set (0.01 sec)


mysql>

  • 這裏的localhost 表示主機名,會反解析你的IP所屬主機名

10.在mysql裏面使用 方向鍵 可以查看之前使用的命令,和shell的命令類似,並且也有記錄命令歷史的功能,默認的緩存文件在 / 下,名字為“.mysql_history”

  • 支持ctrl+l 清屏

[root@tianqi-01 ~]# ls -la

total 68

dr-xr-x---. 6 root root 238 Mar 22 21:36 .

dr-xr-xr-x. 18 root root 256 Feb 27 08:31 ..

-rw-------. 1 root root 1420 Jan 26 09:23 anaconda-ks.cfg

-rw-------. 1 root root 27003 Mar 21 22:36 .bash_history

-rw-r--r--. 1 root root 18 Dec 29 2013 .bash_logout

-rw-r--r--. 1 root root 176 Dec 29 2013 .bash_profile

-rw-r--r--. 1 root root 176 Dec 29 2013 .bashrc

-rw-r--r--. 1 root root 100 Dec 29 2013 .cshrc

-rw------- 1 root root 716 Mar 22 21:36 .mysql_history

drwxr----- 3 root root 19 Feb 27 20:16 .pki

drwxr-xr-x 2 root root 6 Mar 5 08:43 practice

-rw------- 1 root root 1024 Mar 21 20:44 .rnd

drwxr-xr-x. 2 root root 157 Jan 26 10:23 shell

drwx------ 2 root root 29 Mar 19 22:52 .ssh

-rw-r--r--. 1 root root 129 Dec 29 2013 .tcshrc

-rw------- 1 root root 7460 Mar 22 19:52 .viminfo

[root@tianqi-01 ~]#

[root@tianqi-01 ~]# less .mysql_history



_HiStOrY_V2_

use\040mysql

select\040*\040from\040user;

show\040databases;

use\040mysql;

show\040tables;

desc\040users;

desc\040user;

show\040create\040table\040user\134G

show\040create\040table\040user

show\040create\040table\040user;

show\040create\040table\040user\040show\040create\040table\040user;

show\040create\040table\040user

show\040create\040user;

show\040create\040table\040user;

select\040*from\040user;

show\040create\040table\040user;

show\040create\040table\040user\134G;

use\040user;

show\040databases;

use\040mysql;

show\040tables;

desc\040user;

show\040create\040table\040user;

show\040create\040table\040user\134G;

select\040*\040from\040user;

select\040*\040from\040user\134G;

select\040user();

11.查看當前使用的數據庫 select database();

mysql> select database();

+------------+

| database() |

+------------+

| NULL |

+------------+

1 row in set (0.00 sec)


mysql>

//NULL代表為空

mysql> use mysql; //切換到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)


mysql>



創建庫

1.創建庫 create database db1;

mysql> create database db1;

Query OK, 1 row affected (0.00 sec)


mysql>

2.查看db1庫

mysql> show databases;

+--------------------+

| Database |

+--------------------+

| information_schema |

| db1 |

| mysql |

| performance_schema |

| test |

+--------------------+

5 rows in set (0.00 sec)


mysql>

3.切換到db1庫下面去

mysql> use db1;

Database changed

mysql>

4.創建表 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.17 sec)


mysql>

5.查看創建的表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.12 sec)


ERROR:

No query specified


mysql>

6.在數據庫裏面取消命令,即在命令的最前面加一個 # 號就不會生效了

7.刪除表 drop table t1;

mysql> drop table t1;

Query OK, 0 rows affected (0.08 sec)


mysql>

8.創建t1表

mysql> create table t1(`id` int(4), `name` char(40)) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Query OK, 0 rows affected (0.03 sec)


mysql>

9.查看表,會看到變成了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.00 sec)


ERROR:

No query specified


mysql>

10.查看當前數據庫版本 select version();

mysql> select version();

+-----------+

| version() |

+-----------+

| 5.6.35 |

+-----------+

1 row in set (0.05 sec)


mysql>

11.查看數據庫狀態 show status; 它會把常用的數據都列出來

mysql> show status;

+-----------------------------------------------+-------------+

| Variable_name | Value |

+-----------------------------------------------+-------------+

| Aborted_clients | 0 |

| Aborted_connects | 10 |

| Binlog_cache_disk_use | 0 |

| Binlog_cache_use | 0 |

| Binlog_stmt_cache_disk_use | 0 |

| Binlog_stmt_cache_use | 0 |

| Bytes_received | 1235 |

| Bytes_sent | 22520 |

| Com_admin_commands | 0 |

| Com_assign_to_keycache | 0 |

| Com_alter_db | 0 |

| Com_alter_db_upgrade | 0 |

…………………………………………//下面省略很多

12.查看各參數

  • 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> show variables like 'slow%';

+---------------------+--------------------------------+

| Variable_name | Value |

+---------------------+--------------------------------+

| slow_launch_time | 2 |

| slow_query_log | OFF |

| slow_query_log_file | /data/mysql/tianqi-01-slow.log |

+---------------------+--------------------------------+

3 rows in set (0.00 sec)


mysql>

13.修改參數 set global max_connect_errors=1000; ——>僅在內存中生效

mysql> set global max_connect_errors=1000;

Query OK, 0 rows affected (0.04 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]裏面定義

14.查看隊列

  • show processlist; //查看庫的狀況,比如,那些用戶在連,做了些什麽操作,是否鎖表

mysql> show processlist;

+----+------+-----------+------+---------+------+-------+------------------+

| Id | User | Host | db | Command | Time | State | Info |

+----+------+-----------+------+---------+------+-------+------------------+

| 19 | 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 |

+----+------+-----------+------+---------+------+-------+-----------------------+

| 19 | root | localhost | db1 | Query | 0 | init | show full processlist |

+----+------+-----------+------+---------+------+-------+-----------------------+

1 row in set (0.00 sec)


mysql>

十三周四次課