MySQL: Grant **all** privileges on database

后端 未结 11 1193
庸人自扰
庸人自扰 2020-11-22 09:23

I\'ve created database, for example \'mydb\'.

CREATE DATABASE mydb CHARACTER SET utf8 COLLATE utf8_bin;
CREATE USER \'myuser\'@\'%\' IDENTIFIED BY PASSWORD          


        
11条回答
  •  孤街浪徒
    2020-11-22 10:17

    This is old question but I don't think the accepted answer is safe. It's good for creating a super user but not good if you want to grant privileges on a single database.

    grant all privileges on mydb.* to myuser@'%' identified by 'mypasswd';
    grant all privileges on mydb.* to myuser@localhost identified by 'mypasswd';
    

    % seems to not cover socket communications, that the localhost is for. WITH GRANT OPTION is only good for the super user, otherwise it is usually a security risk.

    Update for MySQL 5.7+ seems like this warns about:

    Using GRANT statement to modify existing user's properties other than privileges is deprecated and will be removed in future release. Use ALTER USER statement for this operation.

    So setting password should be with separate commands. Thanks to comment from @scary-wombat.

    ALTER USER 'myuser'@'%' IDENTIFIED BY 'mypassword';
    ALTER USER 'myuser'@'localhost' IDENTIFIED BY 'mypassword';
    

    Hope this helps.

提交回复
热议问题