返回顶部
首页 > 资讯 > 数据库 >mysql初始化、增删改查用户、授权
  • 689
分享到

mysql初始化、增删改查用户、授权

2024-04-02 19:04:59 689人浏览 安东尼
摘要

1. 数据库安全初始化 [root@elasticsearch my.cnf.d]# Mysql_secure_installation #安全初始化命令 NOTE: RUNNING ALL

1. 数据库安全初始化

[root@elasticsearch my.cnf.d]# Mysql_secure_installation        #安全初始化命令

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
passWord for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):               #输入mysql的root账户默认密码(默认为空)
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y                      #是否设置root密码
New password:                                   #为root用户输入一个新密码
Re-enter new password:                          #再次输入密码
Password updated successfully!                  #密码更新成功
Reloading privilege tables..
 ... Success!

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
Go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y                 #是否删除匿名用户
 ... Success!

NORMally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y           #是否允许root用户远程登录
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n]    #是否删除test数据库
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n]              #是否刷新以上操作,使其立即生效
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!
  • 以上操作完成后,命令行直接输入mysql无法登录数据库,使用本机外网地址也无法登录。
[root@elasticsearch my.cnf.d]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@elasticsearch my.cnf.d]# mysql -uroot -h292.168.0.194 -p
Enter password: 
ERROR 1130 (HY000): Host 'node1' is not allowed to connect to this MariaDB server
[root@elasticsearch my.cnf.d]# mysql -uroot -h227.0.0.1 -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 14
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

[root@elasticsearch my.cnf.d]# mysql -uroot -hlocalhost -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 15
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
  • 查看当前授权的用户和地址可见root仅授权了localhost和127.0.0.1可登录
MariaDB [(none)]> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [mysql]> select User,Host from user;
+------+-----------+
| User | Host      |
+------+-----------+
| root | 127.0.0.1 |
| root | ::1       |
| root | localhost |
+------+-----------+
3 rows in set (0.00 sec)

MariaDB [mysql]> 

2. 忘记管理员密码的解决办法:

  1. 启动mysql前,编辑/etc/my.cnf,添加skip-grant-tables和skip-networking;
[mysqld]
skip-grant-tables
skip-networking
datadir=/var/lib/mysql
  1. 通过UPDATE命令修改管理员密码;
[root@elasticsearch ~]# systemctl start mariadb
[root@elasticsearch ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> update mysql.user set authentication_string=password('Centos') where user='root' and Host = 'localhost';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> exit
Bye
  1. 删除/etc/my.cnf中添加的内容,以正常方式启动mysqld进程;
[root@elasticsearch ~]# mysql -uroot -hlocalhost -pcentos
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 

3. 增、删、改、查用户

  1. 查看用户:
  • mysql中用户表在mysql.user中
  • 查看用户示例:
MariaDB [(none)]> SELECT User,Host FROM mysql.user;
+-------+-------------+
| User  | Host        |
+-------+-------------+
| root  | 127.0.0.1   |
| root  | ::1         |
| root  | localhost   |
+-------+-------------+
6 rows in set (0.01 sec)
  1. 添加用户
    • 格式:可一次创建多个用户
CREATE USER  'user'@'host' [IDENTIFIED BY [PASSWORD] 'password'] [,'user'@'host' [IDENTIFIED BY [PASSWORD] 'password']...]
  • 例:
# 单条命令创建一个用户:
MariaDB [(none)]> CREATE USER 'lxk'@'localhost' IDENTIFIED BY PASSWORD 'linux.centos.com';
Query OK, 0 rows affected (0.00 sec)
# 以逗号为分隔,单条命令创建两个用户:
MariaDB [(none)]> CREATE USER 'test0'@'192.168.1.%' IDENTIFIED BY 'maria.centos.com','test1'@'192.168.1.%' IDENTIFIED BY 'maria.centos.com';
Query OK, 0 rows affected (0.00 sec)
  1. 重命名用户:
    • 格式:
    • RENAME USER old_user TO new_user[, old_user TO new_user] ...
  • 例:(创建用户时加了授权地址,修改时也需加上授权地址)
#查看当前用户:
MariaDB [(none)]> SELECT User,Host FROM mysql.user;
+-------+-------------+
| User  | Host        |
+-------+-------------+
| root  | 127.0.0.1   |
| test0 | 192.168.1.% |
| test1 | 192.168.1.% |
| root  | ::1         |
| lxk   | localhost   |
| root  | localhost   |
+-------+-------------+
6 rows in set (0.01 sec)

MariaDB [(none)]> RENAME USER 'test1'@'192.168.1.%' TO 'test001'@'192.168.1.%';
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> SELECT User,Host FROM mysql.user;
+---------+-------------+
| User    | Host        |
+---------+-------------+
| root    | 127.0.0.1   |
| test0   | 192.168.1.% |
| test001 | 192.168.1.% |
  1. 删除用户:
    • 格式:
    • DROP USER 'user'@'host' [, 'user'@'host'] ...
    • 例:
MariaDB [(none)]> DROP USER 'test001'@'192.168.1.%';
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> SELECT User,Host FROM mysql.user;
+-------+------------+
| User  | Host       |
+-------+------------+
| root  | 127.0.0.1  |
| test0 | 192.168.1.%|
| root  | ::1        |
| lxk   | localhost  |
| root  | localhost  |
+-------+------------+
5 rows in set (0.00 sec)
  1. 重新加载授权表:
    • 作用:有时操作并不会马上写到磁盘上,执行此命令可把操作马上同步到磁盘上。
    • 例:
MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

4. 用户授权相关:

  1. 查看用户授权:
  • 格式:
    • SHOW GRANTS [FOR 'user'@'host']
MariaDB [(none)]> show grants;          #不加用户,默认查找的是root用户的授权信息。
+----------------------------------------------------------------------------------------------------------------------------------------+
| Grants for root@localhost                                                                                                              |
+----------------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '*128977E278358FF80A246B5046F51043A2B1FCED' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION                                                                           |
+----------------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

MariaDB [(none)]> show grants for 'test0'@'192.168.1.%';        #查看指定用户的授权信息。
+---------------------------------------------------------------------------------------------------------------+
| Grants for test0@192.168.1.%                                                                                  |
+---------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'test0'@'192.168.1.%' IDENTIFIED BY PASSWORD '*5FC1DC57211AE5F87FC504DEEE4B7C65DEB2CBFA'|
+---------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
  1. 给用户授权:
  • 格式:
GRANT  priv_type [(column_list)] [, priv_type [(column_list)]] ...
    ON [object_type] priv_level
    TO user_specification [, user_specification] ...
    [REQUIRE {NONE | ssl_option [[AND] ssl_option] ...}]
    [WITH with_option ...]
  • 简化格式:
GRANT  priv_type ON  [object_type] priv_level TO user_specification [, user_specification]

    object_type:
        TABLE
        | FUNCTION
        | PROCEDURE

    priv_level:
        *
        | *.*                       #所有库的所有表
        | db_name.*                 #某个库的所有表
        | db_name.tbl_name          #某个库的某个表
        | tbl_name                  #某个表
        | db_name.routine_name      #某个库的某个routine
  • 示例:
MariaDB [(none)]> GRANT all ON *.* TO 'test0'@'192.168.1%';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> SHOW GRANTS FOR 'test0'@'192.168.1%';
+------------------------------------------------------------------------------------------------------------------------+
| Grants for test0@192.168.1.%                                                                                           |
+------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'test0'@'192.168.1.%' IDENTIFIED BY PASSWORD '*5FC1DC57211AE5F87FC504DEEE4B7C65DEB2CBFA'|
+------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)
# 
  1. 取消授权:REVOKE
  • 格式
REVOKE  priv_type [(column_list)][, priv_type [(column_list)]] ...
    ON [object_type] priv_level
    FROM  'user'@'host' [,  'user'@'host'] ...

REVOKE ALL PRIVILEGES, GRANT OPTION
    FROM user [, user] ...
  • 示例:
MariaDB [(none)]> REVOKE ALL PRIVILEGES FROM 'test0'@'192.168.1.%';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'FROM 'test0'@'192.168.1%'' at line 1
MariaDB [(none)]> REVOKE ALL PRIVILEGES ON *.* FROM 'test0'@'192.168.1.%';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> SHOW GRANTS FOR 'test0'@'192.168.1%';
+---------------------------------------------------------------------------------------------------------------+
| Grants for test0@192.168.1.%                                                                                  |
+---------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'test0'@'192.168.1.%' IDENTIFIED BY PASSWORD '*5FC1DC57211AE5F87FC504DEEE4B7C65DEB2CBFA'|
+---------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
您可能感兴趣的文档:

--结束END--

本文标题: mysql初始化、增删改查用户、授权

本文链接: https://lsjlt.com/news/38686.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
  • mysql初始化、增删改查用户、授权
    1. 数据库安全初始化 [root@elasticsearch my.cnf.d]# mysql_secure_installation #安全初始化命令 NOTE: RUNNING ALL...
    99+
    2024-04-02
  • MySql增加用户、授权、修改密码等语句
    MySql增加用户、授权、修改密码等语句 数据库top_develop 登录数据库 1:新增用户 [mysql@lcamdb ~]$ mysql -u root -p Enter password: *...
    99+
    2024-04-02
  • C++之vector容器的的声明初始化和增删改查
    C++vector容器  C++中有两种类型的容器:顺序容器和关联容器。 顺序容器主要有vector、list、deque等。其中vector表示一段连续的内存,基于数组实...
    99+
    2024-04-02
  • Linux-MySQL用户查看及授权
    本文主要给大家简单讲讲Linux-MySQL用户查看及授权,相关专业术语大家可以上网查查或者找一些相关书籍补充一下,这里就不涉猎了,我们就直奔主题吧,希望Linux-MySQL用户查看及授权这篇文章可以给大...
    99+
    2024-04-02
  • MySQL新建用户怎么授权、删除用户和修改密码
    这篇文章主要讲解了“MySQL新建用户怎么授权、删除用户和修改密码”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“MySQL新建用户怎么授权、删除用户和修改密...
    99+
    2024-04-02
  • mysql用户的删除和新增以及授权是怎样的
    本篇文章给大家分享的是有关mysql用户的删除和新增以及授权是怎样的,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。1) 删除用户delet&#...
    99+
    2024-04-02
  • Linux用户的增、删、改、查方法
    这篇文章主要介绍“Linux用户的增、删、改、查方法”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Linux用户的增、删、改、查方法”文章能帮助大家解决问题。Linux系统是一个多用户多任务的分时操...
    99+
    2023-06-28
  • ASP.NET MVC使用Identity增删改查用户
    源码在这里:https://github.com/darrenji/UseIdentityCRUDUserInMVC,本地下载 在VS2013中创建一个MVC项目,用默认的"...
    99+
    2022-11-13
    ASP.NET MVC Identity 增删改查用户
  • mysql数据库更改用户密码和用户授权
    查询用户、密码1、select host,user,password from mysql.user;更改密码2、update mysql.user set password=passwor...
    99+
    2024-04-02
  • mysql如何实现用户创建、修改、删除及授权操作
    这篇文章主要介绍了mysql如何实现用户创建、修改、删除及授权操作,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。1、mysql命令进行连接如...
    99+
    2024-04-02
  • MySQL中如何添加删除用户和授权
    这篇文章将为大家详细讲解有关MySQL中如何添加删除用户和授权,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。下面给大家了解一下MySQL中的用户管理,介绍一...
    99+
    2024-04-02
  • MySQL添加账户、授予权限、删除用户的案例
    MySQL添加账户、授予权限、删除用户的案例?这个问题可能是我们日常学习或工作经常见到的。希望通过这个问题能让你收获颇深。下面是小编给大家带来的参考内容,让我们一起来看看吧!1.添加账户:# 创建一个密码为...
    99+
    2024-04-02
  • 怎么实现JavaWeb用户的增删改查
    本篇内容介绍了“怎么实现JavaWeb用户的增删改查”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!准备工作后端技术前端技术角色维护-分页实现...
    99+
    2023-06-16
  • 通过修改MySQL用户授权IP了解其用户管理
    下文主要给大家带来通过修改MySQL用户授权IP了解其用户管理,希望这些内容能够带给大家实际用处,这也是我编辑通过修改MySQL用户授权IP了解其用户管理这篇文章的主要目的。好了,废话不多说,大家直接看下文...
    99+
    2024-04-02
  • ASP.NET MVC如何使用Identity增删改查用户
    这篇文章主要讲解了“ASP.NET MVC如何使用Identity增删改查用户”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“ASP.NET MVC如何使用Identity...
    99+
    2023-07-04
  • 如何进行MySQL新建用户、授权和删除用户以及修改密码操作
    这篇文章将为大家详细讲解有关如何进行MySQL新建用户、授权和删除用户以及修改密码操作,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。首先要声明一下:一般情况...
    99+
    2024-04-02
  • mysql查看用户授权信息的具体方法
    具体方法: 打开命令提示符 输入mysql -u root -p命令,然后回车 输入正确的密码,进入mysql命令行 查看用户授权信息 SHOW GRANTS FOR 'root'@'localhost'; 查...
    99+
    2022-06-01
    mysql 授权信息
  • MySql中添加用户、新建数据库、用户授权、删除用户以及修改密码的方法
    本篇内容介绍了“MySql中添加用户、新建数据库、用户授权、删除用户以及修改密码的方法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大...
    99+
    2024-04-02
  • Vue用户管理的增删改查功能详解
    目录1、向api请求发出查询用户列表数据,渲染表单数据2、通过v-model绑定查询数据,进行表单信息查询3、通过改变布尔值,来控制打开取消添加用户对话框4、通过动态绑定curren...
    99+
    2024-04-02
  • 如何利用PHP+Mysql实现增删改查
    这篇文章主要介绍了如何利用PHP+Mysql实现增删改查,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。PHP+Mysql实现增删改查PHP 是一种创建动态交互性站点的强有力的...
    99+
    2023-06-22
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作