一文搞懂MySQL兄弟数据库MariaDB的安装和使用( 二 )


(2)本例中直接禁止了root的远程登录,但实际上有可能需要远程访问数据,这可以在上边的初始化操作中设置允许root管理员远程访问;然后在设置防火墙,使其放行对数据库服务的访问请求 。
[root@mariadb ~]# firewall-cmd --permanent --add-service=mysql success [root@mariadb ~]# firewall-cmd --reloadsuccess2.4 修改密码通过set密码可以修改root用户的密码,假设密码修改为888888
MariaDB [(none)]> set password=password('888888');Query OK, 0 rows affected (0.00 sec)MariaDB [(none)]> exitBye[root@mariadb ~]# mysql -uroot -pEnter password: 输入新密码登录修改密码后,退出再登录就只能用刚设置的新密码登录了 。
三、MariaDB账户管理为了保障数据库系统的安全性,以及让其他用户协同管理数据库,生产环境一般不用root管理员账户 。一般是以在MariaDB数据库管理系统中创建多个专用的数据库管理账户,然后再分配合理的权限,以满足工作需求 。
3.1 添加账户添加账户的语句为:“CREATE USER 用户名@主机名 IDENTIFIED BY '密码'; ”
MariaDB [(none)]> create user heima@localhost identified by 'heima';Query OK, 0 rows affected (0.00 sec)MariaDB [(none)]> use mysqlReading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedMariaDB [mysql]> select host,user,password from user where user='heima';+-----------+-------+-------------------------------------------+| host    | user| password                                |+-----------+-------+-------------------------------------------+| localhost | heima | *58613E96F5518C264EA39AA2A57D3DFEB191E343 |+-----------+-------+-------------------------------------------+1 row in set (0.00 sec)MariaDB [mysql]>exit创建用户后,存储在mysql数据库的user表中,可以进行查看 。
3.2 账户授权管理通过上边的方式创建的heima用户仅仅是一个普通用户,没有数据库的任何操作权限 。
[root@mariadb ~]# mysql -uheima -pheimaWelcome to the MariaDB monitor.Commands end with ; or g.Your MariaDB connection id is 15Server version: 5.5.64-MariaDB MariaDB ServerCopyright (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;+--------------------+| Database           |+--------------------+| information_schema |+--------------------+1 row in set (0.00 sec)MariaDB [(none)]> exitBye[root@mariadb ~]# 我们用heima账户登录,通过查询看不到mysql数据库,说明该用户连数据库查看的权限都没有 。
3.2.1 账号授权(1)grant授权语句
授权使用grant语句,语法格式为:"grant 权限 on 数据库.表名称 to 账户名@主机名" 。
举几个例子:

  • 对某个特定数据库中的特定表单给予授权
GRANT 权限ON 数据库.表单名称TO 账户名@主机名
  • 对某个特定数据库中的所有表单给予授权
GRANT 权限 ON 数据库.*TO 账户名@主机名
  • 对所有数据库及所有表单给予授权
GRANT 权限 ON.TO 账户名@主机名
  • 对某个数据库中的所有表单给予多个授权
GRANT 权限1,权限2 ON 数据库.*TO 账户名@主机 名
  • 对所有数据库及所有表单给予全部授权
GRANT ALL PRIVILEGES ON .TO 账户名@主机
(2)对heima账户授权
用root管理员账户登录,通过grant语句给heima用户对msyql数据库user表的增删改查的授权:
MariaDB [(none)]> show grants for heima@localhost;+------------------------------+| Grants for heima@localhost   |+------------------------------+| GRANT USAGE ON *.* TO 'heima'@'localhost' IDENTIFIED BY PASSWORD '*58613E96F5518C264EA39AA2A57D3DFEB191E343' |+------------------------------+1 row in set (0.01 sec)MariaDB [(none)]> grant select,update,delete,insert on mysql.user to heima@localhost;Query OK, 0 rows affected (0.00 sec)MariaDB [(none)]> show grants for heima@localhost;               +-----------------+| Grants for heima@localhost     |+----------------------------+| GRANT USAGE ON *.* TO 'heima'@'localhost' IDENTIFIED BY PASSWORD '*58613E96F5518C264EA39AA2A57D3DFEB191E343' || GRANT SELECT, INSERT, UPDATE, DELETE ON `mysql`.`user` TO 'heima'@'localhost'  |+-------------------------------+2 rows in set (0.00 sec)MariaDB [(none)]>
通过show grants命令可以看到对用户授予了哪些权限 。


推荐阅读