数据库概述(1)

一:数据库简介

1.1:什么是数据库

在这里插入图片描述

在这里插入图片描述
数据库就是一个存储数据的仓库。

1.2 :关系型数据库

在这里插入图片描述

1.2.1:RDBMS概念

RDBMS 即关系数据库管理系统(Relational Database Management System)的特点: [rɪˈleɪʃənl]

关系型数据库:是建立在关系模型基础上的数据库,借助于集合代数等数学概念和方法来处理数据库中的数据。

这种所谓的"关系型"可以理解为"表格"的概念, 一个关系型数据库由一个或数个表格组成, 如图所示的一个表格:
在这里插入图片描述

1.2.2: RDBMS特点

  • 数据以表格的形式出现
  • 每行为各种记录名称
  • 每列为记录名称所对应的数据域
  • 许多的行和列组成一张表单
  • 若干的表单组成database

1.2.4:RDBMS 术语

在我们开始学习MySQL 数据库前,让我们先了解下RDBMS的一些术语:

  • 数据库: 数据库是一些关联表的集合。
  • 数据表: 表是数据的矩阵。在一个数据库中的表看起来像一个简单的电子表格。
  • 列: 一列(数据元素) 包含了相同类型的数据, 例如邮政编码的数据。
  • 行:一行(=元组,或记录)是一组相关的数据,例如一条用户订阅的数据。
  • 冗余:存储两倍数据,冗余降低了性能,但提高了数据的安全性。
  • 主键:主键是唯一的。一个数据表中只能包含一个主键。你可以使用主键来查询数据。
  • 外键:外键用于关联两个表。
  • 复合键:复合键(组合键)将多个列作为一个索引键,一般用于复合索引。
  • 索引:使用索引可快速访问数据库表中的特定信息。索引是对数据库表中一列或多列的值进行排序的一种结构。类似于书籍的目录。

1.3: mysql,oracle,sqlserver功能和应用场合

1.3.1:mysql

在这里插入图片描述mysql主要用于大型门户,例如搜狗、新浪等,它主要的优势就是开放源代码,因为开放源代码这个数据库是免费的,它现在是甲骨文公司的产品。

1.3.2:oracle

在这里插入图片描述
oracle主要用于银行、铁路、飞机场等。该数据库功能强大,软件费用高。也是甲骨文公司的产品。

1.3.3: sqlserver

在这里插入图片描述
sql server是微软公司的产品,主要应用于大中型企业,如联想、方正等。

1.4:数据库服务器,数据库,表与记录的关系

1.4.1:数据库服务器(硬件)

在这里插入图片描述

1.4.2:数据库(软件)

在这里插入图片描述在这里插入图片描述

1.4.3:表

在这里插入图片描述

1.4.4:记录

在这里插入图片描述数据库服务器,数据库,表,记录的关系如下:
在这里插入图片描述

1.5: 数据库存储引擎种类

在这里插入图片描述
在这里插入图片描述

1.5.1 数据库存储引擎 概念

存储引擎:即表类型(table_type)
用户可以根据应用的需求选择如何来存储数据、索引、是否使用事务等。

选择合适的存储引擎往往能够有效的提高数据库的性能和数据的访问效率。

MySQL支持很多存储引擎,包括MyISAM、InnoDB、BDB、MEMORY、MERGE、EXAMPLE、NDB Cluster、ARCHIVE等。

其中InnoDB和BDB支持事务安全

它还支持一些第三方的存储引擎,例如TokuDB(高写性能高压缩存储引擎)、Infobright(列式存储引擎)。

1.5.2 查看存储引擎:

show engines;

在这里插入图片描述

1.5.3 各个存储引擎的对比

详情查看:
在这里插入图片描述

二:linux平台下安装与配置mysql

2.1:yum安装

操作系统:centos7.7

[root@localhost packages]# yum -y install mariadb mariadb-server

在这里插入图片描述
验证:

[root@localhost ~]# rpm -qa |grep mariadb
mariadb-libs-5.5.65-1.el7.x86_64
mariadb-server-5.5.65-1.el7.x86_64
mariadb-5.5.65-1.el7.x86_64

管理服务(启动/停止):

设置开机自启
[root@localhost ~]# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
启动服务
[root@localhost ~]# systemctl start mariadb
停止服务
[root@localhost ~]# systemctl stop mariadb

验证服务端口:

[root@localhost ~]# ss -lptnu|grep 3306
tcp    LISTEN     0      50        *:3306                  *:*                   users:(("mysqld",pid=2283,fd=15))

登录数据库:

默认是没有密码的!

[root@localhost ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
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)]>

2.2:源码包编译参数与安装

2.2.1 安装编译源码所需的工具和库:

MySQL 5.5之后的源码包版本,安装方式采用CMake工具编译进行安装,因此在安装最新版MySQL之前,需要提前安装它。

CMake是一个跨平台、开源软件构建系统,用于控制软件编译过程及生成独立的配置文件(makefile或者project)

CMake官网https://cmake.org/

yum -y install cmake gcc gcc-c++ ncurses-devel

2.2.2 下载源代码:

下载路径:
https://downloads.mysql.com/archives/community/
在这里插入图片描述
上传源码包:

[root@localhost src]# pwd
/usr/local/src
[root@localhost src]# ll
总用量 34352
-rw-r--r--. 1 root root 35174149 10月  7 13:39 mysql-5.6.10.tar.gz

解压:

[root@localhost src]# tar zxvf mysql-5.6.10.tar.gz
[root@localhost src]# cd mysql-5.6.10

2.2.3 编译安装:

[root@localhost mysql-5.6.10]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
> -DDEFAULT_CHARSET=utf8 \
> -DDEFAULT_COLLATION=utf8_general_ci \
> -DWITH_INNOBASE_STORAGE_ENGINE=1 \
> -DMYSQL_USER=mysql \
> -DMYSQL_TCP_PORT=3306

编译参数说明:

-DCMAKE_INSTALL_PREFIX=指向mysql安装目录
-DDEFAULT_CHARSET=指定数据库默认的字符集
-DDEFAULT_COLLATION=设定默认排序规则(utf8_general_ci快速/utf8_unicode_ci准确)[kəˈleɪʃn]
-DWITH_INNOBASE_STORAGE_ENGINE=1 启用innodb存储引擎
-DMYSQL_USER=mysql 指定mysql用户
-DMYSQL_TCP_PORT=3306 指定服务监听的端口号

当出现下面的截图,就可以执行make&&make install 了:
在这里插入图片描述
编译过程有点长

make -j2 && make install

注:-j 用来指定CPU核心数,可加快编译速度,不加也可以

【编译有错误后,执行make clean ,然后要删除 rm CMakeCache.txt ,才能重新编译】

在这里插入图片描述

官网编译参数详解

2.2.4 Mysql初始化数据目录:

Mysql启动前需要进行数据的初始化。

创建数据目录
mkdir -p /data/mysql
[root@localhost scripts]# pwd
/usr/local/mysql/scripts
[root@localhost scripts]# ./mysql_install_db --basedir=/usr/local/mysql --user=mysql --datadir=/data/mysql/ 

在这里插入图片描述报错处理:
在初始化MySQL数据库时出现提示FATAL ERROR: please install the following Perl modules before executing

yum -y install autoconf

2.2.5 手动启动服务

修改my.cnf

[root@localhost mysql]# pwd
/usr/local/mysql

[root@localhost mysql]# cat my.cnf |grep -v "^#"|sed '/^$/d'
[mysqld]
basedir = /usr/local/mysql
datadir = /data/mysql
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 

[root@localhost bin]# ./mysqld_safe --defaults-file=/usr/local/mysql/my.cnf &
[1] 36632

在这里插入图片描述

2.2.6 检测服务是否存活

[root@localhost mysql]# /usr/local/mysql/bin/mysqladmin ping 
mysqld is alive

2.2.7 手动停止服务

[root@localhost mysql]# /usr/local/mysql/bin/mysqladmin shutdown
201007 16:09:39 mysqld_safe mysqld from pid file /data/mysql/localhost.localdomain.pid ended
[1]+  完成                  ./mysqld_safe --defaults-file=/usr/local/mysql/my.cnf(工作目录:/usr/local/mysql/bin)
(当前工作目录:/usr/local/mysql)

2.2.8 使用脚本管理服务

[root@localhost support-files]# pwd
/usr/local/mysql/support-files
脚本用法:
[root@localhost support-files]# ./mysql.server 
Usage: mysql.server  {
    
    start|stop|restart|reload|force-reload|status}  [ MySQL server options ]

查看服务状态:
[root@localhost support-files]# ./mysql.server status
 ERROR! MySQL is not running

启动服务:
[root@localhost support-files]# ./mysql.server start
Starting MySQL. SUCCESS! 

停止服务:
[root@localhost support-files]# ./mysql.server stop
Shutting down MySQL. SUCCESS! 

2.3:设置root密码

[root@localhost support-files]# /usr/local/mysql/bin/mysqladmin  -uroot password "123"
/usr/local/mysql/bin/mysqladmin: connect to server at 'localhost' failed
error: 'Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)'
Check that mysqld is running and that the socket: '/tmp/mysql.sock' exists!

报错处理:
查看socket路径
在这里插入图片描述
加上参数:-S ,指定socket路径

[root@localhost support-files]# /usr/local/mysql/bin/mysqladmin  -uroot password "123" -S /var/lib/mysql/mysql.sock 
Warning: Using a password on the command line interface can be insecure.

登录mysql:
在这里插入图片描述

三:mysql数据库的管理

3.1:库的建立与删除

3.1.1 直接创建

create database 数据库名;

CREATE DATABASE `mydb` CHARACTER SET utf8 COLLATE utf8_general_ci;

举例:

MySQL [(none)]> create database wg;
Query OK, 1 row affected (0.00 sec)

3.1.2 如果不存在则创建(加了判断)

create database if not exists 数据库名;

举例:

MySQL [(none)]> create database if not exists wg;
Query OK, 1 row affected, 1 warning (0.00 sec)

3.1.3 直接删除数据库

drop database 数据库名;

举例:

MySQL [(none)]> drop database wg;
Query OK, 0 rows affected (0.00 sec)

3.1.4 如果存在则删除数据库(加判断)

drop database if exists 数据库名;

举例:

MySQL [(none)]> drop database if exists wg;
Query OK, 0 rows affected, 1 warning (0.00 sec)

3.2 查看当前的所有数据库

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

3.3 使用指定数据库

use 数据库名;

MySQL [(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

猜你喜欢

转载自blog.csdn.net/zhangshaohuas/article/details/108947147