学Mysql怎样快速入门?

前言

MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下产品。

MySQL 是最流行的关系型数据库管理系统之一,在 WEB 应用方面,MySQL是最好的 RDBMS

(Relational Database Management System,关系数据库管理系统) 应用软件之一。关系数据库将数据 保存在不同的表中,而不是将所有数据放在一个大仓库内,这样就增加了速度并提高了灵活性。

MySQL所使用的 SQL 语言是用于访问数据库的最常用标准化语言。MySQL 软件采用了双授权政策,分 为社区版和商业版,由于其体积小、速度快、总体拥有成本低,尤其是开放源码这一特点,一般中小型

网站的开发都选择 MySQL 作为网站数据库

RDBMS即关系数据库管理系统(Relational Database management System)

关系型数据库:

SQLServer、Oracle、mysql等。

什么是关系型数据库?

关系型数据库是依据关系模型来创建的数据库。

所谓关系模型就是“一对一、一对多、多对多”等关系模型,关系模型就是指二维表格模型,因而一个关系

型数据

库就是由二维表及其之间的联系组成的一个数据组织。

关系型数据可以很好地存储一些关系模型的数据,比如一个老师对应多个学生的数据(“一对多”),一

本书对应

多个作者(“一对多”),一本书对应一个出版日期(“一对一”)

非关系型数据库redis,mongodb,memcached

1.以键值对的方式存储数据---(Key-Value)的形式

2.缓存数据库

什么是非关系型数据库?

非关系型数据库主要是基于“非关系模型”的数据库(由于关系型太大,所以一般用“非关系型”来表其 他类型的 数据库)。

关系型数据库与非关系型数据库的区别

1.关系型数据库:

优点:

1、易于维护:都是使用表结构,格式一致;

2、使用方便:SQL语言通用,可用于复杂查询;

3、复杂操作:支持SQL,可用于一个表以及多个表之间非常复杂的查询。

缺点:

1、读写性能比较差,尤其是海量数据的高效率读写;

2、固定的表结构,灵活度稍欠;

3、高并发读写需求,传统关系型数据库来说,硬盘I/O是一个很大的瓶颈。

====================================================================2.非关系型数据库严格上不是一种数据库,应该是一种数据结构化存储方法的集合,可以是文档或者键值 对等。

优点:

1、格式灵活:存储数据的格式可以是key,value形式、文档形式、图片形式等等,文档形式、图片形式 等等,使用灵活,应用场景广泛,而关系型数据库则只支持基础类型。

2、速度快:nosql可以使用硬盘或者随机存储器作为载体,而关系型数据库只能使用硬盘;

3、高扩展性;

4、成本低:nosql数据库部署简单,基本都是开源软件。

缺点:

1、不提供sql支持,学习和使用成本较高;

2、无事务处理;

3、数据结构相对复杂,复杂查询方面稍欠。

MySQL的官方网址: http://www.mysql.com/ ,MySQL的社区版本下载地址为: http://dev.mysql.com/downloads/mysql/ ,在写本文时,当前的MySQL最新版本是:8.0 。

什么是sql?

SQL代表结构化查询语言(Structured Query Language)。SQL是用于访问数据库的标准化语言。

SQL包含三个部分:

数据定义语言包含定义数据库及其对象的语句,例如表,视图,触发器,存储过程等。

数据操作语言包含允许您更新和查询数据的语句。

数据控制语言允许授予用户权限访问数据库中特定数据的权限

mysql安装

关闭防火墙和selinux

编译安装mysql5.7

注意:首先环境的干净,删除mysql用户,卸载mysql相关的软件。

创建mysql用户

[root@bogon ~]# useradd -r mysql -M -s /bin/false

[root@bogon ~]# id mysql

uid=997(mysql) gid=995(mysql) groups=995(mysql)

 

从官网下载tar包

[root@bogon~]#wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-boost-5.7.27.tar.gz

注意:网络的稳定哦!!

安装编译工具

[root@bogon ~]# yum -y install ncurses ncurses-devel openssl-devel bison gcc gcc-c++ make

[root@bogon ~]# yum -y install cmake

创建mysql目录

[root@bogon ~]# mkdir -p /usr/local/{data,mysql,log}

 

解压

[root@bogon /]# tar xf mysql-boost-5.7.27.tar.gz -C /usr/local/

注:如果安装的MySQL5.7及以上的版本,在编译安装之前需要安装boost,因为高版本mysql需要boots库的

安装才可以正常运行。否则会报CMake Error at cmake/boost.cmake:81错误

安装包里面自带boost包

编译安装

cd 解压的mysql目录

[root@bogon /]# cd /usr/local/mysql-5.7.27/

[root@mysql-server mysql-5.7.27]#cmake . \

-DWITH_BOOST=boost/boost_1_59_0/ \

-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \

-DSYSCONFDIR=/etc \

-DMYSQL_DATADIR=/usr/local/mysql/data \

-DINSTALL_MANDIR=/usr/share/man \

-DMYSQL_TCP_PORT=3306 \

-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \

-DDEFAULT_CHARSET=utf8 \

-DEXTRA_CHARSETS=all \

-DDEFAULT_COLLATION=utf8_general_ci \

-DWITH_READLINE=1 \

-DWITH_SSL=system \

-DWITH_EMBEDDED_SERVER=1 \

-DENABLED_LOCAL_INFILE=1 \

-DWITH_INNOBASE_STORAGE_ENGINE=1

提示:boost也可以使用如下指令自动下载,如果不下载bost压缩包,把下面的这一条添加到配置中第二行

-DDOWNLOAD_BOOST=1/

参数详解:

-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ 安装目录

-DSYSCONFDIR=/etc \ 配置文件存放 (默认可以不安装配置文件)

-DMYSQL_DATADIR=/usr/local/mysql/data \ 数据目录 错误日志文件也会在这个目录

-DINSTALL_MANDIR=/usr/share/man \ 帮助文档

-DMYSQL_TCP_PORT=3306 \ 默认端口

-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \ sock文件位置,用来做网络通信的,客户端连接服务器的时候用

-DDEFAULT_CHARSET=utf8 \ 默认字符集。字符集的支持,可以调

-DEXTRA_CHARSETS=all \ 扩展的字符集支持所有的

-DDEFAULT_COLLATION=utf8_general_ci \ 支持的

-DWITH_READLINE=1 \ 上下翻历史命令

-DWITH_SSL=system \ 使用私钥和证书登陆(公钥) 可以加密。 适用与长连接。坏处:速度慢

-DWITH_EMBEDDED_SERVER=1 \ 嵌入式数据库

-DENABLED_LOCAL_INFILE=1 \ 从本地倒入数据,不是备份和恢复。

-DWITH_INNOBASE_STORAGE_ENGINE=1 默认的存储引擎,支持外键

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

MYSQL怎么使用SYS_GUID mysql怎样使用_MYSQL怎么使用SYS_GUID

 

 

编译时会占用大量的系统资源,建议使用多个核心同时进行编译,否则可能会编译失败

7.编译

#查看服务器cpu数

[root@bogonmysql-5.7.27]# grep processor /proc/cpuinfo | wc -l

2

[root@bogonmysql-5.7.27]# make && make install

MYSQL怎么使用SYS_GUID mysql怎样使用_MySQL_02

 

需要很长时间!大约半小时

初始化

[root@mysql-server mysql-5.7.27]# cd /usr/local/mysql

[root@mysql-server mysql]# chown -R mysql.mysql .

[root@mysql-server mysql]# ./bin/mysqld --initialize --user=mysql --

basedir=/usr/local/mysql --datadir=/usr/local/mysql/data ---初始化完成之后,一

定要记住提示最后的密码用于登陆或者修改密码

 

 

 

 

 

 

 

MYSQL怎么使用SYS_GUID mysql怎样使用_MySQL_03

 

初始化,只需要初始化一次

 

[root@mysql-server ~]# vim /etc/my.cnf ---添加如下内容

[mysqld]

basedir=/usr/local/mysql #指定安装目录

datadir=/usr/local/mysql/data #指定数据存放目录

 

 

 

 

 

 

启动mysql

 

 

 

 

 

MYSQL怎么使用SYS_GUID mysql怎样使用_MySQL_04

 

[root@mysql-server ~]# cd /usr/local/mysql

[root@mysql-server mysql]# ./bin/mysqld_safe --user=mysql &

 

登录mysql

 

 

 

 

 

 

 

 

 

[root@mysql-server mysql]# /usr/local/mysql/bin/mysql -uroot -p'GP9TKGgY9i/8'

mysql: [Warning] Using a password on the command line interface can be insecure.

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 2

Server version: 5.7.27

Copyright (c) 2000, 2019, 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> exit

 

 

 

 

 

 

 

 

 

 

 

 

 

 

修改密码

 

[root@mysql-server mysql]# /usr/local/mysql/bin/mysqladmin -u root -

p'GP9TKGgY9i/8' password 'QianFeng@123'

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.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

11.添加环境变量

 

[root@mysql-server mysql]# vim /etc/profile ---添加如下

PATH=$PATH:$HOME/bin:/usr/local/mysql/bin

[root@mysql-server mysql]# source /etc/profile

之后就可以在任何地方使用mysql命令登陆Mysql服务器:

[root@mysql-server mysql]# mysql -uroot -p'QianFeng@123'

mysql: [Warning] Using a password on the command line interface can be insecure.

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 5

Server version: 5.7.27 Source distribution

Copyright (c) 2000, 2019, 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>exit

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

12.配置mysqld服务的管理工具:

[root@mysql-server mysql]# cd /usr/local/mysql/support-files/

[root@mysql-server support-files]# cp mysql.server /etc/init.d/mysqld

[root@mysql-server support-files]# chkconfig --add mysqld

[root@mysql-server support-files]# chkconfig mysqld on

先将原来的进程杀掉

[root@mysql-server ~]# /etc/init.d/mysqld start

Starting MySQL. SUCCESS!

[root@mysql-server ~]# netstat -lntp

Active Internet connections (only servers)

Proto Recv-Q Send-Q Local Address Foreign Address State

PID/Program name

tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN

1087/sshd

tcp6 0 0 :::22 :::* LISTEN

1087/sshd

tcp6 0 0 :::3306 :::* LISTEN

31249/mysqld

[root@mysql-server ~]# /etc/init.d/mysqld stop

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

数据库编译安装完成.