一、建立用戶php
CREATE USER test@localhost IDENTIFIED BY '123456'; #創建的用戶名爲test,密碼爲123的用戶, #localhost限制在固定地址localhost登錄
#建立遠程鏈接用戶 create user test2 identified by '123456';
二、受權mysql
GRANT privileges ON databasename.tablename TO 'username'@'host'
說明: privileges - 用戶的操做權限,如SELECT , INSERT , UPDATE 等。若是要授予所的權限則使用 ALL;
databasename - 數據庫名,tablename-表名,若是要授予該用戶對全部數據庫和表的相應操做權限則可用*表示, 如*.*sql
例如:數據庫
GRANT INSERT,DELETE,UPDATE,SELECT ON test.* TO 'test'@'localhost'; flush privileges;
grant all privileges on *.* to 'test2'@'%'identified by '123456' with grant option;
all表明接受全部操做,好比 select,insert,delete....;安全
*.* 表明全部庫下面的全部表;ide
% 表明這個用戶容許從任何地方登陸;spa
爲了安全期間,這個%能夠替換爲你容許的ip地址;3d
而後刷新mysql用戶權限相關表;code
flush privileges ;
三、設置與更改用戶密碼blog
SET PASSWORD FOR 'username'@'host' = PASSWORD('newpassword') #若是是當前登錄用戶 SET PASSWORD = PASSWORD("newpassword"); #例如: SET PASSWORD FOR 'test2'@'%' = PASSWORD("123456"); update mysql.user set password=password('新密碼') where User="phplamp" and Host="localhost";
update mysql.user set password=password('新密碼') where User="test" and Host="localhost";
打開 mysql配置文件
bind-address = 127.0.0.1
這裏mysql默認綁定了本地ip,不接受其餘來源;如要遠程鏈接數據庫,註釋掉這個,重啓mysql;
刪除用戶
delete from user where User='test' and Host='localhost';