数据库就是一个存储数据的仓库。
RDBMS 即关系数据库管理系统(Relational Database Management System)的特点: [rɪˈleɪʃənl]
关系型数据库:是建立在关系模型基础上的数据库,借助于集合代数等数学概念和方法来处理数据库中的数据。
这种所谓的"关系型"可以理解为"表格"的概念, 一个关系型数据库由一个或数个表格组成, 如图所示的一个表格:
在我们开始学习MySQL 数据库前,让我们先了解下RDBMS的一些术语:
数据库: 数据库是一些关联表的集合。数据表: 表是数据的矩阵。在一个数据库中的表看起来像一个简单的电子表格。列: 一列(数据元素) 包含了相同类型的数据, 例如邮政编码的数据。行:一行(=元组,或记录)是一组相关的数据,例如一条用户订阅的数据。冗余:存储两倍数据,冗余降低了性能,但提高了数据的安全性。主键:主键是唯一的。一个数据表中只能包含一个主键。你可以使用主键来查询数据。外键:外键用于关联两个表。复合键:复合键(组合键)将多个列作为一个索引键,一般用于复合索引。索引:使用索引可快速访问数据库表中的特定信息。索引是对数据库表中一列或多列的值进行排序的一种结构。类似于书籍的目录。mysql主要用于大型门户,例如搜狗、新浪等,它主要的优势就是开放源代码,因为开放源代码这个数据库是免费的,它现在是甲骨文公司的产品。
oracle主要用于银行、铁路、飞机场等。该数据库功能强大,软件费用高。也是甲骨文公司的产品。
sql server是微软公司的产品,主要应用于大中型企业,如联想、方正等。
数据库服务器,数据库,表,记录的关系如下:
存储引擎:即表类型(table_type) 用户可以根据应用的需求选择如何来存储数据、索引、是否使用事务等。
选择合适的存储引擎往往能够有效的提高数据库的性能和数据的访问效率。
MySQL支持很多存储引擎,包括MyISAM、InnoDB、BDB、MEMORY、MERGE、EXAMPLE、NDB Cluster、ARCHIVE等。
其中InnoDB和BDB支持事务安全。
它还支持一些第三方的存储引擎,例如TokuDB(高写性能高压缩存储引擎)、Infobright(列式存储引擎)。
详情查看:
操作系统:centos7.7
[root@localhost packages]# yum -y install mariadb mariadb-server验证:
[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管理服务(启动/停止):
设置开机自启 [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验证服务端口:
[root@localhost ~]# ss -lptnu|grep 3306 tcp LISTEN 0 50 *:3306 *:* users:(("mysqld",pid=2283,fd=15))登录数据库:
默认是没有密码的!
[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)]>MySQL 5.5之后的源码包版本,安装方式采用CMake工具编译进行安装,因此在安装最新版MySQL之前,需要提前安装它。
CMake是一个跨平台、开源软件构建系统,用于控制软件编译过程及生成独立的配置文件(makefile或者project)
CMake官网https://cmake.org/
yum -y install cmake gcc gcc-c++ ncurses-devel下载路径: https://downloads.mysql.com/archives/community/ 上传源码包:
[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解压:
[root@localhost src]# tar zxvf mysql-5.6.10.tar.gz [root@localhost src]# cd mysql-5.6.10编译参数说明:
-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 指定服务监听的端口号当出现下面的截图,就可以执行make&&make install 了: 编译过程有点长
make -j2 && make install注:-j 用来指定CPU核心数,可加快编译速度,不加也可以
【编译有错误后,执行make clean ,然后要删除 rm CMakeCache.txt ,才能重新编译】
官网编译参数详解
Mysql启动前需要进行数据的初始化。
创建数据目录 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/报错处理: 在初始化MySQL数据库时出现提示FATAL ERROR: please install the following Perl modules before executing
yum -y install autoconf修改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报错处理: 查看socket路径 加上参数:-S ,指定socket路径
[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.登录mysql:
create database 数据库名;
CREATE DATABASE `mydb` CHARACTER SET utf8 COLLATE utf8_general_ci;举例:
MySQL [(none)]> create database wg; Query OK, 1 row affected (0.00 sec)create database if not exists 数据库名;
举例:
MySQL [(none)]> create database if not exists wg; Query OK, 1 row affected, 1 warning (0.00 sec)drop database 数据库名;
举例:
MySQL [(none)]> drop database wg; Query OK, 0 rows affected (0.00 sec)drop database if exists 数据库名;
举例:
MySQL [(none)]> drop database if exists wg; Query OK, 0 rows affected, 1 warning (0.00 sec)use 数据库名;
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