添加用户
mysql -u root -p
//允许本地访问
create user 'username'@'localhost' identified by 'password';
// 允许外网访问
create user 'username'@'%' identified by 'password';
//刷新授权
flush privileges;
修改密码
SET PASSWORD=PASSWORD('修改的密码');
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY
'newpasswd';
ALTER USER 'username'@'localhost' IDENTIFIED WITH mysql_native_password BY 'newpasswd';
//外网
ALTER USER 'username'@'%' IDENTIFIED WITH mysql_native_password BY
'newpasswd';
// mysql_native_password 表示传统的加密方式(mysql 5.x),不使用该选项则使用新的加密方式(mysql 8)
分配权限
# create database testdb DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
//# grant all privileges on testdb.* to 'username'@'localhost' (identified with mysql_native_password by 'password'; )
// 不需要更改密码,括号内的可不写
# grant all privileges on testdb.* to 'username'@'%' ( identified with mysql_native_password by 'password'; )
// *.*所有数据库; tested.*表示只能操作testdb数据库; testedb.student表示只能操作testdb数据库的student表;
// 分配部分权限: insert, select,update,delete,create,drop,alter(为表增删改字段add,delete,modify/change),
//刷新权限
# flush privileges
- 退出 root 重新登录
用新帐号 重新登录,由于使用的是 % 任意IP连接,所以需要指定外部访问IP
mysql -u username -p -h www.xxx.com (主机名为域名或者IP地址)
//如果登录本地数据库,-h选项可省