mariadb数据库用户管理(创建、赋权、)
数据库查看当前用户 select user();
MariaDB [(none)]> select user(); +----------------+ | user() | +----------------+ | root@localhost | +----------------+ 1 row in set (0.00 sec)
数据库新建用户 (--创建了一个名为:test 密码为:1234 的用户)
1 2 | MariaDB [(none)]> create user 'test' @ 'localhost' identified by '1234' ; Query OK, 0 rows affected (0.00 sec) |
查询用户
MariaDB [zabbix]> select user,host from mysql.user; +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | root | ::1 | | root | localhost | | test | localhost | | zabbix | localhost | +--------+-----------+ 5 rows in set (0.00 sec)
修改用户密码
方法1,密码实时更新;修改用户“test”的密码为“5678”
1 2 | MariaDB [mysql]> set password for test @localhost =password( '5678' ); Query OK, 0 rows affected (0.00 sec) |
MariaDB [mysql]> exit
Bye
[root@192 ~]# mysql -utest -p5678
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 271
Server version: 10.2.36-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)]> select user();
+----------------+
| user() |
+----------------+
| test@localhost |
+----------------+
1 row in set (0.00 sec)
方法2 ,需要刷新;修改用户“test”的密码为“1234”
1 2 3 4 5 6 | MariaDB [(none)]> update mysql.user set password =password( 'root' ) where user= 'test' ; 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) |
方法3,命令行修改 mysqladmin -u用户名 -p旧密码 password 新密码
[root@192 ~]# mysqladmin -uroot -p1 password root [root@192 ~]# mysql -uroot -proot Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 95 Server version: 10.2.36-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)]>
分配用户权限
授予用户test通过外网IP对所有数据库所有表的全部权限
1 2 | MariaDB [(none)]> grant all privileges on *.* to 'test' @ '%' identified by '1234' ; Query OK, 0 rows affected (0.00 sec) |
注意:修改完权限以后 一定要刷新服务,或者重启服务,刷新服务用:flush privileges;
查看用户权限 show grants for test;
1 2 3 4 5 6 7 | MariaDB [(none)]> show grants for test ; +--------------------------------------------------------------------------------------------------------------+ | Grants for test @% | +--------------------------------------------------------------------------------------------------------------+ | GRANT ALL PRIVILEGES ON *.* TO 'test' @ '%' IDENTIFIED BY PASSWORD '*A4B6157319038724E3560894F7F932C8886EBFCF' | +--------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) |
查看自己的权限 show grants
1 2 3 4 5 6 7 8 | MariaDB [(none)]> show grants; +----------------------------------------------------------------------------------------------------------------------------------------+ | Grants for root@localhost | +----------------------------------------------------------------------------------------------------------------------------------------+ | GRANT ALL PRIVILEGES ON *.* TO 'root' @ 'localhost' IDENTIFIED BY PASSWORD '*81F5E21E35407D884A6CD4A731AEBFB6AF209E1B' WITH GRANT OPTION | | GRANT PROXY ON '' @ '%' TO 'root' @ 'localhost' WITH GRANT OPTION | +----------------------------------------------------------------------------------------------------------------------------------------+ 2 rows in set (0.00 sec) |
回收用户权限 revoke
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | MariaDB [(none)]> show grants for test ; +--------------------------------------------------------------------------------------------------------------+ | Grants for test @% | +--------------------------------------------------------------------------------------------------------------+ | GRANT ALL PRIVILEGES ON *.* TO 'test' @ '%' IDENTIFIED BY PASSWORD '*A4B6157319038724E3560894F7F932C8886EBFCF' | +--------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) MariaDB [(none)]> revoke all on *.* from test @ '%' ; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> show grants for test ; +-----------------------------------------------------------------------------------------------------+ | Grants for test @% | +-----------------------------------------------------------------------------------------------------+ | GRANT USAGE ON *.* TO 'test' @ '%' IDENTIFIED BY PASSWORD '*A4B6157319038724E3560894F7F932C8886EBFCF' | +-----------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) |
删除用户 drop user test;
1 2 | MariaDB [(none)]> drop user test ; Query OK, 0 rows affected (0.00 sec) |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· AI 智能体引爆开源社区「GitHub 热点速览」