配置一个mariadb数据库《二》

                                                         mariadb 配置一个数据库

 

案例4:配置一个数据库

4.1 问题

本例要求在虚拟机server0上部署 MariaDB 数据库,具体要求如下:

  1. 此数据库系统只能被 localhost 访问
  2. 新建一个数据库名为 Contacts,其中应该包含来自数据库复制的内容,复制文件的 URL 为:http://classroom/pub/materials/users.sql
  3. 除了 root 用户,此数据库只能被用户 Raikon 查询,此用户的密码为atenorth
  4. root用户的密码为 atenorth

4.2 方案

为数据库账号修改密码:

  1. mysqladmin  [-u用户名]  [-p[旧密码]]  password  '新密码'

导入/恢复到数据库:

  1. mysql  [-u用户名]  [-p[密码]]  数据库名  <  备份文件.sql

为数据库用户授权/撤销权限:

  1. grant  权限1,权限2...  on  库名.表名  to  用户名@客户机地址  identified  by '密码';
  2. revoke 权限1,权限2... on  库名.表名  from  用户名@客户机地址;

4.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:禁止mariadb服务提供网络监听(只服务于本机)

1)修改配置文件

  1. [root@server0 ~]# vim  /etc/my.cnf
  2. [mysqld]
  3. skip-networking                                          //跳过网络

2)重启mariadb服务

  1. [root@server0 ~]# systemctl  restart  mariadb              //重启服务

3)确认结果

  1. [root@server0 ~]# netstat  -anptu  |  grep  :3306          //已经不提供端口监听
  2. [root@server0 ~]# pgrep  -l  mysqld                      //但进程仍在
  3. 3127 mysqld_safe
  4. 3297 mysqld

步骤二:配置数据库管理密码

1)使用mysqladmin为用户root设置密码

原管理账号root的密码为空,因此无需验证旧密码:

  1. [root@server0 ~]# mysqladmin  -u  root  password  'atenorth'

2)验证新密码是否可用

root使用空密码从本机连接将会失败:

  1. [root@server0 ~]# mysql  -uroot
  2. ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

必须指定正确的新密码才能连接成功:

  1. [root@server0 ~]# mysql  -uroot  -patenorth
  2. Welcome to the MariaDB monitor.  Commands end with ; or \g.
  3. Your MariaDB connection id is 4
  4. Server version: 5.5.35-MariaDB MariaDB Server
  5. Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.
  6. Type 'help;' or '\hfor help. Type '\c' to clear the current input statement.
  7. .. ..

步骤三:建Contacts库并导入备份数据

1)创建新库Contacts,并退出操作环境

  1. MariaDB [(none)]> CREATE  DATABASE  Contacts;
  2. Query OK, 1 row affected (0.00 sec)
  3. MariaDB [(none)]> QUIT
  4. Bye

2)下载指定的数据库备份

  1. [root@server0 ~]# wget  http://classroom.example.com/pub/materials/users.sql
  2. --2016-11-26 19:00:37--  http://classroom.example.com/pub/materials/users.sql
  3. Resolving classroom.example.com (classroom.example.com)... 172.25.254.254
  4. Connecting to classroom.example.com (classroom.example.com)|172.25.254.254|:80... connected.
  5. HTTP request sent, awaiting response... 200 OK
  6. Length: 2634 (2.6K) [application/sql]
  7. Saving to: ‘users.sql’
  8. 100%[=====================>] 2,634       --.-K/s   in 0s      
  9. 2016-11-26 19:00:37 (269 MB/s) - ‘users.sql’ saved [2634/2634]
  10. [root@server0 ~]# ls  -lh  users.sql                      //确认下载的文件
  11. -rw-r--r--. 1 root root 2.6K Mar 31  2016 users.sql

3)导入数据库

  1. [root@server0 ~]# mysql  -uroot  -patenorth  Contacts  <  users.sql

4)重新连入操作环境,确认导入结果

  1. [root@server0 ~]# mysql  -uroot  -patenorth
  2. .. ..
  3. MariaDB [(none)]> USE  Contacts;                     //使用指定库
  4. Database changed
  5. MariaDB [Contacts]> SHOW  TABLES;                  //列出有哪些表
  6. +--------------------+
  7. | Tables_in_Contacts |
  8. +--------------------+
  9. | base               |
  10. | location           |
  11. +--------------------+
  12. 2 rows in set (0.00 sec)

步骤四:为Contacts库授权

1)允许用户Raikon从本机访问,具有查询权限,密码为atenorth

  1. MariaDB [Contacts]> GRANT  select  ON  Contacts.*  TO  Raikon@localhost  IDENTIFIED BY  'atenorth';
  2. Query OK, 0 rows affected (0.00 sec)

2)退出操作环境

  1. MariaDB [Contacts]> QUIT
  2. Bye
  3. [root@server0 ~]#
posted @ 2019-11-26 17:19  云计算(互联网)  Views(174)  Comments(0Edit  收藏  举报