First experience with MySQL database

database

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 collection of data stored together in a certain way that can be shared by multiple users, with as little redundancy as possible, and independent of the application.
A database is composed of multiple tablespaces (tablespace)
compared to a database For notebooks and documents, it lies in massive data, easy to manage data, easy to quickly query data, data algorithms, and unstructured characteristics

Data
Symbol records describing things are called data (Data),
including numbers, text, graphics, images, sounds, file records, etc.
Stored in a unified format in the form of "records"
Each row is called a record
Each column is called a field

The table
organizes the different records together to form a "table
is used to store specific data

Database A
database is a collection of tables and a warehouse
for storing data. Related data is stored in a certain organization.
Database Management System (DBMS)

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

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

The role of the database

  • Persistent storage of data
  • High reliability, high availability
  • Fast extraction of data

History of database system development

第一代数据库
    自20世纪60年代起,第一代数据库系统问世。它们是层次模型与网状模型的数据库系统,为统一管理和共享数据提供了有力的支撑
第二代数据库
    20世纪70年代初,第二代数据库–关系数据库开始出现
    20世纪80年代初,IBM公司的关系数据库系统DB2问世,作为第二代数据库系统的关系数据库,开始逐步取代层次与网状模型的数据库,成为占主导地位的数据库,成为行业主流。
    到目前为止,关系数据库系统仍占领数据库应用的主要地位
第三代数据库
    自20世纪80年代开始,各种适应不同领域的新型数据库系统不断涌现,如工程数据库,多媒体数据库,图形数据库,智能数据库,分布式数据库及面向对象数据库等,特别是面向对象数据库系统,由于其实用性强,适应面广而受到人们的青睐
    20世纪90年代后期,形成了多种数据库系统共同支撑应用的局面
    当然,在商务应用方面,依然还是关系数据库占主流,不过,已经有一些新的元素被添加进主流商务数据库系统中。
    例如,Oracle支持的“关系-对象”数据库模型

Today's mainstream database types

Database storage type

  • Relational database:
    stores numeric values, characters, strings, Boolean values, etc.
  • Non-relational database: it
    stores pictures, videos, voices, etc. (objects)

Relational Database

MySQL(甲骨文公司收购)
    免费,开源,体积小
sql server(微软公司产品)
    面向Windows操作系统
    简单,易用
access(微软公司产品)
    面向Windows操作系统
    是Microsoft office套件应用程序的成员
oracle(甲骨文公司产品)
    面向所有主流平台
    安全,完善,操作复杂
db2(IBM公司)
    面向所有主流平台
    大型,安全,完善
sybase(Sybase公司)
    面向所有主流平台
    大型,安全,完善
    全球最大

The relational database system is a database system based on the relational model. Its basic concept comes from the relational model. The
relational model is based on the theory of relational algebra. The data structure uses simple and easy to understand two-dimensional data tables. You can use simple "entity-relationship" "(ER) diagram to directly indicate that the
ER diagram contains three elements: entity (data object), relationship and attribute.

  • 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 called relationship. For example, there is a "savings" relationship between a bank customer and a bank account.
    The collection of all entities and their connections constitutes a relational database
    . There are codes in the relational database table, called the primary key, which is unique and non-empty
    . The structure is a two-dimensional table, and the data reflecting things and their connections are stored in the form of a table.
    In each two-dimensional table, each row is called a record, which is used to describe the information of an object; each column is called a field. To describe an attribute of the object
关系型数据库的应用举例:
    12306用户信息系统
    淘宝账号信息系统、支付宝账号系统移动、电信、联通手机号信息系统、计费系统银行用户账号系统
    网站用户信息系统

Non-relational database

MongoDB
Redis(内存/缓存数据库),以K-V(key-value—)键值对形式存在,变量名-值
memcache(内存/缓存数据库),以K-V(key-value—)键值对形式存在,变量名-值
Redis和memcache相同点
    存储高热数据库
Redis和memcache不同点
    Redis可以做持久化保存,可以存储对象

Non-relational database is also called NOsQL (Not Only SQL)

存储数据不以关系模型为依据,不需要固定的表格式

非关系型数据库作为关系数据库的一个补充,在日益快速发展的网站时代,发挥着高效率与高性能

非关系型数据库的优点
    数据库高并发读写的需求
    对海量数据高效率存储与访问
    数据库的高扩展性与高可用性的需求

NoSQL大部分以文件形式存在

关系型数据库存储方式
    键值方式( key-value),以键为依据存储、删、改数据
    列存储( Column- oriented),将相关的数据存储在列族中
    文档的方式,数据库由一系列数据项组成,每个数据项都有名称与对应的值
    图形方式,实体为顶点,关系为边,数据保存为一个图形

Non-relational database products

    Memcached是一个开源的、高性能的、具有分布式内存对象的缓存系统,以 key-value方式存储数据
        缓存数据以减轻数据库压力并能加快访问速度
        加速动态Web应用
        缓存的内容保存在内存中

    redis也是一个以key-vaue方式存储数据的,数据也是保存在内存中,但会定期将数据写入磁盘中
        相对于 Memcached有以下特点
            支持内存缓存
            支持持久化
            数据类型更多
            支持集群、分布式
            支持队列

    数据库的日志文件用来做恢复,oracle日志称为重做日志组

    例如:

        阿里oss对象存储采用oracle重做日志组思想,

        阿里oss数据一式三份,是盘古系统

        Redis有一个中继日志(二进制日志)

    Redis应用举例
        数据库前端缓存
        session共享
        当需要缓存除了key/value之外的更多数据类型时
        当缓存的数据需要长久保存

Compile and install MySQL

[root@localhost ~]# yum -y install ncurses ncurses-devel bison cmake
[root@localhost ~]# useradd -s /sbin/nologin mysql
[root@localhost ~]# cd /opt
[root@localhost opt]# mkdir mysql
[root@localhost opt]# cd mysql/
mysql-boost-5.7.20.tar.gz
[root@localhost mysql]# tar zvxf mysql-boost-5.7.20.tar.gz 
[root@localhost mysql]# cd mysql-5.7.20/
[root@localhost mysql-5.7.20]# 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
[root@localhost mysql-5.7.20]# make && make install
[root@localhost mysql-5.7.20]# chown -R mysql:mysql /usr/local/mysql/
[root@localhost mysql-5.7.20]# vim /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/mysql.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
[root@localhost mysql-5.7.20]# chown mysql:mysql /etc/my.cnf 
[root@localhost mysql-5.7.20]# vim /etc/profile
 77 PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
 78 export PATH
[root@localhost mysql-5.7.20]# source /etc/profile
[root@localhost mysql-5.7.20]# echo $PATH
/usr/local/mysql/bin:/usr/local/mysql/lib:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@localhost mysql-5.7.20]# cd /usr/local/mysql/
[root@localhost mysql]# bin/mysqld \
> --initialize-insecure \
> --user=mysql \
> --basedir=/usr/local/mysql \
> --datadir=/usr/local/mysql/data
[root@localhost mysql]# cp usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
[root@localhost mysql]# netstat -natp | grep 3306
[root@localhost mysql]# systemctl start mysqld.service 
[root@localhost mysql]# netstat -natp | grep 3306
tcp6       0      0 :::3306                 :::*                    LISTEN      97987/mysqld   

[root@localhost mysql]# systemctl start mysqld.service 
[root@localhost mysql]# systemctl enable mysqld.service 
Created symlink from /etc/systemd/system/multi-user.target.wants/mysqld.service to /usr/lib/systemd/system/mysqld.service.
[root@localhost mysql]# systemctl stop mysqld.service 
[root@localhost mysql]# systemctl status mysqld.service 

[root@localhost mysql]# mysqladmin -u root -p password "abc123"
Enter password: 
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.

[root@localhost mysql]# mysql -u root -p 
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.20 Source distribution

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> quit
Bye

Guess you like

Origin blog.csdn.net/weixin_46355881/article/details/108064503