MySql常用命令總結

2021-05-23 23:04:53 字數 2306 閱讀 9835

1:使用show語句找出在伺服器上當前存在什麼資料庫: 

mysql> show databases; 

2:2、建立乙個資料庫mysqldata

mysql> create database mysqldata; 

3:選擇你所建立的資料庫 

mysql> use mysqldata; (按回車鍵出現database changed 時說明操作成功!)

4:檢視現在的資料庫中存在什麼表 

mysql> show tables;

5:建立乙個資料庫表

mysql> create table mytable (name varchar(20), *** char(1));

6:顯示表的結構: 

mysql> describe mytable; 

7:往表中加入記錄 

mysql> insert into mytable values ("hyq","m"); 

8:用文字方式將資料裝入資料庫表中(例如d:/mysql.txt)

mysql> load data local infile "d:/mysql.txt" into table mytable;

9:匯入.sql檔案命令(例如d:/mysql.sql)

mysql>use database;

mysql>source d:/mysql.sql; 

10:刪除表

mysql>drop table mytable; 

11:清空表

mysql>delete from mytable;

12:更新表中資料

mysql>update mytable set ***="f" where name='hyq'; 

在windows中mysql以服務形式存在,在使用前應確保此服務已經啟動,未啟動可用net start mysql命令啟動。而linux中啟動時可用「/etc/rc.d/init.d/mysqld start"命令,注意啟動者應具有管理員許可權。

剛安裝好的mysql包含乙個含空密碼的root帳戶和乙個匿名帳戶,這是很大的安全隱患,對於一些重要的應用我們應將安全性盡可能提高,在這裡應把匿名帳戶刪除、 root帳戶設定密碼,可用如下命令進行:

use mysql;

delete from user where user="";

update user set password=password('newpassword') where user='root';

如果要對使用者所用的登入終端進行限制,可以更新user表中相應使用者的host欄位,在進行了以上更改後應重新啟動資料庫服務,此時登入時可用如下類似命令:

mysql -uroot -p;

mysql -uroot -pnewpassword;

mysql mydb -uroot -p;

mysql mydb -uroot -pnewpassword;

上面命令引數是常用引數的一部分,詳細情況可參考文件。此處的mydb是要登入的資料庫的名稱。

在進行開發和實際應用中,使用者不應該只用root使用者進行連線資料庫,雖然使用root使用者進行測試時很方便,但會給系統帶來重大安全隱患,也不利於管理技術的提高。我們給乙個應用中使用的使用者賦予最恰當的資料庫許可權。如乙個只進行資料插入的使用者不應賦予其刪除資料的許可權。mysql的使用者管理是通過user表來實現的,新增新使用者常用的方法有兩個,一是在user表插入相應的資料行,同時設定相應的許可權;二是通過grant命令建立具有某種許可權的使用者。其中grant的常用用法如下:

grant all on mydb.* to newusername@hostname identified by "password" ;

grant usage on *.* to newusername@hostname identified by "password";

grant select,insert,update on mydb.* to newusername@hostname identified by "password";

grant update,delete on mydb.testtable to newusername@hostname identified by "password";

若要給此使用者賦予他在相應物件上的許可權的管理能力,可在grant後面新增with grant option選項。而對於用插入user表新增的使用者,password欄位應用password 函式進行更新加密,以防不軌之人竊看密碼。對於那些已經不用的使用者應給予清除,許可權過界的使用者應及時**許可權,**許可權可以通過更新user表相應字段,也可以使用revoke操作。

mysql常用命令總結 mySql常用命令總結

總結一下自己常用的mysql資料庫的常用命令 mysql u root p 進入mysql bin目錄後執行,回車後輸入密碼連線。資料庫操作 1 create database dbname 建立資料庫,資料庫名為dbname 2 create database todo default chara...

mysql常用命令總結 Mysql 常用命令總結

mysql資料庫中的sql用法 檢視專案連線的mysql庫中的所有資料庫資訊 show databases 產看當前專案連線的資料庫資訊 select database 展示當前連線的資料庫的所有資料表資訊 show tables 查詢mysql的所有使用者資訊 select distinct co...

MySql常用命令總結

1 使用show語句找出在伺服器上當前存在什麼資料庫 mysql show databases 2 2 建立乙個資料庫mysqldata mysql create database mysqldata 3 選擇你所建立的資料庫 mysql use mysqldata 按回車鍵出現database c...