mariadb创建用户_如何在MySQL和MariaDB中创建新用户并设置,授予用户权限?

mariadb创建用户

mariadb创建用户

MySQL provides the ability to create and users from its shell. Security is an important part of the database servers. In this tutorial, we will examine how to create users and manage privileges in MySQL database server. We will specifically learn to restrict user access to a database, database server, grant different types of privileges, print these privileges.

MySQL提供了从Shell创建和用户的能力。 安全性是数据库服务器的重要组成部分。 在本教程中,我们将研究如何在MySQL数据库服务器中创建用户和管理特权。 我们将专门学习限制用户访问数据库,数据库服务器,授予不同类型的特权,打印这些特权。

登录到MySQL Shell (Login To MySQL Shell)

In order to create a user and manage the privileges of this user, we will log in to the MySQL shell. MySQL shell provides the ability to access databases, run SQL queries, administrative operations like create user, delete user, manage user privileges. We will use mysql command with the user name which is root in this case. We will provide the root user password too.

为了创建用户并管理该用户的特权,我们将登录到MySQL Shell。 MySQL Shell提供了访问数据库,运行SQL查询以及诸如创建用户,删除用户,管理用户权限之类的管理操作的能力。 在这种情况下,我们将使用具有root用户名的mysql命令。 我们还将提供root用户密码。

$ sudo mysql -u root -p
Login To MySQL Shell
Login To MySQL Shell
登录到MySQL Shell

创建新的数据库用户 (Create New Database User)

We will use CREATE USER command by providing the user name, hostname, and password. The hostname is used to set whether the user can connect from a given host. In this example, we specify the localhost which means given user  ismail can only connect from localhost to this database server. We also provide the password as SoS3cret..!

我们将通过提供用户名,主机名和密码来使用CREATE USER命令。 主机名用于设置用户是否可以从给定主机进行连接。 在此示例中,我们指定了localhost ,这意味着给定的用户ismail只能从localhost连接到此数据库服务器。 我们还提供密码SoS3cret..!

> CREATE USER 'ismail'@'localhost' IDENTIFIED BY 'SoS3cret..!';
Create New User
Create New User
建立新使用者

As it is successfully created Query OK, 0 rows affected (0.01 sec)  is printed.

成功创建Query OK, 0 rows affected (0.01 sec)成功后Query OK, 0 rows affected (0.01 sec)将打印Query OK, 0 rows affected (0.01 sec)

删除现有的数据库用户 (Remove Existing Database User)

If we do not need a given database user we have to remove users because it may create security problems and unintended database access. We can remove the existing database user with the DROP USER command by providing the user name and access hostname. In this example, we will remove user ismail with localhost access.

如果我们不需要给定的数据库用户,则必须删除用户,因为这可能会导致安全问题和意外的数据库访问。 通过提供用户名和访问主机名,我们可以使用DROP USER命令删除现有的数据库用户。 在此示例中,我们将删除具有localhost访问权限的用户ismail

> DROP USER[email protected];
Remove Existing Database User
Remove Existing Database User
删除现有的数据库用户

列出现有的数据库用户(List Existing Database Users)

After creating a database user we may want to check and list existing users in the MySQL database. In this case, we will use an SQL statement that will list the content of the mysql database user table. user table in the mysql database holds database server users and related information like password etc. We will use SELECT User statement.

创建数据库用户后,我们可能要检查并列出MySQL数据库中的现有用户。 在这种情况下,我们将使用一条SQL语句,该语句将列出mysql数据库user表的内容。 mysql数据库中的user表包含数据库服务器用户和相关信息,例如密码等。我们将使用SELECT User语句。

> SELECT User FROM mysql.user;
List Existing Database Users
List Existing Database Users
列出现有的数据库用户

创建新用户以访问特定数据库(Create New User with To Access Specific Database)

We can specify a user only access to the specified databases. This will prevent user access to other databases. We will use GRANT ALL PRIVILEGES command in this case by providing the database name, user name, and hostname. In the following example, we will grant all privileges of the database poftut to the user ismail .

我们可以指定一个用户只能访问指定的数据库。 这将阻止用户访问其他数据库。 在这种情况下,我们将通过提供数据库名称,用户名和主机名来使用GRANT ALL PRIVILEGES命令。 在以下示例中,我们将向用户ismail授予数据库poftut所有特权。

> GRANT ALL PRIVILEGES ON poftut . * TO 'ismail'@'localhost';
Create New User with To Access Specific Database
Create New User with To Access Specific Database
创建新用户以访问特定数据库

将更改写入数据库(Write Changes To Database)

Changes may be stored in the cache which will not effective immediately. We can write these changes into the database server explicitly with the following command.

更改可能会存储在缓存中,但不会立即生效。 我们可以使用以下命令将这些更改显式写入数据库服务器。

> FLUSH PRIVILEGES;
Write Changes To Database
Write Changes To Database
将更改写入数据库

创建具有访问位置的新用户(Create New User with Access Location)

We can also specify the access location for the given user and database. Up to now, we have used the localhost which is a secure way where only local users can access. We can also provide access rights for specific networks or hosts. In this example, we will provide access from the IP Address 192.168.1.10 .

我们还可以指定给定用户和数据库的访问位置。 到目前为止,我们使用了本地主机,这是仅本地用户可以访问的安全方式。 我们还可以提供特定网络或主机的访问权限。 在此示例中,我们将提供从IP地址192.168.1.10访问。

> GRANT ALL PRIVILEGES ON poftut . * TO 'ismail'@'192.168.1.10';

显示并列出给定的用户权限 (Show and List Given User Privileges)

We may want to list granted privileges for the given user. We will use SHOW GRANTS FOR command for this operation. In this example, we will list granted privileges for the user ismail access from localhost.

我们可能要列出给定用户的授予的特权。 我们将使用SHOW GRANTS FOR命令进行此操作。 在此示例中,我们将列出从localhost访问用户ismail授予特权。

> SHOW GRANTS FOR[email protected];
Show and List Given User Privileges
Show and List Given User Privileges
显示并列出给定的用户权限
LEARN MORE  How To List MySQL Database Users?
了解更多如何列出MySQL数据库用户?

翻译自: https://www.poftut.com/how-to-create-new-user-and-set-grant-permissions-to-user-in-mysql-and-mariadb/

mariadb创建用户

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值