基礎(chǔ)管理
1 用戶, 權(quán)限
1.1 用戶
作用: 登錄和管理數(shù)據(jù)庫
用戶語法:
1.) ziya@'10.0.0.%' (只允許ziya用戶通過10.0.0.0/24的網(wǎng)段登錄數(shù)據(jù)庫)
2.) ziya@'%' (允許ziya用戶通過所有網(wǎng)段登錄數(shù)據(jù)庫)
3.) ziya@'10.0.0.200' (只允許ziya用戶通過10.0.0.200登錄數(shù)據(jù)庫)
4.) ziya@'localhost' (只允許ziya用戶通過本地登錄數(shù)據(jù)庫)
5.) ziya@'db02' (只允許ziya用戶通過域名為db02對應(yīng)的ip登錄數(shù)據(jù)庫)
6.) ziya@'10.0.0.5%' (只允許ziya用戶通過10.0.0.50-59的IP地址登錄數(shù)據(jù)庫)
7.) ziya@'10.0.0.0/255.255.254.0' (只允許ziya用戶通過10.0.0.0/23的網(wǎng)段登錄數(shù)據(jù)庫)
1.1.1 管理用戶操作:
增:
mysql> create user ziya@'10.0.0.%' identified by '123';
改:
mysql> alter user ziya@'10.0.0.%' identified by '456';
查:
mysql> desc mysql.user; ----> authentication_string (查看表頭)
mysql> select user ,host ,authentication_string from mysql.user;
刪:
mysql> drop user ziya@'10.0.0.%';
1.2 權(quán)限
1.2.1 權(quán)限管理
mysql> grant all on app.* to ziya@'10.0.0.%' identified by '123';
grant ---- 授權(quán)
all ---- 要授的權(quán)利(all為所有權(quán)利)
on ---- 在什么上面授權(quán)
app.* ---- 哪個(gè)數(shù)據(jù)庫(為所有).哪個(gè)表(為所有表)
to ---- 給哪個(gè)用戶
ziya@'10.0.0.%' ---- 哪個(gè)用戶
identified by ' 密碼' ---- 用戶的密碼
1.2.2 常用權(quán)限:
select (查看), insert (插入), update (更新), delete (刪除數(shù)據(jù)), create (創(chuàng)建), drop (刪除庫), reload (重新加載), shutdown (關(guān)閉), process (處理), file (文件), references (引用), index (索引), alter (更改), show databases (顯示數(shù)據(jù)庫), super (超級), temporary tables (創(chuàng)建臨時(shí)表), lock tables (鎖定表), execute (執(zhí)行), replication slave (開啟主從復(fù)制), replication client (復(fù)制客戶機(jī)), create view (創(chuàng)建試圖), show view (顯示試圖), create routine (創(chuàng)建列程), alter routine (更改列程), create user (創(chuàng)建用戶), event (事件), trigger (觸發(fā)器), create tablespace (創(chuàng)建表空間)
ALL : 以上所有權(quán)限,一般是普通管理員擁有的
with grant option:超級管理員才具備的,給別的用戶授權(quán)的功能
1.2.3 查看授權(quán)
mysql> show grants for app@'10.0.0.%';
1.2.4 回收權(quán)限
revoke delete on app.* from app@'10.0.0.%'仰冠;
delete ---- 要收回哪些權(quán)利(用逗號隔開)
on ---- 從哪刪
app.* ---- 從哪個(gè)庫(為所有).哪個(gè)表(為所有)
from ---- 從哪個(gè)用戶
app@'10.0.0.%' ---- 用戶
2.給用戶授權(quán)流程
1) 你從哪里來 ( 從哪個(gè)地址連接數(shù)據(jù)庫)
2) 你到哪里去 (對哪個(gè)庫,哪個(gè)表操作)
3) 你要干什么 (授給什么權(quán)利)
4) 你何時(shí)離開 (什么時(shí)候把權(quán)利收回來)
5) 密碼要求
注: 授權(quán)的時(shí)候要慎重 慎重 在慎重
3. 找回本地管理員用戶密碼
3.1 先關(guān)閉數(shù)據(jù)庫
通過開機(jī)時(shí)用的命令 不要把兩套命令用混了
3.2 使用免驗(yàn)證啟動數(shù)據(jù)庫
mysqld_safe --defaults-file=/data/3307/my.cnf --skip-grant-tables --skip-networking &
--defaults-file=/data/3307/my.cnf ---- 配置文件路徑
--skip-grant-tables ---- 關(guān)閉驗(yàn)證表(登錄時(shí)不用驗(yàn)證用戶密碼)
--skip-networking ---- 禁止遠(yuǎn)程登錄
& ---- 把程序放到后臺
3.3 進(jìn)入數(shù)據(jù)庫
mysql -uroot -p -S /data/3307/mysql.sock
3.4 把驗(yàn)證表加載到內(nèi)存
flush privileges;
3.5 修改密碼
update user set authentication_string = password('123456') where user='root';
alter user root@'localhost' identified by '123';
set password for root@localhost = password('123');
3.6 關(guān)閉服務(wù)
用kill殺死服務(wù) 在重啟服務(wù)就可以了