欢迎投稿

今日深度:

mariadb,

mariadb,


许多生产环境中需要使用到站库分离技术(网站和数据库不在一个服务器上),所以需要root管理员远程访问数据库,可在初始化操作中允许root管理员远程访问,并设置防火墙策略。数据库服务程序默认占用3306端口,在防火墙策略管理规则中同一名称为mysql

实验1:安装mariadb数据库服务,初始化数据库,root管理员登录数据库,修改数据库管理员密码

[root@localhost ~]# yum install -y mariadb mariadb-server  
[root@localhost ~]# systemctl start mariadb
[root@localhost ~]# systemctl enable mariadb
ln -s '/usr/lib/systemd/system/mariadb.service' '/etc/systemd/system/multi-user.target.wants/mariadb.service'
[root@localhost ~]# mysql_secure_installation 
/bin/mysql_secure_installation: line 379: find_mysql_client: command not found

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] y                              禁止远程登录管理员账户
 ... 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] 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 -u root -p              -u 指定登录账户 -p 密码提示符
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.35-MariaDB MariaDB Server
MariaDB [(none)]> SET password = PASSWORE('linuxprobe');
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 'PASSWORE('linuxprobe')' at line 1
MariaDB [(none)]> SET password = PASSWORD('linuxprobe');
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> exit
Bye
[root@localhost ~]# mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 5.5.35-MariaDB MariaDB Server

实验2:管理用户以及授权

localhost主机中创建名为luck密码为linuxprobe的账户

 

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]> CREATE user luck@localhost IDENTIFIED BY 'linuxprobe';
Query OK, 0 rows affected (0.01 sec)

 

查询luck账户的主机名,用户名,进过加密的密码值信息

MariaDB [mysql]> SELECT host,user,password FROM user WHERE user='luck';
+-----------+------+-------------------------------------------+
| host      | user | password                                  |
+-----------+------+-------------------------------------------+
| localhost | luck | *55D9962586BE75F4B7D421E6655973DB07D6869F |
+-----------+------+-------------------------------------------+
1 row in set (0.00 sec)

针对mysql数据库中的user表单向账户luke授予查询、更新、删除以及插入等权限。

MariaDB [mysql]> GRANT SELECT,UPDATE,DELETE,INSERT ON mysql.user TO luck@localhost;
Query OK, 0 rows affected (0.00 sec)

MariaDB [mysql]> SHOW GRANTS FOR luck@localhost;
+-------------------------------------------------------------------------------------------------------------+
| Grants for luck@localhost                                                                                   |
+-------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'luck'@'localhost' IDENTIFIED BY PASSWORD '*55D9962586BE75F4B7D421E6655973DB07D6869F' |
| GRANT SELECT, INSERT, UPDATE, DELETE ON `mysql`.`user` TO 'luck'@'localhost'                                |
+-------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

授权操作执行后来查看下luke用户的权限,在删除查询,更新权限。

MariaDB [mysql]> show GRANTS FOR luck@localhost;
+-------------------------------------------------------------------------------------------------------------+
| Grants for luck@localhost                                                                                   |
+-------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'luck'@'localhost' IDENTIFIED BY PASSWORD '*55D9962586BE75F4B7D421E6655973DB07D6869F' |
| GRANT INSERT, DELETE ON `mysql`.`user` TO 'luck'@'localhost'                                                |
+-------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

实验3:创建数据库与表单

创建一个名为linuxprobe的数据库;在新建的linuxprobe数据库中创建表单mybook,然后进行表单的初始化,分别定义3个字段项,其中,长度为15个字符的字符型字段name用来存放图书名称,整型字段price和pages分别存储图书的价格和页数。

 

MariaDB [mysql]> CREATE DATABASE linuxprobe;
Query OK, 1 row affected (0.00 sec)

MariaDB [mysql]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| linuxprobe         |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)

MariaDB [mysql]> use linuxprobe;
Database changed
MariaDB [linuxprobe]> create table mybook(name char(15), price int,pages int);
Query OK, 0 rows affected (0.00 sec)

MariaDB [linuxprobe]> show tables;
+----------------------+
| Tables_in_linuxprobe |
+----------------------+
| mybook               |
+----------------------+
1 row in set (0.00 sec)

MariaDB [linuxprobe]> DESCRIBE mybook;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| name  | char(15) | YES  |     | NULL    |       |
| price | int(11)  | YES  |     | NULL    |       |
| pages | int(11)  | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+
3 rows in set (0.00 sec)

实验4:管理表单及数据

我们使用该命令插入一条图书信息,其中书名为linuxprobe,价格和页数分别是60元和518页。

MariaDB [linuxprobe]> INSERT INTO mybook(name,price,pages) VALUES('linuxprobe','60','518');
Query OK, 1 row affected (0.00 sec)

MariaDB [linuxprobe]> select * from mybook;
+------------+-------+-------+
| name       | price | pages |
+------------+-------+-------+
| linuxprobe |    60 |   518 |
+------------+-------+-------+
1 row in set (0.00 sec)

使用update命令将刚才插入的linuxprobe图书信息的价格修改为55元,在使用select命令查看该图书的名称和定价信息。

 

MariaDB [linuxprobe]> UPDATE mybook SET price=55;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MariaDB [linuxprobe]> select name,price FROM mybook;
+------------+-------+
| name       | price |
+------------+-------+
| linuxprobe |    55 |
+------------+-------+
1 row in set (0.00 sec)

使用delete命令删除某个数据表单中的内容。

MariaDB [linuxprobe]> DELETE FROM mybook
    -> ;
Query OK, 1 row affected (0.00 sec)

MariaDB [linuxprobe]> select * FROM mybook;
Empty set (0.00 sec)

先使用insert插入命令依次插入4条图书信息,分别在mybook表单中查找出价格大于75元或价格不等于80元的图书

MariaDB [linuxprobe]> INSERT INTO mybook(name,price,pages) VALUES('linuxprobe1','45','345');
Query OK, 1 row affected (0.00 sec)

MariaDB [linuxprobe]> INSERT INTO mybook(name,price,pages) VALUES('linuxprobe2','50','678');
Query OK, 1 row affected (0.00 sec)

MariaDB [linuxprobe]> INSERT INTO mybook(name,price,pages) VALUES('linuxprobe3','56','897');
Query OK, 1 row affected (0.00 sec)

MariaDB [linuxprobe]> INSERT INTO mybook(name,price,pages) VALUES('linuxprobe4','80','897');
Query OK, 1 row affected (0.00 sec)

MariaDB [linuxprobe]> select * FROM mybook;
+-------------+-------+-------+
| name        | price | pages |
+-------------+-------+-------+
| linuxprobe1 |    45 |   345 |
| linuxprobe2 |    50 |   678 |
| linuxprobe3 |    56 |   897 |
| linuxprobe4 |    80 |   897 |
+-------------+-------+-------+
4 rows in set (0.00 sec)
MariaDB [linuxprobe]> SELECT * FROM mybook WHERE price>75;
+-------------+-------+-------+
| name        | price | pages |
+-------------+-------+-------+
| linuxprobe4 |    80 |   897 |
+-------------+-------+-------+
1 row in set (0.00 sec)

MariaDB [linuxprobe]> SELECT * FROM mybook WHERE price!=80;
+-------------+-------+-------+
| name        | price | pages |
+-------------+-------+-------+
| linuxprobe1 |    45 |   345 |
| linuxprobe2 |    50 |   678 |
| linuxprobe3 |    56 |   897 |
+-------------+-------+-------+
3 rows in set (0.00 sec)

实验5:数据库的备份及恢复

[root@localhost ~]# mysqldump -u root -p linuxprobe > /root/linuxprobedb.dump
Enter password: 
[root@localhost ~]# mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 15
Server version: 5.5.35-MariaDB MariaDB Server

Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.

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

MariaDB [(none)]> DROP database linuxprobe;
Query OK, 1 row affected (0.02 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

MariaDB [(none)]> exit
Bye
[root@localhost ~]# mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 16
Server version: 5.5.35-MariaDB MariaDB Server

Copyright (c) 2000, 2013, Oracle, Monty Program 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 linuxprobe;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> exit
Bye
[root@localhost ~]# mysql -u root -p linuxprobe < /root/linuxprobedb.dump 
Enter password: 
[root@localhost ~]# mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 18
Server version: 5.5.35-MariaDB MariaDB Server

Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.

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

MariaDB [(none)]> use linuxprobe;
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 [linuxprobe]> DESCRIBE mybook;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| name  | char(15) | YES  |     | NULL    |       |
| price | int(11)  | YES  |     | NULL    |       |
| pages | int(11)  | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+
3 rows in set (0.00 sec)

1.RHEL 7系统为何选择使用MariaDB替代MySQL数据库管理系统?

2.初始化MariaDB或MySQL数据库管理系统的命令是什么?

3.用来查看已有数据库或数据表单的命令是什么?

4.切换至某个指定数据库的命令是什么?

5.若想针对某个账户进行授权或取消授权操作,应该执行什么命令?

6.若只想查看mybook表单中的name字段,应该执行什么命令?

7.若只想查看mybook表单中价格大于75元的图书信息,应该执行什么命令?

8. 要想把linuxprobe数据库中的内容导出为一个文件(保存到root管理员的家目录中),应该执行什么命令?

 

www.htsjk.Com true http://www.htsjk.com/mariadb/24918.html NewsArticle mariadb, 许多生产环境中需要使用到站库分离技术(网站和数据库不在一个服务器上),所以需要root管理员远程访问数据库,可在初始化操作中允许 root 管理员远程访问,并设置防火墙...
相关文章
    暂无相关文章
评论暂时关闭