Linux運(yùn)維-day49-MySQL數(shù)據(jù)庫的基本操作整理

參考:https://blog.csdn.net/len_yue_mo_fu/article/details/79153870

進(jìn)入數(shù)據(jù)庫:命令行直接寫mysql痰滋,回車即可
進(jìn)入指定數(shù)據(jù)庫:mysql -uwordpress -p wordpress
退出數(shù)據(jù)庫:Ctrl+d

一、創(chuàng)建操作

1.1 創(chuàng)建數(shù)據(jù)庫

create database test;

MariaDB [(none)]> create database test;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| lianxi             |
| mysql              |
| performance_schema |
| test               |
| wordpress          |
+--------------------+
6 rows in set (0.00 sec)
MariaDB [(none)]>

1.2 創(chuàng)建用戶

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

1.3 創(chuàng)建表和字段

create table tl_user(id int primary key auto_increment,name nvarchar(20),age int,sex varchar(4),address nvarchar(50),updatetime date);

MariaDB [test]> create table tl_user(id int primary key auto_increment,name nvarchar(20),age int,sex varchar(4),address nvarchar(50),updatetime date);
Query OK, 0 rows affected (0.00 sec)

MariaDB [test]> show tables from test;
+----------------+
| Tables_in_test |
+----------------+
| tl_user        |
+----------------+
1 row in set (0.00 sec)

MariaDB [test]> 

1.4 向表中插入數(shù)據(jù)

insert into 數(shù)據(jù)庫名.表名(表字段,……) values(對應(yīng)字段的值,……)

如:insert into test.tl_user(name,age,sex,address,updatetime) values('張三',23,'男','北京朝陽區(qū)某某號','2019-6-11');

MariaDB [test]> insert into tl_user(name,age,sex,address,updatetime) values('張三',23,'男','北京朝陽區(qū)某某號','2019-6-11');
Query OK, 1 row affected, 1 warning (0.00 sec)

MariaDB [test]> select * from tl_user\G
*************************** 1. row ***************************
        id: 1
      name: 張三
       age: 23
       sex: ?
   address: 北京朝陽區(qū)某某號
updatetime: 2019-06-11
1 row in set (0.00 sec)

MariaDB [test]> 

二、刪除操作

2.1 刪除數(shù)據(jù)庫

drop database 數(shù)據(jù)庫名;
如:drop database test;

MariaDB [(none)]> drop database test;
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| lianxi             |
| mysql              |
| performance_schema |
| wordpress          |
+--------------------+
5 rows in set (0.00 sec)
MariaDB [(none)]> 

2.2 刪除用戶

drop user 'oldboy'@'localhost'
用戶刪除之后:flush privileges

2.3 刪除表

drop table 數(shù)據(jù)庫名.表名;

2.4 刪除表中的某條數(shù)據(jù)

delete from 數(shù)據(jù)庫名. 表名 where 要?jiǎng)h除的條件

如:delete from test.tl_user where id=2;

MariaDB [test]> select * from tl_user;
+----+--------+------+------+--------------------------+------------+
| id | name   | age  | sex  | address                  | updatetime |
+----+--------+------+------+--------------------------+------------+
|  1 | 張三   |   23 | ?    | 北京朝陽區(qū)某某號         | 2019-06-11 |
|  2 | dddd   |   23 | nv   | dfdfdfdfdfd              | 2019-06-11 |
|  3 | 李四   |   23 | nv   | 的輔導(dǎo)輔導(dǎo)輔導(dǎo)費(fèi)         | 2019-06-11 |
+----+--------+------+------+--------------------------+------------+
3 rows in set (0.00 sec)

MariaDB [test]> delete from tl_user where id=2;
Query OK, 1 row affected (0.00 sec)

MariaDB [test]> select * from tl_user;
+----+--------+------+------+--------------------------+------------+
| id | name   | age  | sex  | address                  | updatetime |
+----+--------+------+------+--------------------------+------------+
|  1 | 張三   |   23 | ?    | 北京朝陽區(qū)某某號         | 2019-06-11 |
|  3 | 李四   |   23 | nv   | 的輔導(dǎo)輔導(dǎo)輔導(dǎo)費(fèi)         | 2019-06-11 |
+----+--------+------+------+--------------------------+------------+
2 rows in set (0.00 sec)

MariaDB [test]> 

三、查詢操作

3.1 查詢數(shù)據(jù)庫

show databases;

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)
MariaDB [(none)]> 

3.2 查詢數(shù)據(jù)庫中的用戶(指定字段)

select user,host from mysql.user;

MariaDB [(none)]> select user,host from mysql.user;
+------+-----------+
| user | host      |
+------+-----------+
| root | 127.0.0.1 |
| root | ::1       |
|      | localhost |
| root | localhost |
|      | web01     |
| root | web01     |
+------+-----------+
6 rows in set (0.00 sec)

3.3 查詢數(shù)據(jù)庫中的用戶(所有字段)

select * from mysql.user;

為了顯示好看可以縱向顯示,直接在語句后面加\G,如:select * from mysql.user\G

3.4 顯示當(dāng)前所用的用戶

select user();

3.5 使用數(shù)據(jù)庫(進(jìn)入指定數(shù)據(jù)庫)

use 數(shù)據(jù)庫名;如:use mysql;

MariaDB [(none)]> use wordpress;
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
MariaDB [wordpress]> 

3.6 顯示當(dāng)前使用的數(shù)據(jù)庫

select database();

MariaDB [wordpress]> select database();
+------------+
| database() |
+------------+
| wordpress  |
+------------+
1 row in set (0.00 sec)

MariaDB [wordpress]> 

3.7 只顯示1條數(shù)據(jù)

select * from mysql.user limit 1 ;
select * from mysql.user limit 1\G

MariaDB [wordpress]> select * from wp_posts limit 1 \G
*************************** 1. row ***************************
                   ID: 1
          post_author: 1
            post_date: 2019-06-10 09:30:38
        post_date_gmt: 2019-06-10 09:30:38
         post_content: <!-- wp:paragraph -->
<p>Welcome to WordPress. This is your first post. Edit or delete it, then start writing!</p>
<!-- /wp:paragraph -->
       ……
MariaDB [wordpress]> 

四节值、更新操作

4.1 根據(jù)條件更新表中的某些字段的值

update 數(shù)據(jù)庫名.表名 set 要更新的字段=值 where 根據(jù)什么條件;

如:更新tl_user表中張三的地址
update test.tl_user set address='黑龍江省齊齊哈爾市某某號' where id=1;

MariaDB [test]> select * from tl_user;
+----+--------+------+------+--------------------------+------------+
| id | name   | age  | sex  | address                  | updatetime |
+----+--------+------+------+--------------------------+------------+
|  1 | 張三   |   23 | ?    | 北京朝陽區(qū)某某號         | 2019-06-11 |
|  3 | 李四   |   23 | nv   | 的輔導(dǎo)輔導(dǎo)輔導(dǎo)費(fèi)         | 2019-06-11 |
+----+--------+------+------+--------------------------+------------+
2 rows in set (0.00 sec)

MariaDB [test]> update tl_user set address='黑龍江省齊齊哈爾市某某號' where id=1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MariaDB [test]> select * from tl_user;
+----+--------+------+------+--------------------------------------+------------+
| id | name   | age  | sex  | address                              | updatetime |
+----+--------+------+------+--------------------------------------+------------+
|  1 | 張三   |   23 | ?    | 黑龍江省齊齊哈爾市某某號             | 2019-06-11 |
|  3 | 李四   |   23 | nv   | 的輔導(dǎo)輔導(dǎo)輔導(dǎo)費(fèi)                     | 2019-06-11 |
+----+--------+------+------+--------------------------------------+------------+
2 rows in set (0.00 sec)

MariaDB [test]> 

五、備份操作

1>導(dǎo)出所有的數(shù)據(jù)庫

mysqldump -uroot -p -all-database >/root/all.sql
mysqldump -uroot -p -A >/root/all.sql

五榜聂、恢復(fù)數(shù)據(jù)庫

恢復(fù)所有數(shù)據(jù)庫:mysql -uroot -p </root/all.sql
給指定的數(shù)據(jù)庫恢復(fù)表:如:mysql -uzabbix -p123456 zabbixdb(數(shù)據(jù)庫名) <schema.sql

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末搞疗,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子峻汉,更是在濱河造成了極大的恐慌贴汪,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,907評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件休吠,死亡現(xiàn)場離奇詭異扳埂,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)瘤礁,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,987評論 3 395
  • 文/潘曉璐 我一進(jìn)店門阳懂,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人柜思,你說我怎么就攤上這事岩调。” “怎么了赡盘?”我有些...
    開封第一講書人閱讀 164,298評論 0 354
  • 文/不壞的土叔 我叫張陵号枕,是天一觀的道長。 經(jīng)常有香客問我陨享,道長葱淳,這世上最難降的妖魔是什么钝腺? 我笑而不...
    開封第一講書人閱讀 58,586評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮赞厕,結(jié)果婚禮上艳狐,老公的妹妹穿的比我還像新娘。我一直安慰自己皿桑,他們只是感情好毫目,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,633評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著诲侮,像睡著了一般镀虐。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上浆西,一...
    開封第一講書人閱讀 51,488評論 1 302
  • 那天粉私,我揣著相機(jī)與錄音,去河邊找鬼近零。 笑死,一個(gè)胖子當(dāng)著我的面吹牛抄肖,可吹牛的內(nèi)容都是我干的久信。 我是一名探鬼主播,決...
    沈念sama閱讀 40,275評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼漓摩,長吁一口氣:“原來是場噩夢啊……” “哼裙士!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起管毙,我...
    開封第一講書人閱讀 39,176評論 0 276
  • 序言:老撾萬榮一對情侶失蹤腿椎,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后夭咬,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體啃炸,經(jīng)...
    沈念sama閱讀 45,619評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,819評論 3 336
  • 正文 我和宋清朗相戀三年卓舵,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了南用。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,932評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡掏湾,死狀恐怖裹虫,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情融击,我是刑警寧澤筑公,帶...
    沈念sama閱讀 35,655評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站尊浪,受9級特大地震影響匣屡,放射性物質(zhì)發(fā)生泄漏封救。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,265評論 3 329
  • 文/蒙蒙 一耸采、第九天 我趴在偏房一處隱蔽的房頂上張望兴泥。 院中可真熱鬧,春花似錦虾宇、人聲如沸搓彻。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,871評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽旭贬。三九已至,卻和暖如春搪泳,著一層夾襖步出監(jiān)牢的瞬間稀轨,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,994評論 1 269
  • 我被黑心中介騙來泰國打工岸军, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留奋刽,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,095評論 3 370
  • 正文 我出身青樓艰赞,卻偏偏與公主長得像佣谐,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子方妖,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,884評論 2 354