MySQL添加用户、创建数据库、分配权限

一  登录root

mysql -uroot -p

二  创建新用户

mysql> create user 'user'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

mysql> create user 'user'@'%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

三  创建数据库

mysql> create database testdb default charset utf8 collate utf8_general_ci;
Query OK, 1 row affected (0.00 sec)

四  分配权限

mysql> grant all privileges on `testdb`.* to 'user'@'%';
Query OK, 0 rows affected (0.00 sec)

mysql> grant all privileges on `testdb`.* to 'user'@'localhost';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

  注意: 此处以前我习惯性用 

    grant all privileges on `testdb`.* to 'user'@'%' identified by '123456';  

      但是数据库版本不同有时会不能成功执行,但是都支持 

    grant all privileges on `testdb`.* to 'user'@'%';

五  退出并使用新账号登录

mysql> exit
Bye
mysql -uuser -p     //输入密码123456

六  特别说明

  • 第四步中的注意
  • 有的操作系统,比如ubuntu,MySQL默认是只允许本地登录,因此
    ① 需要修改配置文件并重启mysql
    #bind-address        = 127.0.0.1        #注释掉这一行并重启

    ② 需要对外/指定ip/ip段 开放3306端口

原文地址:https://www.cnblogs.com/bushuwei/p/13618484.html