linux 系统mariadb 数据库应用

linux 系统mariadb 数据库应用_第1张图片
linux 系统mariadb 数据库应用_第2张图片
linux 系统mariadb 数据库应用_第3张图片
linux 系统mariadb 数据库应用_第4张图片

一.部署mariadb数据库
1.安装mariadb -server数据库软件

        [root@room4pc09 桌面]# yum -y install mariadb-server
        已加载插件:fastestmirror, langpacks, ovl
        riadb                                                    | 4.3 kB     00:00     
        Loading mirror speeds from cached hostfile
        软件包 1:mariadb-server-5.5.60-1.el7_5.x86_64 已安装并且是最新版本
        无须任何处理

2.重启服务、开机自启

     [root@room4pc09 桌面]# systemctl restart mariadb      
     [root@room4pc09 桌面]# systemctl enable mariadb

3.进入mysql

[root@room4pc09 桌面]# mysql               //所以;结尾

4.1命令搜索 show

    MariaDB [(none)]> show databases;        //搜索数据库
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | test               |
    +--------------------+
    4 rows in set (0.00 sec)

4.2命令创建数据库 create 命令

MariaDB [(none)]> create database panfeilong;    //创建panfeilong数据库
Query OK, 1 row affected (0.00 sec)

    MariaDB [(none)]> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | panfeilong         |                    //创建的数据库
    | performance_schema |
    | test               |
    +--------------------+
5 rows in set (0.00 sec)

4.3删除数据库drop 命令

MariaDB [(none)]> drop database panfeilong;       //删除panfeilong数据
Query OK, 0 rows affected (0.00 sec)
    
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+

4.4退出数据库quit

MariaDB [(none)]> quit
Bye

4.5 数据库设置密码

[root@room4pc09 桌面]# mysqladmin -u root password '123456'  //设置密码123456
[root@room4pc09 桌面]# mysql -u root -p         //进入数据库root账号
Enter password:                                                //输入进入密码123456
[root@room4pc09 桌面]# mysql -u root -p123456    //免交互进入

4.6修改数据库配置文件,跳过网络监听

[root@room4pc09 桌面]# vim /etc/my.cnf
[mysqld]
skip-networking                    //增加一行,跳过网络监听

4.7进入数据库的小分库

 MariaDB [(none)]> use mysql      //进入数据库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]> 

4.8显示数据库的列表 show tables

MariaDB [mysql]> show tables;    //列出数据库表
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
| func                      |
| general_log               |
| help_category             |
| help_keyword              |

5.数据库导入

格式:mysql(命令) -u(用户) root(用户号) p123456(密码123456) nsd(数据库名称) <导入 (路径文件)

[root@room4pc09 /]# mysql -u root -p123456 nsd < /content/courses/rhce/rhel7.0/materials/users.sql        
 [root@room4pc09 /]# mysql -u root -p123456     //今天数据库
MariaDB [(none)]> use nsd                   //进入小分数据库
MariaDB [nsd]> show tables;                 //列出数据库内容
+---------------+
| Tables_in_nsd |
+---------------+
| base          |
| location      |
+---------------+
2 rows in set (0.00 sec)

MariaDB [nsd]> 

6.数据查询

MariaDB [nsd]> select * from base;                 //查询* 所有base数据
+------+---------+------------+
| id   | name    | password   |
+------+---------+------------+
|    1 | Tom     | 123        |
|    2 | Barbara | 456        |
|    3 | James   | solicitous |
|    4 | Smith   | tarena     |
|    5 | Barbara | pwd123     |
    MariaDB [nsd]> select id,name from base;       //只查收ID 和name名称
    +------+---------+
    | id   | name    |
    +------+---------+
    |    1 | Tom     |
    |    2 | Barbara |
    |    3 | James   |
    |    4 | Smith   |
    |    5 | Barbara |
    +------+---------+

MariaDB [nsd]> select * from base where name='tom';     // 只查询 base  里Tom
+------+------+----------+
| id   | name | password |
+------+------+----------+
|    1 | Tom  | 123      |
+------+------+----------+
MariaDB [nsd]> select * from location where city='beijing';          // 只查询 列city 里beijing
+------+---------+
| id   | city    |
+------+---------+
|    1 | Beijing |
+------+---------+
1 row in set (0.00 sec)

7.数据授权

MariaDB [(none)]> grant select on nsd.* to lisi@localhost identified by '123456';      //当lisi用户从本地localhost 登录,输入密码123456,将会获得库nsd所有表的查询权限
Query OK, 0 rows affected (0.01 sec)

查看MariaDB数据库中,用户表信息

riaDB [(none)]> select user,password from mysql.user;  
+------+-------------------------------------------+
| user | password                                  |
+------+-------------------------------------------+
| root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root |                                           |
| root |                                           |
| root |                                           |
|      |                                           |
|      |                                           |
| lisi | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+------+-------------------------------------------+

选择全部*从nsd中base里查找密码等于solicitous的

MariaDB [(none)]> select * from nsd.base where password='solicitous';
+------+-------+------------+
| id   | name  | password   |
+------+-------+------------+
|    3 | James | solicitous |
+------+-------+------------+

选择全部 从nsd中base里查找密码等于solicitous满足条件id=3

MariaDB [(none)]> select * from nsd.base where password='solicitous' or id='3';
+------+-------+------------+
| id   | name  | password   |
+------+-------+------------+
|    3 | James | solicitous |
+------+-------+------------+

选择全部nsd中:1.base满足条件名称等于‘Barbara’;2.,location 库 中满足条件city=Sunnyvale;3.满足条件base库.id=location库.id

MariaDB [nsd]> select *from base,location where base.name='Barbara' and location.city='Sunnyvale' and base.id=location.id;
+------+---------+----------+------+-----------+
| id   | name    | password | id   | city      |
+------+---------+----------+------+-----------+
|    5 | Barbara | pwd123   |    5 | Sunnyvale |
+------+---------+----------+------+-----------+
1 row in set (0.00 sec)

选择全部nsd中:1.base满足条件名称等于‘Barbara’;2.,location 库 中满足条件city=Sunnyvale;3.满足条件base库.id=location库.id,count(*)只显示数量

MariaDB [nsd]> select count(*)from base,location where base.name='Barbara' and location.city='Sunnyvale' and base.id=location.id;
+----------+
| count(*) |
+----------+
|        1 |
+----------+
1 row in set (0.02 sec)

插入base表记录第6行Barbara,密码123456

ariaDB [nsd]> insert base values(6,'Barbara',123456);
Query OK, 1 row affected (0.00 sec)

MariaDB [nsd]> select * from base
    -> ;
+------+---------+------------+
| id   | name    | password   |
+------+---------+------------+
|    1 | Tom     | 123        |
|    2 | Barbara | 456        |
|    3 | James   | solicitous |
|    4 | Smith   | tarena     |
|    5 | Barbara | pwd123     |
|    6 | Barbara | 123456     |
+------+---------+------------+

插入location 库表 第6行 Sunnyvale

MariaDB [nsd]> insert location values(6,'Sunnyvale');
Query OK, 1 row affected (0.00 sec)

MariaDB [nsd]> select * from location;
+------+-----------+
| id   | city      |
+------+-----------+
|    1 | Beijing   |
|    2 | Paris     |
|    3 | Sunnyvale |
|    4 | Berlin    |
|    5 | Sunnyvale |
|    6 | Sunnyvale |
+------+-----------+
6 rows in set (0.00 sec)

你可能感兴趣的:(数据库,数据库)