table of Contents
1. Database overview
- Database (database), in short, can be regarded as an electronic file cabinet-a place for storing electronic files. Users can perform operations such as adding, intercepting, updating, and deleting data in the file.
- The so-called "database" is a data collection that is stored together in a certain way, can be shared by multiple users, has as little redundancy as possible, and is independent of the application.
- A database is composed of multiple tablespaces (tablespace)
1.1 Basic Concepts of Database
-
Data (Data)
describes the symbolic records of things,
including numbers, text, images, graphics, sounds, file records, etc.
Stored in a unified format in the form of "records" -
The table
organizes the different records together.
Used to store specific data. -
A collection of database tables is a warehouse
for storing data. A collection of related data stored in a certain organizational manner -
Database Management System (DBMS)
is a system software that realizes effective organization, management and access to database resources -
The database system
is a human-machine system consisting of hardware, OS, database, DBMS, application software and database users.
Users can operate the database through DBMS or application programs.
1.2 History of database development
- The first-generation database
Since the 1960s, the first-generation database system has come out. They are database systems of hierarchical model and network model, which provide strong support for unified management and sharing of data - The second-generation database
In the early 1970s, the second-generation database-relational database began to appear.
In the early 1980s, IBM's relational database system DB2 came out. As the relational database of the second-generation database system, it began to gradually replace the level and network. The database of the state model has become the dominant database and the mainstream of the industry.
So far, relational database systems still occupy the main position of database applications
Since the third generation of databases began in the 1980s, various new database systems adapted to different fields have emerged, such as engineering databases, multimedia databases, graph databases, intelligent databases, distributed databases and object-oriented databases, etc., especially object-oriented databases. The system, due to its strong practicability and wide adaptability, has been favored by people.
In the late 1990s, a situation where a variety of database systems jointly support applications was formed.
Of course, in terms of business applications, relational databases still dominate. Some new elements have been added to the mainstream business database system.
For example, the "relation-object" database model supported by Oracle
1.3 Introduction to current mainstream databases
-
Relational database: Typical representative mysql oracle server
structured data: characters, strings, values, Boolean values, etc.
Large database: oracle db2 (IBM) sybase (low-key leader)
medium-sized database: mysql (oracle company) sql server ( MS Microsoft)
small database: access (office) -
MySQL (acquired by Oracle) is
free, open source, and small -
sql server (product of Microsoft)
for Windows operating system,
simple and easy to use -
access (product of Microsoft Corporation)
for Windows operating system
is a member of the Microsoft office suite of applications -
oracle (Oracle's product)
for all mainstream platforms
Safe, complete, and complex operation -
db2 (IBM company)
for all mainstream platforms
Large, safe and complete -
sybase (Sybase company)
for all major platforms,
large-scale, security, and improve
the world's largest
1.4 Relational Database
- Relational database is a database system based on relational model
- The data structure of the relational model uses a simple and easy to understand two-dimensional data table
- The relational model can be represented by a simple "entity-relationship" (ER) diagram
- The ER diagram contains three elements: entity (data object), relationship and attribute
- Entities
are also called instances, corresponding to "events" or "things" that can be distinguished from other objects in the real world, such as bank accounts, bank customers, etc. - Attribute
A certain characteristic of an entity. An entity can have multiple attributes. For example, each entity in the "bank customer" entity set has attributes such as name, address, and phone number. - Contact
The corresponding relationship between entity sets is called contact, also known as relationship. For example, there is a "savings" relationship between bank customers and bank accounts - The collection of all entities and their connections constitutes a relational database
1.4.1 Relational database storage structure
- The storage structure of the relational database is a two-dimensional table
- In each two-dimensional table,
each row is called a record, used to describe the information of an object,
each column is called a field, used to describe an attribute of the object
1.4.2 Application of relational database
- Relational database:
Oracle, MySQL
SQLServer, Sybase
infomix, access
DB2, FoxPRO - Application examples
12306 user information system
Taobao account system
China Unicom mobile phone account system
bank user account system
website user information system
1.5 Non-relational database
-
Non-relational databases are also called NOsQL (Not Only SQL), and the stored data is not based on the relational model and does not require a fixed table format
-
As a supplement to the relational database, non-relational databases play high efficiency and high performance in the era of increasingly rapid development of websites
-
The advantages of non-relational databases The
demand
for high concurrent read and write of the database The demand for efficient storage and access of massive data The demand for
high scalability and high availability of the database
Most of NoSQL exists in the form of files -
Relational database storage method
Key-value method (key-value), storage, deletion, and
modification of data based on keys Column-oriented storage (column-oriented), a method of storing related data in a column family
document. The database consists of a series of data Item composition, each data item has a name and a corresponding value
Graphic mode, the entity is a vertex, the relationship is an edge, and the data is saved as a graph
Non-relational database product -
Memcached is an open source, high-performance, distributed memory object caching system, which stores data in a key-value manner.
Caches data to reduce database pressure and speed up access.
Accelerate dynamic Web applications. The
cached content is stored in memory -
Redis is also a key-vaue method to store data. The data is also stored in memory, but the data is written to disk regularly.
Compared with Memcached, it has the following characteristics.
Supports memory cache.
Supports persistence.
More data types.
Supports clusters and distributed
support.
The log file of the queue database is used for recovery, and the oracle log is called the redo log group -
For example:
Ali oss object storage adopts the idea of oracle redo log group.
Ali oss data is in triplicate. It is
a relay log (binary log) of Pangu system Redis.
Redis application example.
Database front-end cache
session sharing.
When you need to cache other than key/value when more data types outside
when the cached data needs to be stored for a long time
Two: compile and install MySQL
2.1 Preparation
- Prepare the mysql compression package mysql-boost-5.7.17.tar
- Prepare the boost compression package boost_1_59_0.tar
- Prepare environment and toolkit
[root@localhost ~]# yum -y install ncurses ncurses-devel bison cmake
## bison用于自动生成语法分析器程序 ## 安装光盘自带的ncurses-devel包 autoconf 适应多种Unix类系统的shell脚本的工具
## cmake 编译分析工具
[root@localhost ~]# useradd -s /sbin/nologin mysql ## 创建程序用户mysql
2.2 Mysql source code compilation and installation
[root@localhost ~]# tar zxvf mysql-5.7.17.tar.gz ## 解压MySQL压缩包
[root@localhost ~]# tar zxvf boost_1_59_0.tar.gz -C /usr/local/ ## 解压MySQL压缩包
[root@localhost ~]# cd /usr/local
[root@localhost local]# mv boost_1_59_0 boost
[root@localhost ~]# cd
[root@localhost ~]# cd mysql-5.7.17/ ## 进入解压后的安装包
[root@localhost ~]#
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \## DCMAKE_INSTALL_PREFIX 安装路径
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \ ## DMYSQL_UNIX_ADDR= 数据库对接目录
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \ ## DDEFAULT_CHARSET 字符集为uft8 即中文
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \ ## -DMYSQL_DATADIR数据库数据管理用户目录
-DWITH_BOOST=/usr/local/boost \
-DWITH_SYSTEMD=1
[root@localhost mysql-5.7.17]# make && make install ## 编译及安装 时间较长需耐心等待
2.3 Adjust the permissions of the installation path file
[root@localhost ~]#chown -R mysql:mysql /usr//local/mysql/ ## 属主 属组都必须是mysql用户
2.4 Adjust the configuration file
[root@localhost mysql-5.7.17]#vi /etc/my.cnf
[client]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock
[mysql]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock
[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character_set_server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
2.5 Set environment variables
[root@localhost ~]#echo 'PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile ## 将MySQL命令路径追加到 PATH环境 写入到 全局变量文件里面
[root@localhost ~]#echo 'export PATH' >>/etc/profile ## 将PATH申明为全局变量
[root@localhost ~]#source /etc/profile ## 重新加载全局文件,使刚才问配置生效
2.6 Initialize the database
[root@localhost ~]#cd /usr/local/mysql
bin/mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data
2.7 Open the database
[root@localhost ~]# cp usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/ ## 系统能识别 能用systemctl命令开启
[root@localhost ~]# systemctl start mysqld ## 其他如自启动关闭查看状态跟其他服务命令一个格式
[root@localhost mysql]# netstat -anpt | grep 3306 ## 开启后查看服务
tcp6 0 0 :::3306 :::* LISTEN 47949/mysqld
2.8 Set the administrator password for the database
[root@localhost mysql]# mysqladmin -u root -p password
Enter password: ## 原来没密码 直接回车
New password: ## 输入新密码
Confirm new password:
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.