數據庫概述(1)

一:數據庫簡介

1.1:什麼是數據庫

在這裏插入圖片描述

在這裏插入圖片描述
數據庫就是一個存儲數據的倉庫。
html

1.2 :關係型數據庫

在這裏插入圖片描述

1.2.1:RDBMS概念

RDBMS 即關係數據庫管理系統(Relational Database Management System)的特色: [rɪˈleɪʃənl]mysql

關係型數據庫:是創建在關係模型基礎上的數據庫,藉助於集合代數等數學概念和方法來處理數據庫中的數據。linux

這種所謂的"關係型"能夠理解爲"表格"的概念, 一個關係型數據庫由一個或數個表格組成, 如圖所示的一個表格:
在這裏插入圖片描述
c++

1.2.2: RDBMS特色

  • 數據以表格的形式出現
  • 每行爲各類記錄名稱
  • 每列爲記錄名稱所對應的數據域
  • 許多的行和列組成一張表單
  • 若干的表單組成database

1.2.4:RDBMS 術語

在咱們開始學習MySQL 數據庫前,讓咱們先了解下RDBMS的一些術語:sql

  • 數據庫: 數據庫是一些關聯表的集合。
  • 數據表: 表是數據的矩陣。在一個數據庫中的表看起來像一個簡單的電子表格。
  • 列: 一列(數據元素) 包含了相同類型的數據, 例如郵政編碼的數據。
  • 行:一行(=元組,或記錄)是一組相關的數據,例如一條用戶訂閱的數據。
  • 冗餘:存儲兩倍數據,冗餘下降了性能,但提升了數據的安全性。
  • 主鍵:主鍵是惟一的。一個數據表中只能包含一個主鍵。你可使用主鍵來查詢數據。
  • 外鍵:外鍵用於關聯兩個表。
  • 複合鍵:複合鍵(組合鍵)將多個列做爲一個索引鍵,通常用於複合索引。
  • 索引:使用索引可快速訪問數據庫表中的特定信息。索引是對數據庫表中一列或多列的值進行排序的一種結構。相似於書籍的目錄。

1.3: mysql,oracle,sqlserver功能和應用場合

1.3.1:mysql

在這裏插入圖片描述mysql主要用於大型門戶,例如搜狗、新浪等,它主要的優點就是開放源代碼,由於開放源代碼這個數據庫是免費的,它如今是甲骨文公司的產品。數據庫

1.3.2:oracle

在這裏插入圖片描述
oracle主要用於銀行、鐵路、飛機場等。該數據庫功能強大,軟件費用高。也是甲骨文公司的產品。
centos

1.3.3: sqlserver

在這裏插入圖片描述
sql server是微軟公司的產品,主要應用於大中型企業,如聯想、方正等。
安全

1.4:數據庫服務器,數據庫,表與記錄的關係

1.4.1:數據庫服務器(硬件)

在這裏插入圖片描述

1.4.2:數據庫(軟件)

在這裏插入圖片描述在這裏插入圖片描述

1.4.3:表

在這裏插入圖片描述

1.4.4:記錄

在這裏插入圖片描述數據庫服務器,數據庫,表,記錄的關係以下:
在這裏插入圖片描述
bash

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/ 

在這裏插入圖片描述

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 數據庫名;
舉例:

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
相關文章
相關標籤/搜索