您好,登錄后才能下訂單哦!
1.登錄數(shù)據(jù)庫(kù)
[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ù)庫(kù)
mysql> show databases;
5.使用數(shù)據(jù)庫(kù)
mysql> use mysql;
Database changed
mysql>
6.查看數(shù)據(jù)庫(kù)的表
mysql> show tables;
7.查看數(shù)據(jù)表的結(jié)構(gòu)
mysql> describe user;
mysql> show columns from test;
8.從表中查找字段(此處查詢的字段不區(qū)分大小寫)
mysql> select Host,User from user; #直接查表
mysql> select host,user from mysql.user; #從哪個(gè)庫(kù)的哪一個(gè)表
9.刪除庫(kù)中的數(shù)據(jù)(use數(shù)據(jù)庫(kù)中的 用戶)
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.查看當(dāng)前的用戶
mysql> select user();
11.查看當(dāng)前所在的數(shù)據(jù)庫(kù)
mysql> select database();
12.處理完用戶要執(zhí)行的命令
mysql> flush privileges;
13.導(dǎo)出數(shù)據(jù)庫(kù)
[root@web01 ]#mysqldump -uroot -poldboy123 wordexpress -B|gzip >bak.sql.gz
14.把數(shù)據(jù)庫(kù)腳本導(dǎo)入
先把備份解壓,再導(dǎo)入 gunzip 2016-06-19.sql.gz
[root@web01 ]#mysql -uroot -poldboy123</tmp/bak.sql
此處有個(gè)坑,備份的目錄不存在,數(shù)據(jù)將不會(huì)備份,即使看起來(lái)是成功的,備份檢查
15.對(duì)數(shù)據(jù)庫(kù)授權(quán)
在數(shù)據(jù)庫(kù)中授權(quán)
mysql> grant all on wordpress.* to wordpress@'172.16.1.%' identified by '123456';
16.遠(yuǎn)程連接數(shù)據(jù)庫(kù)
[root@web01 ~]# mysql -ubbs -p123456 -h 172.16.1.51
17.登錄多實(shí)例數(shù)據(jù)庫(kù)
mysql -uroot -poldboy123 -h 10.0.0.7 -P3306
18.多實(shí)例數(shù)初始化密碼
[root@oldboy scripts]# mysqladmin -uroot password oldboy789 -S /data/3308/mysql.sock
19.mysql命令總結(jié)
-p, --password[=name]
-S 指定socket
-P 指定端口
-h 指定遠(yuǎn)程的IP
20.查看幫助
help creat database;
21查看字符集校對(duì)規(guī)則
mysql> show character set;
22.創(chuàng)建GBK字符集的數(shù)據(jù)庫(kù)oldboy,并查看建立數(shù)據(jù)庫(kù)的完整語(yǔ)句
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超級(jí)管理員,即和root等價(jià)的管理員用戶,只是名字不同而已
mysql>grant all privileges on *.* to system@'localhost' identified by 'oldboy123' with grant option;
這個(gè)system用戶不需要提前增加,默認(rèn)會(huì)給加上去
26多實(shí)例用戶修改密碼
[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)建庫(kù)的命令
mysql> show create database oldboy;
29.like語(yǔ)句
mysql> show databases like 'oldboy%';
mysql> show databases like '%oldboy%';
30.刪除數(shù)據(jù)庫(kù)
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.查看當(dāng)前處于的數(shù)據(jù)庫(kù)
mysql> select database();
33.查看當(dāng)前系統(tǒng)時(shí)間
mysql> select now();
34.創(chuàng)建用戶,并授權(quán)(默認(rèn)沒有權(quán)限)
第一種授權(quán)
mysql> create user oldboy@'localhost' identified by 'oldboy123';
mysql> grant all on oldboy.* to oldboy@'localhost';
第二種授權(quán)
mysql> grant all on oldboyutf8.* to oldboy@'localhost' identified by 'oldboy123'; #此處用戶不存也可以
第三種授權(quán)(示例)
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.查看用戶的權(quán)限
mysql> show grants for oldboy@'localhost';
36.回收權(quán)限
mysql> REVOKE INSERT ON oldboy.* FROM 'oldboy'@'localhost';
回收oldboy用戶對(duì)oldboy表的insert權(quán)限
37.查看用戶具體權(quán)限信息
mysql> select * from mysql.user \G;
39.查看表的創(chuàng)建
mysql> show create table student;
40.查看數(shù)據(jù)庫(kù)表的結(jié)構(gòu)
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.遠(yuǎn)程連接
mysql -uwordpress -poldboy123 -h 172.16.1.51 -P3306
43.查看用戶所擁有的具體權(quán)限
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.給表添加索引(生產(chǎn)場(chǎng)景晚上建立索引)
mysql> alter table student add index index_name(name);
mysql> create index index_age on student(age);
49.以一個(gè)表的一個(gè)字段的前n個(gè)字符創(chuàng)建索引
mysql> create index index_age on student(age(n));
mysql> alter table student add index index_age(age(1));
50.給表的多個(gè)字段創(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)); #字段的前幾個(gè)字符創(chuàng)建聯(lián)合索引
51.對(duì)用戶進(jìn)行去重(或者說(shuō)對(duì)特定的一列進(jìn)行去重)
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)建一個(gè)表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語(yǔ)句刪除比較慢
60.查詢前兩行數(shù)據(jù)
mysql> select * from test limit 2; #查詢表的前兩行
mysql> select * from test limit 1,2; #從第一個(gè)之后查詢兩個(gè),不包括第一個(gè)
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.復(fù)制表的結(jié)構(gòu),創(chuàng)建數(shù)據(jù)庫(kù)的時(shí)候
mysql>create table grade like sanlang ;
65.復(fù)制表的結(jié)構(gòu)和數(shù)據(jù)在創(chuàng)建數(shù)據(jù)的時(shí)候
mysql>create table student select * from sanlang ;
66.非交互式操作mysql數(shù)據(jù)庫(kù)服務(wù)器
單條命令
[root@oldboy ~]# mysql -uroot -poldboy123 -S /data/3306/mysql.sock -e "show databases;"
多條命令:
[root@oldboy ~]# mysql -uroot -poldboy123 -S /data/3306/mysql.sock -e "use backup;show tables;"
67.查看mysql的配置的命令
mysql> show variables;
[root@oldboy oldboy]# mysql -uroot -poldboy123 -S /data/3306/mysql.sock -e "show variables like \"server_id\" ;"
[root@oldboy oldboy]# mysql -uroot -poldboy123 -S /data/3306/mysql.sock -e "show variables like 'server_id' ;" #查看server_id
[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ù)庫(kù),修改數(shù)據(jù)庫(kù)的配置文件
以后不重啟修改數(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 #臨時(shí)生效,重啟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
(沒有的話加進(jìn)去,手動(dòng)加進(jìn)去)
69.殺死數(shù)據(jù)sql語(yǔ)句的進(jìn)程
kill ID
70.查看當(dāng)前會(huì)how global status話的數(shù)據(jù)庫(kù)狀態(tài)信息
mysql> show session status;
71.查看整個(gè)數(shù)據(jù)庫(kù)的運(yùn)行狀態(tài)信息,很重要,做分析并做好監(jiān)控
mysql> show global status ; #可以查看數(shù)據(jù)庫(kù)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ù),不同的查詢會(huì)增減,同一條命令不會(huì)增加
| 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.查看標(biāo)的列
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.導(dǎo)出數(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ù)庫(kù)的停止 不要用kill -9,否則起不來(lái),后果很嚴(yán)重,創(chuàng)建數(shù)據(jù)庫(kù)的時(shí)候最好不要有特殊字符
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。