Linux下MySQL資料庫常用基本操作

來源:互聯網
上載者:User

標籤:ext   ase   class   create   存在   tables   logs   esc   ble   

1、顯示資料庫
 show databases;
2、選擇資料庫
use 資料庫名;
3、顯示資料庫中的表
show tables;
4、顯示資料表的結構 
describe 表名;
 5、顯示表中記錄 
SELECT * FROM 表名
 6、建庫
 create databse 庫名;
 7、建表
create table 表名 (欄位設定列表);mysql> create table name(    -> id int auto_increment not null primary key ,    -> uname char(8),    -> gender char(2),    -> birthday date );Query OK, 0 rows affected (0.03 sec)mysql> show tables;+------------------+| Tables_in_userdb |+------------------+| name             |+------------------+1 row in set (0.00 sec)mysql> describe name;+----------+---------+------+-----+---------+----------------+| Field    | Type    | Null | Key | Default | Extra          |+----------+---------+------+-----+---------+----------------+| id       | int(11) | NO   | PRI | NULL    | auto_increment || uname    | char(8) | YES  |     | NULL    |                || gender   | char(2) | YES  |     | NULL    |                || birthday | date    | YES  |     | NULL    |                |+----------+---------+------+-----+---------+----------------+4 rows in set (0.00 sec)註: auto_increment 自增     primary key    主鍵
 8、增加記錄
 insert into name(uname,gender,birthday) values(‘張三‘,‘男‘,‘1971-10-01‘);
 9、修改記錄
update name set birthday=‘1971-01-10‘ where uname=‘張三‘;
 10、刪除記錄
delete from name where uname=‘張三‘;
 11、刪除表
drop table 表名
 12、刪除庫
 drop database 庫名;
13、備份資料庫 
mysqldump -u root -p --opt 資料庫名>備份名; //進入到庫目錄
14、恢複
mysql -u root -p 資料庫名<備份名; //恢複時資料庫必須存在,可以為空白資料庫
 15、資料庫授權

  格式:grant select on 資料庫.* to 使用者名稱@登入主機 identified by "密碼"

例1、增加一個使用者user001密碼為123456,讓他可以在任何主機上登入,並對所有資料庫有查詢、插入、修改、刪除的許可權。首先用以root使用者連入MySQL,然後鍵入以下命令:

 mysql> grant select,insert,update,delete on *.* to [email protected]"%" Identified by "123456";

 

例2、增加一個使用者user002密碼為123456,讓此使用者只可以在localhost上登入,也可以設定指定IP,並可以對資料庫test進行查詢、插入、修改、刪除的操作 (localhost指本地主機,即MySQL資料庫所在的那台主機)

        //這樣使用者即使用知道user_2的密碼,他也無法從網上直接存取資料庫,只能通過MYSQL主機來操作test庫。
        //首先用以root使用者連入MySQL,然後鍵入以下命令:

  mysql>grant select,insert,update,delete on test.* to [email protected] identified by "123456";

 

Linux下MySQL資料庫常用基本操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.