SQL -- mysql經常使用命令

###1.鏈接mysql
格式:mysql -h 主機地址 -P 端口號 -u 用戶名 -p用戶密碼
打開dos窗口,輸入命令mysql

mysql -h localhost -P3306 -u root -p654321

1.若是mysql服務器安裝在本臺主機上,能夠省略 -h localhost
2.-p後面直接輸入密碼,不能帶有空格
3.若是是剛安裝好的mysql,超級用戶root是沒有密碼的,直接輸入命令 mysql -h localhost -u root -p 就能夠進入mysql,接着修改/設置密碼參看我這篇 http://www.javashuo.com/article/p-pgovrjdo-kd.html
4.退出mysql命令:exit(回車)
###2.修改密碼
有兩種方法
####方法1:以root用戶登陸進去了sql

mysql> set password for root@localhost=password('654321');
Query OK, 0 rows affected, 1 warning (0.04 sec)

mysql>

####方法2:未登陸
注意,最好是在mysql安裝目錄bin目錄下,執行以下命令
mysqladmin -u 用戶名 -p舊密碼 password 新密碼數據庫

E:\mysql-5.7.14-winx64\mysql-5.7.14-winx64\bin>mysqladmin -u root -p654321 passw
ord 777888999
mysqladmin: [Warning] Using a password on the command line interface can be inse
cure.
Warning: Since password will be sent to server in plain text, use ssl connection
 to ensure password safety.

E:\mysql-5.7.14-winx64\mysql-5.7.14-winx64\bin>

由上可知,個人mysql安裝在E:\mysql-5.7.14-winx64\目錄下
注意事項:
1.-p後面是原密碼,這裏沒有空格
2.password後面是新密碼,這裏有空格
###3.增長新用戶
####1.建立新用戶(不帶密碼)
進入mysql後,採用以下命令查看目前有多少個databases
輸入圖片說明
如上可知,test數據庫是我建立的,其它均爲系統自帶。
information_schema:稱它爲信息數據庫,它提供了訪問數據庫"元數據"的方式,元數據是指:數據庫名、表名、列的數據類型、訪問權限等。簡單的說,該表就是保存其它數據庫的 元數據。
performance_schema:它是一個存儲引擎,主要是保存數據庫服務器性能參數。例如:提供進程等待信息、保存歷史事件等。
全部的用戶都存放在mysql數據庫中。
使用mysql數據庫命令安全

mysql> use mysql;
Database changed
mysql>

建立新用戶服務器

mysql> create user panda;
Query OK, 0 rows affected (0.00 sec)

mysql>

如上可知,建立了一個叫作panda的用戶,該用戶能夠在任意安裝了mysql的客戶端(例如:navicat),無需密碼就能訪問目標服務器上的mysql數據庫。
查看剛剛新建的用戶採用以下命令:ide

mysql> select user from user;
+------------+
| user       |
+------------+
| panda      |
| reply_user |
| mysql.sys  |
| root       |
+------------+
4 rows in set (0.00 sec)

mysql>

由如上信息可知,mysql數據庫中有4個用戶,很明顯panda用戶是剛剛新建的。
顯示當前用戶採用以下命令性能

mysql> select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)

mysql>

很明顯當前用戶是root。
####2.建立新用戶(帶上密碼)
很明顯,受權panda登錄本身的mysql服務器是不安全的,任何人均可以在任何地方使用panda來訪問/修改本身的數據庫。所以,須要建立帶密碼的用戶。採用以下命令加密

mysql> create user lxl identified by '123456';
Query OK, 0 rows affected (0.00 sec)

mysql>

####3.指定用戶在指定的主機上訪問數據庫
如上的lxl用戶在任何一臺主機上均可以訪問本身的數據庫,也不是很安全,這裏設置lxl用戶只能在指定的主機上訪問本身的數據庫。.net

mysql> create user zhangSan@192.168.2.11 identified by '123456';
Query OK, 0 rows affected (0.00 sec)

mysql>

如上是 指定用戶 zhangSan只能在192.168.2.11這臺主機上採用密碼 123456 來訪問本身的數據庫。
這裏在看下剛剛新建的用戶code

mysql> select user,authentication_string from user;
+------------+-------------------------------------------+
| user       | authentication_string                     |
+------------+-------------------------------------------+
| root       | *8D641143581B4D519215B231D2305B0CE66CF3A2 |
| mysql.sys  | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| reply_user | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| panda      |                                           |
| lxl        | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| zhangSan   | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+------------+-------------------------------------------+
6 rows in set (0.00 sec)

mysql>

由上可知,用戶panda是沒有密碼的。用戶lxl和zhangSan的密碼是同樣的(通過加密了)。
####4.授予用戶權限
命令:grant 權限1,權限2...權限n on 數據庫名稱 表名稱 to 用戶名@用戶地址 identified by '密碼'
權限有哪些
1>普通數據用戶
普通數據用戶具備增、刪、改、查的權利
例如,給如上 lxl 用戶賦予 增刪改查的權限

mysql> grant select,insert,update,delete on test.* to lxl;
Query OK, 0 rows affected (0.07 sec)

查詢用戶具有哪些權限,使用以下命令

mysql> show grants for lxl;
+---------------------------------------------------------------+
| Grants for lxl@%                                              |
+---------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'lxl'@'%'                               |
| GRANT SELECT, INSERT, UPDATE, DELETE ON `test`.* TO 'lxl'@'%' |
+---------------------------------------------------------------+
2 rows in set (0.00 sec)

mysql>

####5.刪除用戶
方式1:採用drop user 用戶名

mysql> select user from user;
+-----------+
| user      |
+-----------+
| lxl       |
| panda     |
| mysql.sys |
| root      |
| zhangSan  |
+-----------+
5 rows in set (0.00 sec)

mysql> drop user zhangSan@localhost;
Query OK, 0 rows affected (0.00 sec)

mysql> select user from user;
+-----------+
| user      |
+-----------+
| lxl       |
| panda     |
| mysql.sys |
| root      |
+-----------+
4 rows in set (0.00 sec)

mysql>

方式2:採用delete from user where user=用戶名

mysql> delete from user where user='lxl';
Query OK, 1 row affected (0.00 sec)

mysql> select user from user;
+-----------+
| user      |
+-----------+
| panda     |
| mysql.sys |
| root      |
+-----------+
3 rows in set (0.00 sec)

mysql> select user from db;
+-----------+
| user      |
+-----------+
| lxl       |
| mysql.sys |
+-----------+
2 rows in set (0.00 sec)

mysql>

由上信息可知,採用delete刪除用戶時,僅僅將該用戶在user表裏刪除了,可是該用戶依然存在db表中。
總結:drop user,將該用戶的信息所有刪掉。delete只會清除user表,還會存在於db表,即若是delete以後,再建立一個最小權限的用戶,它將會繼承以前的權限。
####6.修改用戶權限
命令:revoke 權限 on 數據庫.表 from 用戶

mysql> show grants for panda;
+-----------------------------------------------------------------+
| Grants for panda@%                                              |
+-----------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'panda'@'%'                               |
| GRANT SELECT, INSERT, UPDATE, DELETE ON `test`.* TO 'panda'@'%' |
+-----------------------------------------------------------------+
2 rows in set (0.00 sec)

mysql> revoke delete on test.* from panda;
Query OK, 0 rows affected (0.00 sec)

mysql> show grants for panda;
+---------------------------------------------------------+
| Grants for panda@%                                      |
+---------------------------------------------------------+
| GRANT USAGE ON *.* TO 'panda'@'%'                       |
| GRANT SELECT, INSERT, UPDATE ON `test`.* TO 'panda'@'%' |
+---------------------------------------------------------+
2 rows in set (0.00 sec)

mysql>

###4.建立數據庫
####4.1 建立數據庫
命令:create database 數據庫名稱

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+
5 rows in set (0.02 sec)

mysql> create database study;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| study              |
| sys                |
| test               |
+--------------------+
6 rows in set (0.00 sec)

mysql>

####4.2 顯示數據庫
命令:show databases;(注意最後有個s)
####4.3 刪除數據庫
命令:drop database 數據庫名稱;

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| study              |
| sys                |
| test               |
+--------------------+
6 rows in set (0.00 sec)

mysql> drop database study;
Query OK, 0 rows affected (0.01 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+
5 rows in set (0.00 sec)

mysql>

####4.4 數據庫的一些信息
1.查詢mysql版本

mysql> select version();
+-----------+
| version() |
+-----------+
| 5.7.13    |
+-----------+
1 row in set (0.01 sec)

mysql>

2.顯示當前日期

mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2016-09-17 12:23:32 |
+---------------------+
1 row in set (0.03 sec)

mysql>

###5.數據表
####1.顯示錶
命令:show tables;

mysql> use test;
Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t_book         |
| t_stock        |
| t_thing        |
| t_user         |
+----------------+
4 rows in set (0.00 sec)

mysql>

####2.建立表
命令:create table 表名(<字段名><類型名(位數)>[<字段名><類型名(位數)>,......]);

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t_book         |
| t_stock        |
| t_thing        |
| t_user         |
+----------------+
4 rows in set (0.00 sec)

mysql> create table t_location
    -> (
    -> location_id int(10) primary key,
    -> location_name varchar(100)
    -> );
Query OK, 0 rows affected (0.11 sec)

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t_book         |
| t_location     |
| t_stock        |
| t_thing        |
| t_user         |
+----------------+
5 rows in set (0.00 sec)

mysql>

####3.刪除表

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t_book         |
| t_location     |
| t_stock        |
| t_thing        |
| t_user         |
+----------------+
5 rows in set (0.00 sec)

mysql> drop table t_location;
Query OK, 0 rows affected (0.06 sec)

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t_book         |
| t_stock        |
| t_thing        |
| t_user         |
+----------------+
4 rows in set (0.00 sec)

mysql>

####4.向表中插入數據
命令:insert into 表名(<字段名1>,<字段名2>,...) values(值1,值2,...)

mysql> insert into t_location(location_id,location_name) values(1000000000,'成都');
Query OK, 1 row affected (0.02 sec)

mysql> select * from t_location;
+-------------+---------------+
| location_id | location_name |
+-------------+---------------+
|  1000000000 | 成都          |
+-------------+---------------+
1 row in set (0.00 sec)

mysql>

####5.增長字段
命令:alter table 表名 add 字段 類型;

mysql> alter table t_location add grade int(1);
Query OK, 0 rows affected (0.13 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> select * from t_location;
+-------------+---------------+-------+
| location_id | location_name | grade |
+-------------+---------------+-------+
|  1000000000 | 成都          |  NULL |
+-------------+---------------+-------+
1 row in set (0.00 sec)

mysql>

####6.修改字段
命令:alter table 表名 change 原字段 新字段 類型;

mysql> alter table t_location change grade bank int(1);
Query OK, 0 rows affected (0.06 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> select * from t_location;
+-------------+---------------+------+
| location_id | location_name | bank |
+-------------+---------------+------+
|  1000000000 | 成都          | NULL |
+-------------+---------------+------+
1 row in set (0.00 sec)

mysql>

####7.刪除字段
命令:alter table 表名 drop 字段名;

mysql> alter table t_location drop bank;
Query OK, 0 rows affected (0.12 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> select * from t_location;
+-------------+---------------+
| location_id | location_name |
+-------------+---------------+
|  1000000000 | 成都          |
+-------------+---------------+
1 row in set (0.00 sec)

mysql>

####8.更新數據
命令:update 表名 set 字段1=新值1,... where 條件;

mysql> update t_location set location_name='北京';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from t_location;
+-------------+---------------+
| location_id | location_name |
+-------------+---------------+
|  1000000000 | 北京          |
+-------------+---------------+
1 row in set (0.02 sec)

mysql>

####9.修改表名
命令:rename table 原表名 to 新表名;

mysql> rename table t_location to t_address;
Query OK, 0 rows affected (0.07 sec)

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t_address      |
| t_book         |
| t_stock        |
| t_thing        |
| t_user         |
+----------------+
5 rows in set (0.00 sec)

mysql>

###6.數據備份
####1.導出數據庫
命令:mysqldump -u 用戶名 -p密碼 數據庫名稱 > 導出的文件名稱;
首先進入mysql的安裝路徑,一直到bin下,接着使用如上命令

D:\softDown\mysql\mysql-5.7.13-winx64>cd bin
D:\softDown\mysql\mysql-5.7.13-winx64\bin>mysqldump -u root -p123456 test > test.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
D:\softDown\mysql\mysql-5.7.13-winx64\bin>

這樣導出的文件就在bin目錄下面。
####2.導出單張表數據
命令:mysqldump -u 用戶名 -p密碼 數據庫名稱 表名稱 > 導出的文件名稱;
實際上就是在數據庫名稱後面添加上表名稱 就行。

D:\softDown\mysql\mysql-5.7.13-winx64\bin>mysqldump -u root -p123456 test t_user > test_user.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
D:\softDown\mysql\mysql-5.7.13-winx64\bin>
相關文章
相關標籤/搜索