1. 创建数据库,指定utf8
编码和utf8_bin
排序
mysql> create database if not exists mall default character set utf8 collate utf8_bin;
2. 添加新用户
mysql> create user 'username'@'%' identified by '123456';
# or
mysql> insert into mysql.user(Host, User, Password) values ('localhost', 'username', '123456');
3. 为用户授权数据库访问
3.1 语法
mysql> grant 权限1,权限2,…权限n on 数据库名称.表名称 to 用户名@用户地址 identified by ‘连接口令’;
其中权限1到n
代表 select,insert,update,delete,create,drop,index,alter,grant,references,reload,shutdown,process,file
等14个权限。
- 当
权限1,权限2,…权限n
被all privileges
或者all
代替,表示赋予用户全部权限。 - 当
数据库名称.表名称
被*.*
代替,表示赋予用户操作服务器上所有数据库所有表的权限。 - 用户地址可以是
localhost
,也可以是ip地址
、机器名字
、域名
。也可以用%
表示从任何地址连接。 连接口令
不能为空,否则创建失败。
3.2 例如
# 授权`root`用户使用`password`密码从任何主机连接到mysql服务器上的任何数据库。
mysql> grant all privileges on *.* to 'root'@'%' identified by 'password';
mysql> flush privileges;
# 授权`root`用户使用`password`密码从主机192.168.10.149连接到mysql服务器上的任何数据库。
mysql> grant all privileges on *.* to 'root'@'192.168.10.149' identified by 'password';
mysql> flush privileges;
# 授权`root`用户使用`password`密码从主机192.168.10.149连接到mysql服务器上的sonar数据库。
mysql> grant all privileges on dbname.* to 'root'@'192.168.10.149' identified by 'password';
mysql> flush privileges;
# 授权`username`用户只有部分权限
mysql> grant select,insert,update,delete on dbname.* to 'username'@'%' identified by 'password';