练习:安装mariadb服务,并创两个用户,用户A可以管理表A,用户B可以管理所有表。
一、安装服务
# yum install -y mariadb mariadb-server
二、初始化mariadb数据库
[root@localhost ~]# 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):
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
New password:
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] n
... skipping.
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] y
- 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] y
... 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!
三、创建表格
[root@localhost ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.65-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; //原本的数据库
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
MariaDB [(none)]> create database testDB; //创建测试数据库
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> use testDB; //使用该数据库
Database changed
//创建A、B表
MariaDB [testDB]> create table A(id int not null primary key,name varchar(50),addr varchar(255));
Query OK, 0 rows affected (0.00 sec)
MariaDB [testDB]> create table B(id int not null primary key,name varchar(50),addr varchar(255));
Query OK, 0 rows affected (0.00 sec)
MariaDB [testDB]> show tables; //查看该数据库中的表
+------------------+
| Tables_in_testDB |
+------------------+
| A |
| B |
+------------------+
2 rows in set (0.00 sec)
四、创建用户
语法:create user 用户名 identified by ‘密码’;
MariaDB [(none)]> create user userA identified by '123456';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> create user userB identified by '123456';
Query OK, 0 rows affected (0.00 sec)
五、给予权限
语法:grant 权限 on 数据库.* to ‘用户名’@‘登录主机’ identified by “密码”;
权限可以是查询、插入、修改、删除(select,insert,update,delete);
登录主机可以是本地主机和任何机器(localhost,%)
MariaDB [(none)]> grant all privileges on testDB.A to 'userA'@'%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> grant all privileges on *.* to 'userB'@'%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
六、测试
查看用户A可管理的表格:
[root@localhost ~]# mysql -uuserA -p123456;
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 6
Server version: 5.5.65-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; //列出数据库;
+--------------------+
| Database |
+--------------------+
| information_schema |
| testDB |
+--------------------+
2 rows in set (0.00 sec)
MariaDB [(none)]> use testDB; //使用testDB数据库;
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 [testDB]> show tables; //列出testDB数据库中可管理的表格
+------------------+
| Tables_in_testDB |
+------------------+
| A |
+------------------+
1 row in set (0.00 sec)
MariaDB [testDB]>
查看用户B可管理的表格:
[root@localhost ~]# mysql -uuserB -p123456;
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 7
Server version: 5.5.65-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;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| testDB |
+--------------------+
4 rows in set (0.01 sec)
MariaDB [(none)]> use testDB;
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 [testDB]> show tables; //可看到这里有两个创建的表格
+------------------+
| Tables_in_testDB |
+------------------+
| A |
| B |
+------------------+
2 rows in set (0.00 sec)
版权声明:本文为zhang_ZERO原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。