目錄
- 一:資料庫簡介
- 1.1:什么是資料庫
- 1.2 :關系型資料庫
- 1.2.1:RDBMS概念
- 1.2.2: RDBMS特點
- 1.2.4:RDBMS 術語
- 1.3: mysql,oracle,sqlserver功能和應用場合
- 1.3.1:mysql
- 1.3.2:oracle
- 1.3.3: sqlserver
- 1.4:資料庫服務器,資料庫,表與記錄的關系
- 1.4.1:資料庫服務器(硬體)
- 1.4.2:資料庫(軟體)
- 1.4.3:表
- 1.4.4:記錄
- 1.5: 資料庫存盤引擎種類
- 1.5.1 資料庫存盤引擎 概念
- 1.5.2 查看存盤引擎:
- 1.5.3 各個存盤引擎的對比
- 二:linux平臺下安裝與配置mysql
- 2.1:yum安裝
- 2.2:原始碼包編譯引數與安裝
- 2.2.1 安裝編譯原始碼所需的工具和庫:
- 2.2.2 下載源代碼:
- 2.2.3 編譯安裝:
- 2.2.4 Mysql初始化資料目錄:
- 2.2.5 手動啟動服務
- 2.2.6 檢測服務是否存活
- 2.2.7 手動停止服務
- 2.2.8 使用腳本管理服務
- 2.3:設定root密碼
- 三:mysql資料庫的管理
- 3.1:庫的建立與洗掉
- 3.1.1 直接創建
- 3.1.2 如果不存在則創建(加了判斷)
- 3.1.3 直接洗掉資料庫
- 3.1.4 如果存在則洗掉資料庫(加判斷)
- 3.2 查看當前的所有資料庫
- 3.3 使用指定資料庫
一:資料庫簡介
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/

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
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/164198.html
標籤:其他
上一篇:MySQL5.7安裝檔案
下一篇:淺析mvcc原理
