MySQL資料庫常用命令

來源:互聯網
上載者:User

  1、MySQL常用命令

  create database name; 建立資料庫

  use databasename; 選擇資料庫

  drop database name 直接刪除資料庫,不提醒

  show tables; 顯示表

  describe tablename; 表的詳細描述

  select 中加上distinct去除重複欄位

  mysqladmin drop databasename 刪除資料庫前,有提示。

  顯示當前mysql版本和當前日期

  select version(),current_date;

  2、修改mysql中root的密碼:

  shell>mysql -u root -p

  mysql> update user set password=password(”xueok654123″) where user=’root’;

  mysql> flush privileges //重新整理資料庫

  mysql>use dbname; 開啟資料庫:

  mysql>show databases; 顯示所有資料庫

  mysql>show tables; 顯示資料庫mysql中所有的表:先use mysql;然後

  mysql>describe user; 顯示表mysql資料庫中user表的列資訊);

  3、grant

  建立一個可以從任何地方串連伺服器的一個完全的超級使用者,但是必須使用一個口令something做這個

  mysql> grant all privileges on *.* to user@localhost identified by ’something’ with

  增加新使用者

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

  GRANT ALL PRIVILEGES ON *.* TO monty@localhost IDENTIFIED BY ’something’ WITH GRANT OPTION;

  GRANT ALL PRIVILEGES ON *.* TO monty@”%” IDENTIFIED BY ’something’ WITH GRANT OPTION;

  刪除授權:

  mysql> revoke all privileges on *.* from root@”%”;

  mysql> delete from user where user=”root” and host=”%”;

  mysql> flush privileges;

  建立一個使用者custom在特定用戶端it363.com登入,可訪問特定資料庫fangchandb

  mysql >grant select, insert, update, delete, create,drop on fangchandb.* to custom@ it363.com identified by ‘ passwd’

  重新命名表:

  mysql > alter table t1 rename t2;

  4、mysqldump

  備份資料庫

  shell> mysqldump -h host -u root -p dbname >dbname_backup.sql

  恢複資料庫

  shell> mysqladmin -h myhost -u root -p create dbname

  shell> mysqldump -h host -u root -p dbname < dbname_backup.sql

  如果只想卸出建表指令,則命令如下:

  shell> mysqladmin -u root -p -d databasename > a.sql

  如果只想卸出插入資料的sql命令,而不需要建表命令,則命令如下:

  shell> mysqladmin -u root -p -t databasename > a.sql

  那麼如果我只想要資料,而不想要什麼sql命令時,應該如何操作呢?

  mysqldump -T./ phptest driver

  其中,只有指定了-T參數才可以卸出純文字檔案,表示卸出資料的目錄,./表示目前的目錄,即與mysqldump同一目錄。如果不指定driver 表,則將卸出整個資料庫的資料。每個表會產生兩個檔案,一個為.sql檔案,包含建表執行。另一個為.txt檔案,只包含資料,且沒有sql指令。

  5、可將查詢儲存在一個檔案中並告訴mysql從檔案中讀取查詢而不是等待鍵盤輸入。可利用外殼程式鍵入重新導向公用程式來完成這項工作。例如,如果在檔案my_file.sql 中存放有查

  詢,可如下執行這些查詢:

  例如,如果您想將建表語句提前寫在sql.txt中:

  mysql > mysql -h myhost -u root -p database < sql.txt

        :更多精彩教程請關注三聯圖文教程 頻道,

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.