MySQL database foundation --- database management and high availability

Preface

  • In this era of information explosion, a large amount of data and information are constantly being produced, and what comes with it is how to store, retrieve and manage them safely and effectively. Effective storage, efficient access, convenient sharing, and security control of data have become problems to be solved in the information age.

1. Introduction to the database

1.1 The necessity of using the database

  • Using the database can store data efficiently and clearly, so that people can manage data more quickly and conveniently;
  • The database has the following characteristics:
可以结构化存储大量的数据信息,方便用户进行有效的检索和访问;
可以有效地保持数据信息的一致性、完整性,降低数据冗余;
可以满足应用的共享与安全方面的要求
  • Database technology is one of the core technologies of computer science and has a complete theoretical foundation.

1.2 Basic concepts of database

1.2.1 Data

  • Symbol record
  • Including numbers, text, graphics, images, sounds, file records, etc.
  • Store in a unified format in the form of "records"

1.2.2 Data Sheet

  • Organize different records together
  • Used to store specific data

1.2.3 Database

  • A collection of tables is a warehouse for storing data
  • A collection of related data stored in a certain organization

1.2.4 Database Management System (DBMS)

  • It is a system software that realizes effective organization, management and access to database resources

1.2.5 Database System

  • It is a man-machine system consisting of hardware, OS, database, DBMS, application software and database users
  • Users can operate the database through DBMS or applications

Insert picture description here

1.3 History of database system development

1.3.1 The first generation database

  • Since the 1960s, the first generation of database systems came out
  • It is a database system of hierarchical model and network model
  • Provides strong support for unified management and sharing of data

1.3.2 The second generation database

  • In the early 1970s, the second generation of databases-relational databases began to appear
  • In the early 1980s, IBM's relational database system DB2 came out, and began to gradually replace hierarchical and mesh model databases, becoming the mainstream of the industry
  • So far, relational database systems still occupy the main position of database applications

1.3.3 The third generation database

  • Since the 1980s, new database systems adapted to different fields have been emerging
  • Object-oriented database system with strong practicability and wide adaptability
  • In the late 1990s, a situation in which multiple database systems jointly supported applications was formed
  • -Some new elements have been added to mainstream database systems: for example, the "relation-object" database model supported by Oracle

1.4 Introduction to current mainstream databases

  • SQL Server (product of Microsoft Corporation):
    for Windows operating system; simple and easy to use
    Insert picture description here

  • Oracle (Oracle company product): For
    all mainstream platforms; safe, complete, and complicated to operate
    Insert picture description here

  • DB2 (product of IBM):
    for all mainstream platforms; sky-shaped, safe and complete
    Insert picture description here

  • MySQL (acquired by Oracle):
    free, open source, small in size
    Insert picture description here

1.5 Relational database

1.5.1 Overview

  • Relational database system 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

Insert picture description here

1.5.2 Introduction to each module

  • Entities: also called instances, corresponding to "events" or "things" that can be distinguished from other objects in the real world, such as "bank customers, bank accounts, 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.5.3 Introduction to the structure of relational database

  • 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.5.4 Application of relational database

  • Relational database:
    Oracle, MySQL
    SQLServer, Sybase
    Informix, access
    DB2, FoxPRO

  • Application examples:
    12306 user information system
    Taobao account number system
    China Unicom mobile phone number information system
    bank user account system
    website user information system

1.6 Introduction to non-relational databases

  • Non-relational database is also called NoSQL (Not Only sQL)
  • The stored data is not based on the relational model and does not require a fixed table format
  • The advantages of non-relational databases: the
    database can be read and written
    with high concurrent, and the massive data can be stored and accessed efficiently. The
    database has high scalability and high availability
  • Commonly used non-relational databases: Redis, mongoDB, etc.

2. MySQL service foundation

2.1 Introduction to MySQL database

  • A popular open source relational database
  • Oracle products
  • Comply with GPL agreement, can use and modify for free
  • Features:
    excellent performance, stable service,
    open source, no copyright restrictions, low cost
    , multi-threaded, multi-user
    based on CIS (client/server) architecture,
    safe and reliable

2.2 Compile and install MySQL database

2.2.1 Preparation

  • Need to prepare MySQL component package
  • If other databases are installed, uninstall them first
yum -y remove 数据库名

2.2.2 Source code compilation and installation

  • Install related tools
yum -y install ncurses ncurses-devel cmake
		'//ncurses-devel是字符终端下屏幕控制的基本库'
		'//cmake跨平台编译安装工具'
  • Create a running user
useradd -s /sbin/nologin mysql	'//创建不可登录的用户'
  • Unpack
tar zxvf mysql-boost-5.7.20.tar.gz	'//解包'
cd mysql-5.7.20/
  • cmake installation and configuration
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-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 \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1
  • Compile and install
make && make install		'//编译安装'

2.3 Other adjustments after installation

  • Set permissions on the database directory
cd
chown -R mysql:mysql /usr/local/mysql/		'//设置属主和数组'
  • Create the configuration file /etc/my.cnf
rm -rf /etc/my.cnf		'//如果原来的/etc/文件夹下有 my.cnf 文件可以删除'

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
  • Set environment variables
echo 'PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile
echo 'export PATH' >> /etc/profile
source /etc/profile		'//使配置立即生效'
  • Initialize the database
mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data

2.4 Add and start system services

cd /usr/local/mysql/
cp usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system
systemctl daemon-reload	 	'//守护进程重新加载'
systemctl start mysqld
systemctl enable mysqld
netstat -anpt | grep 3306		'//查看端口信息'

2.5 Access to MySQL database

  • Log in to the MySQL server
mysql -uroot -p		'//初始化密码,第一次以空密码登录'

Insert picture description here

  • You can also change the set login password
mysqladmin -uroot -p password 'abc123'  //给root账号设置密码为abc123
mysql -uroot -pabc123	//以"abc123"为密码登录数据库

Insert picture description here

  • Execute MySQL operation statement
mysql> show databases;	//查看当前mysql服务器中包含的库
mysql> show tables;  //查看当前所在库中包含的表

Insert picture description here

  • Exit "mysql>" operating environment
mysql> exit
Bye
[root@localhost ~]#

3. Basic database operation commands

  • SQL language
Structured Query Language的缩写,即结构化查询语言
关系型数据库的标准语言
用于维护管理数据库:包括数据查询、数据更新、访问控制、对象管理等功能

3.1 Classification of the database

3.1.1 Classification of data

  • Structured data Data that
    can be represented by a two-dimensional logical table is structured data
  • Unstructured data Data that is
    not convenient to use two-dimensional logical tables is unstructured data

3.1.2 Classification of databases

  • Relational database
    The data stored in a relational database is in table format. Therefore, it is stored in the rows and columns of the data table. The data tables can be stored in association with each other, making data extraction easy.
  • Non-relational databases The data in
    non-relational databases is not suitable for access in the form of tables, but is combined in large chunks, usually stored in data sets, which is convenient for storing documents, pictures and other data.
  • Classification of commonly used statements in mysql database
DDL (Data Definition Language,数据定义语言)
用来建立数据库、数据库对象和定义字段,如 CREATE、ALTER、DROP。

DML (Data Manipulation Language,数据操纵语言)
用来插入、删除和修改数据库中的数据,如 INSERT、UPDATE、DELETE。

DQL (Data Query Language,数据查询语言):用来查询数据库中的数据,如 SELECT。

DCL (Data Control Language,数据控制语言):
用来控制数据库组件的存取许可、存取权限等,如 COMMIT、ROLLBACK、GRANT、REVOKE。

3.2 MySQL common management operations

  • View database structure
  • Create and delete libraries and tables
  • Management table records

3.2.1 View the database structure

查看数据库列表信息的命令

show databases;
查看数据库中的数据表信息

use 数据库名;
show tables;
显示数据表的结构(字段)

describe [数据库名.]表名;
use 数据库名;
describe 表名;

3.2.2 Create and delete libraries and tables

  • Create a new library
create database 数据库名;

刚创建的数据库是空的,其中不包含任何表,在/usr/local/mysql/data目录下会自动生成一个与新建的库名相同的空文件夹
  • Create new table
create table 表名(字段 1 名称 类型,字段 1 名称 类型,...,PRIMARY KEY(主键名))

创建表之前,应先明确数据表格的结构、各字段的名称和类型等信息。
  • Delete a data table
DROP TABLE 库名.表名

Guess you like

Origin blog.csdn.net/weixin_42449832/article/details/111563737