Using % for host when creating a MySQL user

前端 未结 6 1577
醉酒成梦
醉酒成梦 2020-11-29 18:06

My MySQL database needs two users: appuser and support.
One of the application developers insists that I create four accounts for these users:

appuser@\'         


        
6条回答
  •  [愿得一人]
    2020-11-29 18:37

    The percent symbol means: any host, including remote and local connections.

    The localhost allows only local connections.

    (so to start off, if you don't need remote connections to your database, you can get rid of the appuser@'%' user right away)

    So, yes, they are overlapping, but...

    ...there is a reason for setting both types of accounts, this is explained in the mysql docs: http://dev.mysql.com/doc/refman/5.7/en/adding-users.html.

    If you have an have an anonymous user on your localhost, which you can spot with:

    select Host from mysql.user where User='' and Host='localhost';
    

    and if you just create the user appuser@'%' (and you not the appuser@'localhost'), then when the appuser mysql user connects from the local host, the anonymous user account is used (it has precedence over your appuser@'%' user).

    And the fix for this is (as one can guess) to create the appuser@'localhost' (which is more specific that the local host anonymous user and will be used if your appuser connects from the localhost).

提交回复
热议问题