这里写目录标题
一、数据库概述
1.1 数据库的基本概念
1.1.1 数据(Data)
- 描述事物的符号记录
- 包括数字,文字、图形、图像、声音、档案记录等
- 以"记录"形式按统一-的格式进行存储
1.1.2 表
- 将不同的记录组织在一起
- 用来存储具体数据
1.1.3 数据库
- 表的集合,是存储数据的仓库
- 以一定的组织方式存储的相互有关的数据集合
1.2 DBMS
1.2.1 数据库管理系统(DBMS)
- 是实现对数据库资源有效组织、管理和存取的系统软件
1.2.2 数据库系统
- 是一个人机系统,由硬件、OS、数据库、DBMS、应用软件和数据库用户组成
- 用户可以通过DBMS或应用程序操作数据库
1.3 当今主流数据库介绍
1.3.1 SQL Server (微软公司产品)
- 面向Windows操作系统,简单、易用
1.3.2 Oracle (甲骨文公司产品)
面向所有主流平台,安全、完善,操作复杂
1.3.3 DB2 (IBM公司产品)
面向所有主流平台,大型、安全、完善
1.3.4 MySQL (甲骨文公司收购)
免费、开源、体积小
1.4 关系数据库
1.4.1 概述
- 关系数据库系统是基于关系模型的数据库系统
- 关系模型的数据结构使用简单易懂的二维数据表
- 关系模型可用简单的“实体关系”(E-R) 图来表示
- E-R图中包含了实体(数据对象)、关系和属性三个要素
1.4.2 属性
1、实体
- 也称为实例,对应现实世界中可区别于其他对象的“事件”或“事物”。如银行客户、银行账户等
2、属性 - 实体所具有的某-特性,一个实体可以有多个属性。如“银行客户”实体集中的每个实体均具有姓名、住址、电话等属性
3、联系 - 实体集之间的对应关系称为联系,也称为关系。如银行客户和银行账户之间存在“储蓄”的关系
4、所有实体及实体之间联系的集合构成一个关系数据库
1.4.3 性质
1、关系数据库的存储结构是二维表格
2、在每个二维表中
- 每一行称为一条记录,用来描述一一个对象的信息
- 每一列称为一个字段,用来描述对象的一个属性
1.5 非关系数据库介绍
1、非关系数据库也被称作NoSQL (Not Only SQL)存储数据不以关系模型为依据,不需要固定的表格式
2、非关系型数据库的优点
- 数据库可高并发读写
- 对海量数据高效率存储与访问
- 数据库具有高扩展性与高可用性
3、常用的非关系数据库:Redis、mongoDB等
二、mysql数据库
2.1 MySQL数据库介绍
一款深受欢迎的开源关系型数据库; Oracle旗下的产品,遵守GPL协议,可以免费使用与修改
特点
- 性能卓越、服务稳定
- 开源、无版权限制、成本低
- 多线程、多用户
- 基于C/S (客户端/服务器)架构
- 安全可靠
三、手工编译安装mysql
[root@localhost ~]# tar zxvf mysql-boost-5.7.20.tar.gz -C /opt/
[root@localhost ~]# cd /opt/mysql-5.7.20/
[root@localhost mysql-5.7.20]# useradd -s /sbin/nologin mysql
[root@localhost mysql-5.7.20]#yum -y install ncurses ncurses-devel bison cmake
[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
[root@localhost mysql-5.7.20]# make install
[root@localhost mysql-5.7.20]# cd /usr/local/
[root@localhost local]# ll
drwxr-xr-x. 11 root root 151 8月 11 09:57 nginx
[root@localhost local]# chown -R mysql:mysql /usr/local/mysql/ //更改属主属组为mysql
[root@localhost local]# ll
drwxr-xr-x. 11 mysql mysql 197 8月 11 12:18 mysql
[root@localhost local]# cd /etc/
[root@localhost etc]# vim 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
设置环境变量
[root@localhost etc]# vim /etc/profile
PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
export PATH //声明为全局变量
[root@localhost etc]# source /etc/profile //加载环境变量
初始化数据库
[root@localhost etc]# cd /usr/local/mysql/
[root@localhost mysql]# ls
bin COPYING-test include man README share usr
COPYING docs lib mysql-test README-test support-files
[root@localhost mysql]# ls bin/
mysql_install_db
mysqld
[root@localhost mysql]# bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data //初始化数据库
2020-08-11T11:41:37.791663Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-08-11T11:41:38.155226Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-08-11T11:41:38.210661Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-08-11T11:41:38.269282Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 9dec1b5f-dbc7-11ea-ab28-000c29cd9a36.
2020-08-11T11:41:38.269993Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-08-11T11:41:38.271838Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
[root@localhost mysql]# cp /usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system
[root@localhost mysql]# systemctl start mysqld
[root@localhost mysql]# netstat -antp | grep mysqld
tcp6 0 0 :::3306 :::* LISTEN 95058/mysqld
[root@localhost mysql]# systemctl enable mysqld //设置开机自启
Created symlink from /etc/systemd/system/multi-user.target.wants/mysqld.service to /usr/lib/systemd/system/mysqld.service.
[root@localhost mysql]# mysqladmin -u root -p password "abc123" //设置mysql密码
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: //输入刚刚设置的密码abc123
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> quit
Bye