mysql常用命令總結

1.登錄數(shù)據(jù)庫

成都網站設計、網站建設服務團隊是一支充滿著熱情的團隊,執(zhí)著、敏銳、追求更好,是創(chuàng)新互聯(lián)的標準與要求,同時竭誠為客戶提供服務是我們的理念。創(chuàng)新互聯(lián)把每個網站當做一個產品來開發(fā),精雕細琢,追求一名工匠心中的細致,我們更用心!

[root@web01 data]# MySQL -uroot -p

2.更改已知用戶的密碼

[root@web01 ~]# mysqladmin  -uroot -p123456 password "oldboy"

3.初始化root的密碼

 [root@web01 ~]# mysqladmin -uroot password "123456"

4.查看數(shù)據(jù)庫

mysql> show databases;

5.使用數(shù)據(jù)庫

mysql> use mysql;

Database changed

mysql> 

6.查看數(shù)據(jù)庫的表

mysql> show tables;

7.查看數(shù)據(jù)表的結構

mysql> describe user;

mysql> show columns  from  test;

8.從表中查找字段(此處查詢的字段不區(qū)分大小寫)

mysql> select Host,User  from user;  #直接查表

mysql> select host,user  from mysql.user;  #從哪個庫的哪一個表

9.刪除庫中的數(shù)據(jù)(use數(shù)據(jù)庫中的 用戶)

mysql> drop  user "sanlang"@"10.0.0.8";

mysql> drop  user ""@"10.0.0.8";

或者:

mysql>delete from mysql.user where user="sanlang" and  host="110.0.0.8";

10.查看當前的用戶

mysql> select user();

11.查看當前所在的數(shù)據(jù)庫

mysql> select database();

12.處理完用戶要執(zhí)行的命令

mysql> flush  privileges;

13.導出數(shù)據(jù)庫

[root@web01 ]#mysqldump  -uroot  -poldboy123  wordexpress -B|gzip >bak.sql.gz

14.把數(shù)據(jù)庫腳本導入

先把備份解壓,再導入 gunzip  2016-06-19.sql.gz 

[root@web01 ]#mysql  -uroot  -poldboy123</tmp/bak.sql

 此處有個坑,備份的目錄不存在,數(shù)據(jù)將不會備份,即使看起來是成功的,備份檢查

15.對數(shù)據(jù)庫授權

在數(shù)據(jù)庫中授權

mysql> grant all on wordpress.* to wordpress@'172.16.1.%' identified by '123456'; 


16.遠程連接數(shù)據(jù)庫

[root@web01 ~]# mysql -ubbs  -p123456  -h 172.16.1.51


17.登錄多實例數(shù)據(jù)庫

mysql -uroot  -poldboy123  -h 10.0.0.7  -P3306


18.多實例數(shù)初始化密碼

[root@oldboy scripts]# mysqladmin -uroot password oldboy789 -S /data/3308/mysql.sock 


19.mysql命令總結

 -p, --password[=name] 

-S  指定socket

-P  指定端口

-h  指定遠程的IP

20.查看幫助

 help  creat  database;

21查看字符集校對規(guī)則

mysql> show character set;

22.創(chuàng)建GBK字符集的數(shù)據(jù)庫oldboy,并查看建立數(shù)據(jù)庫的完整語句

mysql> create  database  oldboy  CHARACTER SET  gbk  COLLATE gbk_chinese_ci;

mysql> show create database  oldboy;

23.查看版本

mysql> select  version();

24模糊查詢

mysql> show databases  like '%my%';

25.增加system并提升Wie超級管理員,即和root等價的管理員用戶,只是名字不同而已

mysql>grant all privileges on *.*  to  system@'localhost'  identified  by 'oldboy123'  with grant option;

這個system用戶不需要提前增加,默認會給加上去

26多實例用戶修改密碼

[root@oldboy 3306]# mysqladmin -usystem -p3306    password  oldboy123  -S  /data/3306/mysql.sock

27更新密碼

mysql> UPDATE  mysql.user  SET  password=PASSWORD("123456")  WHERE user='root' and host='localhost'; 

更新后

mysql> flush  privileges;

28.查看創(chuàng)建庫的命令

mysql> show create  database  oldboy;

29.like語句

mysql> show databases  like 'oldboy%';

mysql> show databases  like '%oldboy%';

30.刪除數(shù)據(jù)庫

mysql> drop  database  oldboy

31.不跳出mysql執(zhí)行l(wèi)inux命令行的命令

mysql> system  cd  /root 

mysql> system  ls 

anaconda-ks.cfg  install.log  install.log.syslog

mysql> 

32.查看當前處于的數(shù)據(jù)庫

mysql> select database();

33.查看當前系統(tǒng)時間

mysql> select  now();

34.創(chuàng)建用戶,并授權(默認沒有權限)

第一種授權

mysql> create user  oldboy@'localhost'  identified  by 'oldboy123';

mysql> grant all  on oldboy.*  to oldboy@'localhost';

第二種授權

mysql> grant all  on  oldboyutf8.*  to oldboy@'localhost'  identified  by  'oldboy123';  #此處用戶不存也可以

第三種授權(示例)

mysql> grant all  on  oldboyutf8.*  to oldboy@'10.0.0.%'  identified  by  'oldboy123';

mysql> grant all  on  oldboyutf8.*  to oldboy@'10.0.0.0/255.255.255.0'  identified  by  'oldboy123';  #不能用24,否則不生效

mysql> create user  oldboy@'localhost'  identified  by 'oldboy123';

mysql> grant all  on oldboy.*  to oldboy@'10.0.0.%';

35.查看用戶的權限

mysql> show grants for  oldboy@'localhost';

36.回收權限 

mysql> REVOKE INSERT ON oldboy.* FROM 'oldboy'@'localhost';

回收oldboy用戶對oldboy表的insert權限

37.查看用戶具體權限信息

mysql> select  * from   mysql.user \G;

39.查看表的創(chuàng)建

mysql> show create table  student;

40.查看數(shù)據(jù)庫表的結構

mysql> desc student;

41.查看表的列

mysql> show columns  from     student;

42.刪除主鍵

mysql> alter  table  student  drop  primary  key;  反思:自增的主鍵刪不掉

mysql> alter  table  student add  primary  key(id);

42.遠程連接

mysql -uwordpress -poldboy123 -h 172.16.1.51 -P3306

43.查看用戶所擁有的具體權限

mysql> select * from mysql.db where user='wordpress'\G    

44.創(chuàng)建主鍵和索引

mysql> create  table  student(

    -> id int(4)  not null auto_increment,

    -> name char(20)  not  null,

    -> age tinyint(3)  not  null default '0',

    -> primary  key(id),

    -> key  index_name(name)

    -> )

    -> ;

Query OK, 0 rows affected (0.09 sec)

mysql> 

45.查看索引的命名;

mysql> desc  student

mysql> show index from  student;

mysql> show index from  student \G;  看的比較清楚

46.刪除索引

查看索引的名字:               

mysql> show index from  student;

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

| Table   | Non_unique | Key_name   | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |

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

| student |          0 | PRIMARY    |            1 | id          | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |

| student |          1 | index_name |            1 | name        | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |

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

2 rows in set (0.00 sec)


mysql> 

mysql> alter  table   student drop index index_name;

47.給表添加索引(生產場景晚上建立索引)

mysql> alter table student  add index index_name(name);

mysql> create index index_age  on student(age); 

49.以一個表的一個字段的前n個字符創(chuàng)建索引

mysql> create index index_age  on student(age(n)); 

mysql> alter  table  student add  index    index_age(age(1));

50.給表的多個字段創(chuàng)建聯(lián)合索引(條件越多,符合條件越少)

mysql> alter   table  stuent add  index   index_name_age(name(3),age(2));

mysql> create  index index_name_age  on student(name,age);  

mysql> create  index index_name_age  on student(name(3),age(2));  #字段的前幾個字符創(chuàng)建聯(lián)合索引

51.對用戶進行去重(或者說對特定的一列進行去重)

mysql> select count(distinct user)  from mysql.user;

52.創(chuàng)建唯一索引

mysql>create  uniq  index  index_id_name  on  student(name);

53.插入數(shù)據(jù)的操作

mysql> insert into student(id,name,age)  values(1,"xiaoming",10);

54.批量插入數(shù)據(jù)

mysql> insert  into grade(name,age) values("xiaoli",90),("sanlang",93);

55.創(chuàng)建一個表test,指定其引擎為innodb  字符集為gbk

mysql> create  table  test( id int(4), name  varchar(10) ) engine=innodb   charset=gbk; 

mysql> alter table test  ENGINE=MYISAM;  #修改表的引擎

56.更新表中的字段

mysql> update test set name="oldgirl"  where id=1;

57.已知表的后邊你插入字段

mysql> alter  table test add age int(2)  after name ;

58.刪除字段

mysql> alter table test drop  age  ;     #刪除age字段

59.刪除表中的所有數(shù)據(jù)

mysql> truncate  test;  #刪除數(shù)據(jù)文件,物理刪除,比較快

mysql>  delete  from test; #邏輯刪除,sql語句刪除比較慢

60.查詢前兩行數(shù)據(jù)

mysql> select * from test  limit 2;  #查詢表的前兩行

mysql> select * from test  limit 1,2;  #從第一個之后查詢兩個,不包括第一個 

61.查詢數(shù)據(jù)并排序

mysql> select * from   test   order  by  id desc;

62.修改字段的類型

mysql>alter  table  test  modify age  char(4) after name;

63.修改字段的名稱

mysql>alter  table  test  change age  oldboyage  char(4) after name;  #直接更改字段名稱以及字段類型

64.修改表名稱

mysql>rename  table   oldtable  to newtable

65.復制表的結構,創(chuàng)建數(shù)據(jù)庫的時候

mysql>create  table grade like sanlang ;

65.復制表的結構和數(shù)據(jù)在創(chuàng)建數(shù)據(jù)的時候

mysql>create  table  student select * from sanlang ;

66.非交互式操作mysql數(shù)據(jù)庫

[root@oldboy oldboy]# mysql -uroot -poldboy123 -S  /data/3306/mysql.sock   -e "show variables like '%log_bin%' ;"  #bin_log日志

[root@oldboy oldboy]# mysql -uroot -poldboy123 -S  /data/3306/mysql.sock   -e "show variables like '%slow%' ;"     #慢查詢是否開啟

68.不重啟數(shù)據(jù)庫,修改數(shù)據(jù)庫的配置文件

以后不重啟修改數(shù)據(jù)的配置文件,都這么干

[root@oldboy ~]# grep key_buffer  /data/3306/my.cnf   

key_buffer_size = 16M

[root@oldboy ~]# set  global key_buffer_size = 1024*1024*32    #臨時生效,重啟mysql失效  

vim  /data/3306/my.cnf                                                                

key_buffer_size = 32M

[root@oldboy ~]# sed -i ‘s#key_buffer_size = 16M#key_buffer_size = 32M#g’/data/3306/my.cnf   

(沒有的話加進去,手動加進去)

69.殺死數(shù)據(jù)sql語句的進程

kill   ID

70.查看當前會how  global status話的數(shù)據(jù)庫狀態(tài)信息

mysql> show session  status;

71.查看整個數(shù)據(jù)庫的運行狀態(tài)信息,很重要,做分析并做好監(jiān)控

mysql> show  global status ; #可以查看數(shù)據(jù)庫select,update,。。 多少次

mysql> show  global  status  like "%select%";  #查看這些命令執(zhí)行的次數(shù),可以按天處理,今天減去昨天的就是今天的

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

| Variable_name          | Value |

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

| Com_insert_select      | 21    |  #插入的次數(shù)

| Com_replace_select     | 0     |

| Com_select             | 26    |  #查詢的次數(shù),不同的查詢會增減,同一條命令不會增加

| Select_full_join       | 0     |

| Select_full_range_join | 0     |

| Select_range           | 0     |

| Select_range_check     | 0     |

| Select_scan            | 58    | 

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

8 rows in set (0.00 sec)


mysql> 

72.查看innodb引擎的性能狀態(tài)

show engine  innodb status

74.查看標的列

mysql> show COLUMNS  from  test;

75.修改主鍵

alter table student change id id int primary key auto_increment; 


76.將序排序和升序排序

mysql> select * from  test  order by  id desc;  #將序排序

mysql> select * from  test  order by  id asc;    #升序排序

77.導出數(shù)據(jù)文件

mysql> select * from  test  order by  id asc  into  outfile '/tmp/file.txt';

78.批量插入數(shù)據(jù)

mysql> insert  into test(name,age)  values("xiaoming",18),("lili",20)

79.創(chuàng)建唯一索引

mysql>create  uniqe index  index_ind_name  on  student(name)


反思:數(shù)據(jù)庫的停止 不要用kill  -9,否則起不來,后果很嚴重,創(chuàng)建數(shù)據(jù)庫的時候最好不要有特殊字符

當前名稱:mysql常用命令總結
URL鏈接:http://muchs.cn/article34/gdipse.html

成都網站建設公司_創(chuàng)新互聯(lián),為您提供網站收錄、品牌網站制作定制網站、建站公司、品牌網站建設網站建設

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

商城網站建設