1. 程式人生 > >MySQL數據庫的基本操作命令

MySQL數據庫的基本操作命令

驗證 用戶授權 數據庫 沒有 all 所有權 用戶名 mysql數據庫 sql數據庫

MySql中添加用戶,新建數據庫,用戶授權,刪除用戶,修改密碼(註意每行後邊都跟個;表示一個命令語句結束):
1.新建用戶
登錄MYSQL:
  @>mysql -u root -p
  @>密碼
創建用戶:
  mysql> insert into mysql.user(Host,User,Password) values("localhost","test",password("1234"));
  這樣就創建了一個名為:test 密碼為:1234 的用戶。
  註意:此處的"localhost",是指該用戶只能在本地登錄,不能在另外一臺機器上遠程登錄。如果想遠程登錄的話,將"localhost"改為"%",表示在任何一臺電腦上都可以登錄。也可以指定某臺機器可以遠程登錄。


然後登錄一下:
  mysql>exit;
  @>mysql -u test -p
  @>輸入密碼
  mysql>登錄成功
2.為用戶授權
  授權格式:grant 權限 on 數據庫.* to 用戶名@登錄主機 identified by "密碼"; 
登錄MYSQL(有ROOT權限),這裏以ROOT身份登錄:
  @>mysql -u root -p
  @>密碼
首先為用戶創建一個數據庫(testDB):
  mysql>create database testDB;
授權test用戶擁有testDB數據庫的所有權限(某個數據庫的所有權限):
   mysql>grant all privileges on testDB.* to test@localhost identified by ‘1234‘;

  mysql>flush privileges;//刷新系統權限表
  格式:grant 權限 on 數據庫.* to 用戶名@登錄主機 identified by "密碼"; 
如果想指定部分權限給一用戶,可以這樣來寫:
  mysql>grant select,update on testDB.* to test@localhost identified by ‘1234‘;
  mysql>flush privileges; //刷新系統權限表
授權test用戶擁有所有數據庫的某些權限:  
  mysql>grant select,delete,update,create,drop on *.* to test@"%" identified by "1234";

//test用戶對所有數據庫都有select,delete,update,create,drop 權限。
  //@"%" 表示對所有非本地主機授權,不包括localhost。(localhost地址設為127.0.0.1,如果設為真實的本地地址,不知道是否可以,沒有驗證。)
//對localhost授權:加上一句grant all privileges on testDB.* to test@localhost identified by ‘1234‘;即可。
3.刪除用戶
@>mysql -u root -p
@>密碼
mysql>Delete FROM user Where User=‘test‘ and Host=‘localhost‘;
mysql>flush privileges;
mysql>drop database testDB; //刪除用戶的數據庫
刪除賬戶及權限:>drop user 用戶名@‘%‘;
        >drop user 用戶名@ localhost;
4.修改指定用戶密碼
@>mysql -u root -p
@>密碼
mysql>update mysql.user set password=password(‘新密碼‘) where User="test" and Host="localhost";
mysql>flush privileges;
5.列出所有數據庫
mysql>show database;
6.切換數據庫
mysql>use ‘數據庫名‘;
7.列出所有表
mysql>show tables;
8.顯示數據表結構
mysql>describe 表名;
9.刪除數據庫和數據表
mysql>drop database 數據庫名;
mysql>drop table 數據表名;

MySQL數據庫的基本操作命令