1、以root用户登录Mysql

    1. mysql -uroot -proot
    2. # 或
    3. sudo mysql

    2、切换到mysql数据库

    1. use mysql

    3、添加用户

    1. //只允许指定ip连接
    2. create user '新用户名'@'localhost' identified by '密码';
    3. //允许所有ip连接(用通配符%表示)
    4. create user '新用户名'@'%' identified by '密码';

    4、为新用户授权

    1. //基本格式如下
    2. grant all privileges on 数据库名.表名 to '新用户名'@'指定ip';
    3. //示例
    4. //允许访问所有数据库下的所有表
    5. grant all privileges on *.* to '新用户名'@'指定ip';
    6. //指定数据库下的指定表
    7. grant all privileges on test.test to '新用户名'@'指定ip';

    5、设置用户操作权限

    1. //设置用户拥有所有权限也就是管理员
    2. grant all privileges on *.* to '新用户名'@'指定ip' identified by '新用户密码' WITH GRANT OPTION;
    3. //拥有查询权限
    4. grant select on *.* to '新用户名'@'指定ip' identified by '新用户密码' WITH GRANT OPTION;
    5. //其它操作权限说明,select查询 insert插入 delete删除 update修改
    6. //取消用户查询的查询权限
    7. REVOKE select ON what FROM '新用户名';

    6、删除用户

    1. DROP USER username@localhost;

    7、修改后刷新权限

    1. FLUSH PRIVILEGES;