• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

MySQL平添用户

mysql 搞代码 7年前 (2018-06-05) 116次浏览 已收录 0个评论

mysql添加用户
Nowadays security is more and more important even in case of smaller websites. As a lot of site uses some database so this is one point where we can make some improvements.

In this article we will work with MySQL on our goal is to create a new user in the database.

There are more ways how you can do this.

    Using CREATE USER and/or GRANT commands
    Inserting a new record into the mysql.user table

First let’s see how to use the CREATE USER command. Here I have to mention that thi s command is available only in MySQL 5 (5.0.2) and newer releases. The syntax is the following:  

CREATE USER user [IDENTIFIED BY [PASSWORD] ‘password’]

Here is an example:

Code:

    
    CREATE USER ‘user1’@’localhost’ IDENTIFIED BY ‘pass1’;
    

Now if you check the mysql.user table you can find a new record in it. Notice that all priviliges are set to No so this user can do nothing in the DB. To add some preiviliges we can use the GRANT command as follows:

Code:

    
    GRANT SELECT,INSERT,UPDATE,DELETE ON *.* TO ‘user1’@’localhost’;
    

Here we have added only the most important priviliges to the user. With the setting above this user is good to run a CMS or a blog, however with such settings this user is not able to install them as it can not create tables.

To add all priviliges to the user you don’t have to list all of them but you can use the ALL shortcut as follows:

Code:

    
    GRANT ALL ON *.* TO ‘user1’@’localhost’;
    

You can create a new MySQL user in one step as well using again the GRANT command with a small extension as here:

Code:

    
    GRANT ALL ON *.* TO ‘user2’@’localhost’ IDENTIFIED BY ‘pass1’;
    

The above examples used dedicated commands, but sumtimes you maybe want to add a new MySQL user via directly editing the mysql.user table. In this case you just inserts a new record into the table with a normal INSERT command:

Code:

    
    INSERT INTO user (Host,User,Password)
    VALUES(‘localhost’,’user3′,PASSWORD(‘pass3’));
    

Or you can add some priviliges as well in a form like this:

Code:

    
    INSERT INTO user (Host,User,Password,Select_priv,Insert_priv)
    VALUES(‘localhost’,’user4′,PASSWORD(‘pass3′),’Y’,’Y’);
    


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:MySQL平添用户

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址