Database overview (1)

One: Introduction to the database

1.1: What is a database

Insert picture description here

Insert picture description here
A database is a warehouse for storing data .

1.2: Relational database

Insert picture description here

1.2.1: RDBMS concept

RDBMS is the characteristic of Relational Database Management System: [rɪˈleɪʃənl]

Relational database: It is a database based on a relational model . It uses mathematical concepts and methods such as set algebra to process the data in the database.

This so-called " relational " can be understood as the concept of " table ". A relational database consists of one or several tables , as shown in the figure:
Insert picture description here

1.2.2: RDBMS features

  • The data appears in the form of a table
  • Various record names per line
  • Each column is the data field corresponding to the record name
  • Many rows and columns form a form
  • Several forms form a database

1.2.4: RDBMS terminology

Before we start learning MySQL database, let us first understand some terms of RDBMS:

  • Database: A database is a collection of related tables.
  • Data table: A table is a matrix of data. A table in a database looks like a simple spreadsheet.
  • Column: A column (data element) contains the same type of data, such as postal code data.
  • Row: A row (= tuple, or record) is a group of related data, such as a piece of data subscribed by a user.
  • Redundancy: Store twice the data. Redundancy reduces performance but improves data security.
  • Primary key: The primary key is unique. A data table can only contain one primary key. You can use the primary key to query data.
  • Foreign key: A foreign key is used to associate two tables.
  • Composite key: A composite key (composite key) uses multiple columns as an index key and is generally used for composite indexes.
  • Index: Use indexes to quickly access specific information in database tables. An index is a structure for sorting the values ​​of one or more columns in a database table. Similar to a catalog of books.

1.3: mysql, oracle, sqlserver functions and applications

1.3.1:mysql

Insert picture description hereMySQL is mainly used for large portals, such as Sogou, Sina, etc. Its main advantage is open source , because the database of open source is free, and it is now a product of Oracle.

1.3.2:oracle

Insert picture description here
Oracle is mainly used in banks, railways, airports, etc. The database is powerful and the software cost is high. It is also a product of Oracle.

1.3.3: sqlserver

Insert picture description here
SQL Server is a product of Microsoft Corporation, which is mainly used in large and medium-sized enterprises, such as Lenovo and Founder.

1.4: The relationship between database server, database, table and record

1.4.1: Database server (hardware)

Insert picture description here

1.4.2: Database (software)

Insert picture description hereInsert picture description here

1.4.3: Table

Insert picture description here

1.4.4: Record

Insert picture description hereThe relationship between database server, database, table, and record is as follows:
Insert picture description here

1.5: Types of database storage engines

Insert picture description here
Insert picture description here

1.5.1 Database storage engine concept

Storage engine: the table type (table_type)
users can choose how to store data, indexes, and whether to use transactions according to the needs of the application.

Choosing a suitable storage engine can often effectively improve database performance and data access efficiency.

MySQL supports many storage engines, including MyISAM, InnoDB, BDB, MEMORY, MERGE, EXAMPLE, NDB Cluster, ARCHIVE, etc.

Among them, InnoDB and BDB support transaction security .

It also supports some third-party storage engines, such as TokuDB (high write performance and high compression storage engine), Infobright (column storage engine).

1.5.2 View storage engine:

show engines;

Insert picture description here

1.5.3 Comparison of various storage engines

Details view:
Insert picture description here

Two: install and configure mysql under linux platform

2.1: yum installation

Operating system: centos7.7

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

Insert picture description here
verification:

[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

Management service (start/stop):

设置开机自启
[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

Verify service port:

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

Log in to the database:

There is no password by default!

[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: Source code package compilation parameters and installation

2.2.1 Install the tools and libraries needed to compile the source code:

For source package versions after MySQL 5.5, the installation method uses CMake tool to compile and install, so you need to install it in advance before installing the latest version of MySQL.

CMake is a cross-platform, open source software build system used to control the software compilation process and generate independent configuration files (makefile or project)

CMake official website https://cmake.org/

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

2.2.2 Download the source code:

Download path:
https://downloads.mysql.com/archives/community/Upload the
Insert picture description here
source package:

[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

Unzip:

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

2.2.3 Compile and install:

[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

Compilation parameter description:

-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 指定服务监听的端口号

When the following screenshot appears, you can execute make&&make install: the
Insert picture description here
compilation process is a bit long

make -j2 && make install

Note: -j is used to specify the number of CPU cores, which can speed up the compilation, and it can be omitted

[After compiling errors, execute make clean, and then delete rm CMakeCache.txt to recompile]

Insert picture description here

Detailed explanation of official website compilation parameters

2.2.4 Mysql initializes the data directory:

Data initialization is required before Mysql is started.

创建数据目录
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/ 

Insert picture description hereError handling:
FATAL ERROR: please install the following Perl modules before executing when the MySQL database is initialized

yum -y install autoconf

2.2.5 Manually start the service

Modify 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

Insert picture description here

2.2.6 Check whether the service is alive

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

2.2.7 Stop the service manually

[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 Use script management services

[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: Set the root password

[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!

Error handling:
view socket path
Insert picture description here
plus parameter: -S, specify socket path

[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.

Log in to mysql:
Insert picture description here

Three: mysql database management

3.1: Library creation and deletion

3.1.1 Direct creation

create database database name;

CREATE DATABASE `mydb` CHARACTER SET utf8 COLLATE utf8_general_ci;

For example:

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

3.1.2 Create if it does not exist (judgment added)

create database if not exists database name;

For example:

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

3.1.3 Delete the database directly

drop database database name;

For example:

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

3.1.4 Delete the database if it exists (plus judgment)

drop database if exists database name;

For example:

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

3.2 View all current databases

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

3.3 Use the specified database

use database name;

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

Guess you like

Origin blog.csdn.net/zhangshaohuas/article/details/108947147