注: 用户:cifpay 用户数据库:cifpaydb
1.新建用户。
//登录MYSQL
@>mysql -u root -p
@>密码
//创建用户
mysql> insert into mysql.user(Host,User,Password) values("localhost","cifpay",password("1234"));
//刷新系统权限表
mysql>flush privileges; --刷新系统表写入磁盘
这样就创建了一个名为:phplamp 密码为:1234 的用户。
然后登录一下。
mysql>exit;
@>mysql -ucifpay -p
@>输入密码
mysql>登录成功
2.为用户授权。
//登录MYSQL(有ROOT权限)。我里我以ROOT身份登录.
@>mysql -u root -p
@>密码
//首先为用户创建一个数据库(cifpaydb)
mysql>create database cifpaydb ;
//授权cifpay用户拥有 cifpaydb 数据库的所有权限。
>grant all privileges on cifpaydb.* to cifpay@localhost identified by '1234';
//刷新系统权限表
mysql>flush privileges;
mysql>其它操作
/*
如果想指定部分权限给一用户,可以这样来写:
mysql>grant select,update on cifpaydb.* to user_name@localhost identified by '1234';
//刷新系统权限表。
mysql>flush privileges;
*/
命令: REVOKE privilege ON databasename.tablename FROM 'username'@'host';
3.删除用户。
@>mysql -u root -p
@>密码
mysql>DELETE FROM user WHERE User="xxxx" and Host="localhost";
mysql>flush privileges;
//删除用户的数据库
mysql>drop database cifpaydb;
------------------------------------------------------------------------------------------------------
4.修改指定用户密码。
@>mysql -u root -p
@>密码
mysql>update mysql.user set password=password('新密码') where User="user_name" and Host="localhost";
mysql>flush privileges;
---------------------------------client 访问授权--------------------------------------
--MYSQL中有一张user表 保存了数据库用户信息,其中有一列叫host,定义了可以访问服务器的客户端ip 要让所有客户端都能访问数据库 需要执行如下:
--备注:所有客户端都可以用root访问数据库,并保留初始密码
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
--备注:所有客户端都可以用root访问数据库,用admin作为密码
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'admin' WITH GRANT OPTION;
--授权:
1. 新增用户
MYSQL > flush privileges;
2. 修改用户密码
MYSQL > flush privileges;
3. 删除用户
MYSQL > flush privileges;
4. 权限分配
-- grant用法
grant 权限 on 数据库.* to 用户名@'登录主机' identified by '密码'; --/* with grant option */ 详解如下;
代码如下:
权限:
数据库:
*.* 表示所有库的所有表
test.* 表示test库的所有表
test.test_table 表示test库的test_table表
用户名:
mysql账户名
登陆主机:
允许登陆mysql server的客户端ip
'%'表示所有ip
'localhost' 表示本机
'192.168.10.2' 特定IP
密码:
账户对应的登陆密码
-------------------------------------- 整理完 --------------------------------------------
ALTER | Allows use of ALTER TABLE. |
ALTER ROUTINE | Alters or drops stored routines. |
CREATE | Allows use of CREATE TABLE. |
CREATE ROUTINE | Creates stored routines. |
CREATE TEMPORARY TABLE | Allows use of CREATE TEMPORARY TABLE. |
CREATE USER | Allows use of CREATE USER, DROP USER, RENAME USER, and REVOKE ALL PRIVILEGES. |
CREATE VIEW | Allows use of CREATE VIEW. |
DELETE | Allows use of DELETE. |
DROP | Allows use of DROP TABLE. |
EXECUTE | Allows the user to run stored routines. |
FILE | Allows use of SELECT... INTO OUTFILE and LOAD DATA INFILE. |
INDEX | Allows use of CREATE INDEX and DROP INDEX. |
INSERT | Allows use of INSERT. |
LOCK TABLES | Allows use of LOCK TABLES on tables for which the user also has SELECT privileges. |
PROCESS | Allows use of SHOW FULL PROCESSLIST. |
RELOAD | Allows use of FLUSH. |
REPLICATION | Allows the user to ask where slave or master |
CLIENT | servers are. |
REPLICATION SLAVE | Needed for replication slaves. |
SELECT | Allows use of SELECT. |
SHOW DATABASES | Allows use of SHOW DATABASES. |
SHOW VIEW | Allows use of SHOW CREATE VIEW. |
SHUTDOWN | Allows use of mysqladmin shutdown. |
SUPER | Allows use of CHANGE MASTER, KILL, PURGE MASTER LOGS, and SET GLOBAL SQL statements. Allows mysqladmin debug command. Allows one extra connection to be made if maximum connections are reached. |
UPDATE | Allows use of UPDATE. |
USAGE | Allows connection without any specific privileges. |