1.CREATEUSER 語(yǔ)法: CREATEUSER ' username'@'host 'IDENTIFIEDBY'password'; 例子:CREATEUSER'dog'@'localhost'IDENTIFIEDBY'123456'; CREATEUSER'pig'@'192.168.1.101_'IDENDIFIEDBY'123456'; CREATEUSER'pig'@'%'IDENTIFIEDBY'123456'; CREATEUSER'pig'@
1. CREATE USER
語(yǔ)法:
CREATE USER 'username'@'host' IDENTIFIED BY 'password';
例子: CREATE USER 'dog'@'localhost' IDENTIFIED BY '123456';
CREATE USER 'pig'@'192.168.1.101_' IDENDIFIED BY '123456';
CREATE USER 'pig'@'%' IDENTIFIED BY '123456';
CREATE USER 'pig'@'%' IDENTIFIED BY '';
CREATE USER 'pig'@'%';
實(shí)例1:
mysql> create user jss;
這樣創(chuàng)建的用戶,可以從任意安裝了mysql客戶端,并能夠訪問(wèn)目標(biāo)服務(wù)器的機(jī)器上創(chuàng)建連接,無(wú)須密碼.例如,從ip:10.0.0.99的客戶端執(zhí)行連接:
mysql -ujss -h 172.16.1.110
查看該用戶:
mysql> select user,host,password from user where user='jss';
SELECT USER(); //顯示當(dāng)前用戶
實(shí)例2:
mysql> create user jss_ps identified by 'jss';
用戶連接時(shí),必須指定密碼,那就可以在創(chuàng)建用戶時(shí),通過(guò)指定identified by子句來(lái)設(shè)定密碼
用密碼登陸:
mysql -ujss_ps -p -h 172.16.1.110
如果希望指定的用戶只能從某臺(tái)指定的域(domain)或主機(jī)訪問(wèn),可以在創(chuàng)建用戶時(shí)指定host,例如,指定用戶只能從10.0.0.99訪問(wèn)
mysql> create user jss_ip@10.0.0.99 identified by password '123456';
2. 使用GRANT語(yǔ)句
語(yǔ)法:mysql> grant 權(quán)限1,權(quán)限2,...權(quán)限n on 數(shù)據(jù)庫(kù)名稱.表名稱 to 用戶名@用戶地址 identified by '連接口令';
權(quán)限1,權(quán)限2,...權(quán)限n代表
select,insert,update,delete,create,drop,index,alter,grant,references,reload,shutdown,process,file等14個(gè)權(quán)限
實(shí)例:
mysql>grant select,insert,update,delete,create,drop on vtdc.employee to joe@10.163.225.87 identified by '123';
給來(lái)自10.163.225.87的用戶joe分配可對(duì)數(shù)據(jù)庫(kù)vtdc的employee表進(jìn)行select,insert,update,delete,create,drop等操作的權(quán)限,并設(shè)定口令為123。
mysql>grant all privileges on vtdc.* to joe@10.163.225.87 identified by '123';
給來(lái)自10.163.225.87的用戶joe分配可對(duì)數(shù)據(jù)庫(kù)vtdc所有表進(jìn)行所有操作的權(quán)限,并設(shè)定口令為123。
mysql>grant all privileges on *.* to joe@10.163.225.87 identified by '123';
給來(lái)自10.163.225.87的用戶joe分配可對(duì)所有數(shù)據(jù)庫(kù)的所有表進(jìn)行所有操作的權(quán)限,并設(shè)定口令為123。
mysql>grant all privileges on *.* to joe@localhost identified by '123';
給本機(jī)用戶joe分配可對(duì)所有數(shù)據(jù)庫(kù)的所有表進(jìn)行所有操作的權(quán)限,并設(shè)定口令為123。
3. 直接向mysql.user表插入記錄:
mysql> insert into user (host,user,password) values ('%','jss_insert',password('jss'));
mysql>flush privileges; //刷新系統(tǒng)權(quán)限表
4. 修改mysql用戶密碼方式:
a. 使用mysqladmin語(yǔ)法:mysqladmin -u用戶名 -p舊密碼 password 新密碼
例如:mysqladmin -u root -p 123 password 456;
b. 直接修改user表的用戶口令:
語(yǔ)法:update mysql.user set password=password('新密碼') where User="phplamp" and Host="localhost";
實(shí)例:update user set password=password('54netseek') where user='root';
flush privileges;
c. 使用SET PASSWORD語(yǔ)句修改密碼:語(yǔ)法:
SET PASSWORD FOR 'username'@'host' = PASSWORD('newpassword');
如果是當(dāng)前登陸用戶用SET PASSWORD = PASSWORD("newpassword");
實(shí)例:
set password for root@localhost=password('');
SET PASSWORD FOR name=PASSWORD('new password');
SET PASSWORD FOR 'pig'@'%' = PASSWORD("123456");
5. 刪除用戶和撤銷權(quán)限:
a. 取消一個(gè)賬戶和其權(quán)限
Drop USER user;
drop user username@'%'
drop user username@localhost
b. 取消授權(quán)用戶:
語(yǔ)法:REVOKE privilege ON databasename.tablename FROM 'username'@'host';
例子: REVOKE SELECT ON *.* FROM 'pig'@'%';
REVOKE SELECT ON test.user FROM 'pig'@'%';
revoke all on *.* from sss@localhost ;
revoke all on user.* from 'admin'@'%';
SHOW GRANTS FOR 'pig'@'%'; //查看授權(quán)
c. 刪除用戶:
語(yǔ)法: Delete from user where user = "user_name" and host = "host_name" ;
例子:delete from user where user='sss' and host='localhost';
二、數(shù)據(jù)庫(kù)表
1.查看所有數(shù)據(jù)庫(kù): 數(shù)據(jù)庫(kù)目錄:/usr/local/mysql/data
mysql> SHOW DATABASES; //顯示數(shù)據(jù)庫(kù)
mysql> USE abccs //進(jìn)入數(shù)據(jù)庫(kù)
mysql> SHOW TABLES; //顯示表
mysql> DESCRIBE mytable; //顯示表結(jié)構(gòu)
mysql> CREATE DATABASE abccs; //創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)
mysql> CREATE TABLE mytable (name VARCHAR(20), sex CHAR(1), birth DATE, birthaddr VARCHAR(20)); //創(chuàng)建表
mysql> insert into mytable values (‘a(chǎn)bccs’,‘f’,‘1977-07-07’,‘china’); //插入表數(shù)據(jù)
使用文本方式插入數(shù)據(jù):
{
mysql.txt內(nèi)容:abccs f 1977-07-07 china
mary f 1978-12-12 usa
tom m 1970-09-02 usa
mysql> LOAD DATA LOCAL INFILE "mytable.txt" INTO TABLE pet; //導(dǎo)入TXT文件數(shù)據(jù)
}
2.刪除數(shù)據(jù)庫(kù):
mysql> drop database drop_database; //刪除一個(gè)已經(jīng)確定存在的數(shù)據(jù)庫(kù)
alter table 表名 ENGINE=存儲(chǔ)引擎名; //修改表的存儲(chǔ)引擎
alter table 表名 drop 屬性名; //刪除字段
alter table 舊表名 rename to 新表名; //修改表名
alter table 表名 modify 屬性名 數(shù)據(jù)類型; //修改字段數(shù)據(jù)類型
alter table 表名 change 舊屬性名 新屬性名 新數(shù)據(jù)類型; //修改字段名
alter table 表名 drop FOREING KEY 外鍵別名; //刪除子表外鍵約束
增加表字段:
{ alter table example add phone VACGAR(20); //增加無(wú)約束的字段
alter table example add age INT(4) NOT NULL; //增加萬(wàn)增約束的字段
alter table example add num INT(8) PRIMARY KEY FIRST; //表的第一個(gè)位置增加字段
alter table example add address VARCHAR(30) NOT NULL AFTER phone; //表的指定位置之后增加字段
alter table example modify name VARCHAR(20) FIRST; //把字段修改到第一位
alter table example modify num INT(8) ATER phone;//把字段修改到指定字段之后
}
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com