mysql创建用户、数据库和分配权限

(1)在window系统下打开dos命令窗口:
输入如下命令:
mysql -u root -p
输入密码,进入到mysql命令行
(2)创建一个用户(问题:这个需要同时创建本地和外网)
允许本地IP访问:
create user 'dbzhang'@'localhost' identified by 'dbzhang';
允许外网IP访问;
create user 'dbzhang'@'%' identified by 'dbzhang';
刷新权限:
flush privileges;
(3)创建数据库:dbzhang_base_001
create database dbzhang_base_001 default charset utf8 collate utf8_general_ci;
(4)用户分配权限
--注意这个表的符号``,而不是单引号;
授予用户通过外网IP对于该数据库的全部权限
grant all privileges on `dbzhang_base_001`.* to 'dbzhang'@'%' identified by 'dbzhang';
授予用户在本地服务器对该数据库的全部权限
grant all privileges on `dbzhang_base_001`.* to 'dbzhang'@'localhost' identified by 'dbzhang';
刷新权限
flush privileges;
退出root用户登陆新建的用户
exit
使用新账号登陆:
mysql -u dbzhang -h 192.168.2.144 -p
输入密码之后进入mysql命令行;


在Ubuntu服务器下,MySQL默认是只允许本地登录,因此需要修改配置文件将地址绑定给注释掉:
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#bind-address        = 127.0.0.1        #注释掉这一行就可以远程登录了


---在给用户dbzhang创建多个数据库dbzhang_base_001和dbzhang_base_002
create database dbzhang_base_002 DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
grant all privileges on `dbzhang_base_002`.* to 'dbzhang'@'%' identified by 'dbzhang';
grant all privileges on `dbzhang_base_002`.* to 'dbzhang'@'localhost' identified by 'dbzhang';
flush privileges;

你可能感兴趣的:(MySql,mysql创建用户相关内容)