The foundation of MySQL database (1)

The origin of database management software

As we learned before, if we want to permanently save some data generated in the program when we write the program, we can only save them to a file and save it to the hard disk, but the program we write can only run in the On a machine, so we need to use the database.

There are several reasons to use a database:

1. It is impossible for all components of the program to run on one machine

#Because once the machine hangs up, it means the entire software will crash, and the execution efficiency of the program depends on the hardware that carries it, and the performance of a machine is always limited, limited by the current hardware level. There is a limit to vertically scaling the performance of a machine.

#So we can only enhance the overall performance of our system through horizontal expansion, which requires us to distribute the various components of the program to multiple machines for execution.

2. Data security issues

#According to the description of 1, we will distribute the various components of the program to each machine, but we need to know that each component is still a whole. The implication is that the data of all components still needs to be shared. However, the components on each machine can only operate the files of this machine, which leads to the inevitable data inconsistency.

#So we thought of separating the data from the application: store the file on one machine, and then use multiple machines to access the file on this machine through the network (implemented by socket), that is, share the file on this machine, Sharing means competition, data insecurity will occur, and locking processing will be required. . . .

3. Concurrency 

According to the description of 2, we must write a socket server to manage the files on this machine (database server), and then write a socket client to complete the following functions:

# 1. Remote connection (support concurrency) 
# 2. Open file 
# 3. Read and write (lock) 
# 4. Close file

Summarize:

#Before we write any program, we need to write a program (socket server and client program) that operates a file on a host based on the network in advance, so some people write such a program as a special processing software, which is mysql, etc. The origin of database management software, but mysql solves not only the problem of data sharing, but also a series of problems such as query efficiency and security. In short, it frees programmers from data management and focuses on writing their own program logic. .

Introduction to Databases

1 What is data (Data) 

Symbolic records describing things are called data. Symbols describing things can be numbers, texts, pictures, images, sounds, languages, etc. Data can be represented in various forms, and they can all be digitized and stored in a computer.

To describe a thing in a computer, it is necessary to extract the typical features of the thing and form a record, which is equivalent to a line of content in a file, such as:

1 egon,male,18,1999,Shandong,Department of Computer Science,2017,oldboy

A simple record does not have any meaning. If we define the meaning of each field in turn by commas, it is equivalent to defining the title of the table.

1 name,sex,age,birth,born_addr,major,entrance_time,school #字段
2 egon, male, 18, 1999, Shandong, Department of Computer Science, 2017, oldboy #record

In this way, we can learn about egon, the gender is male, the age is 18 years old, he was born in 1999, his birthplace is Shandong, and he was admitted to the old boy computer department in 2017.

2 What is a database (DataBase, DB for short)

A database is a warehouse for storing data, but this warehouse is on a computer storage device, and the data is stored in a certain format

In the past, people stored data in file cabinets, but now the amount of data is huge and it is no longer applicable

A database is a long-term storage of data in a computer that is organized and shared.

The data in the database is organized, described and stored according to a certain data model, with less redundancy, high data independence and easy expansion, and can be shared by various users

3 What is a database management system (DataBase Management System referred to as DBMS)

After understanding the concepts of Data and DB, how to organize and store data scientifically, and how to efficiently acquire and maintain data becomes the key

这就用到了一个系统软件---数据库管理系统

如MySQL、Oracle、SQLite、Access、MS SQL Server

mysql主要用于大型门户,例如搜狗、新浪等,它主要的优势就是开放源代码,因为开放源代码这个数据库是免费的,他现在是甲骨文公司的产品。
oracle主要用于银行、铁路、飞机场等。该数据库功能强大,软件费用高。也是甲骨文公司的产品。
sql server是微软公司的产品,主要应用于大中型企业,如联想、方正等。

4 数据库服务器、数据管理系统、数据库、表与记录的关系(重点理解!!!)

记录:1 刘海龙  324245234 22(多个字段的信息组成一条记录,即文件中的一行内容)

表:student,scholl,class_list(即文件)

数据库:oldboy_stu(即文件夹)

数据库管理系统:如mysql(是一个软件)

数据库服务器:一台计算机(对内存要求比较高)

总结:

    数据库服务器-:运行数据库管理软件

    数据库管理软件:管理-数据库

    数据库:即文件夹,用来组织文件/表

    表:即文件,用来存放多行内容/多条记录

一 人工管理阶段

20世纪50年代中期以前,计算机主要用于科学计算。

当时的硬件水平:外存只有纸带、卡片、磁带,没有磁盘等直接存取的存储设备

当时的软件状况:没有操作系统,没有管理数据的软件,数据的处理方式是批处理。

人工管理数据具有以下特点:

1 数据不保存:计算机主要用于科学计算,数据临时用,临时输入,不保存

2 应用程序管理数据:数据要有应用程序自己管理,应用程序需要处理数据的逻辑+物理结构,开发负担很重

3 数据不共享:一组数据只对应一个程序,多个程序之间涉及相同数据时,必须各自定义,造成数据大量冗余

4 数据不具有独立性:数据的逻辑结构或物理结构发生变化后,必须对应用程序做出相应的修改,开发负担进一步加大



二 文件系统阶段

20世纪50年代后期到60年代中期

硬件水平:有了磁盘、磁鼓等可直接存取的存储设备

软件水平:有了操作系统,并且操作系统中已经有了专门的数据管理软件,即文件系统;处理方式上不仅有了批处理,而且能够联机实时处理

文件系统管理数据具有以下优点:

1 数据可以长期保存:计算机大量用于数据处理,因而数据需要长期保存,进行增删改查操作

2 由文件系统管理数据:文件系统这个软件,把数据组织成相对独立的数据文件,利用按文件名,按记录进行存取。实现了记录内的结构性,但整体无结构。并且程序与数据之间由文件系统提供存取方法进行转换,是应用程序与数据之间有了一定的独立性,程序员可以不必过多考虑物理细节。

文件系统管理数据具有以下缺点:

1 数据共享性差,冗余度大:一个文件对应一个应用程序,不同应用有相同数据时,也必须建立各自的文件,不能共享相同的数据,造成数据冗余,浪费空间,且相同的数据重复存储,各自管理,容易造成数据不一致性

2 数据独立性差:一旦数据的逻辑结构改变,必须修改应用程序,修改文件结构的定义。应用程序的改变,也将引起文件的数据结构的改变。因此数据与程序之间缺乏独立性。可见,文件系统仍然是一个不具有弹性的无结构的数据集合,即文件之间是孤立的,不能反映现实世界事物之间的内存联系。



三 数据系统阶段

20世纪60年代后期以来,计算机用于管理的规模越来越大,应用越来越广泛,数据量急剧增长,同时多种应用,多种语言互相覆盖地共享数据结合要求越来越强烈

硬件水平:有了大容量磁盘,硬件架构下降

软件水平:软件价格上升(开发效率必须提升,必须将程序员从数据管理中解放出来),分布式的概念盛行。

数据库系统的特点:

1 数据结构化(如上图odboy_stu)

2 数据共享,冗余度低,易扩充

3 数据独立性高

4 数据由DBMS统一管理和控制

  a:数据的安全性保护

  b:数据的完整性检查

  c:并发控制

  d:数据库恢复
数据库发展历程

MySQL简介

MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下公司。MySQL 最流行的关系型数据库管理系统,在 WEB 应用方面MySQL是最好的 RDBMS (Relational Database Management System,关系数据库管理系统) 应用软件之一。

1、mysql

#mysql就是一个基于socket编写的C/S架构的软件
#客户端软件
  mysql自带:如mysql命令,mysqldump命令等
  python模块:如pymysql

2、数据库管理软件分类

#分两大类:
  关系型:如sqllite,db2,oracle,access,sql server,MySQL,注意:sql语句通用
  非关系型:mongodb,redis,memcache

#可以简单的理解为:
    关系型数据库需要有表结构,存取数据前必须先定义表结构,存数据必须按照字段的类型或约束
    非关系型数据库是key-value存储的,没有表结构

3、下载安装

Linux安装

1.解压tar包
cd /software
tar -xzvf mysql-5.6.21-linux-glibc2.5-x86_64.tar.gz
mv mysql-5.6.21-linux-glibc2.5-x86_64 mysql-5.6.21

2.添加用户与组
groupadd mysql
useradd -r -g mysql mysql
chown -R mysql:mysql mysql-5.6.21

3.安装数据库
su mysql
cd mysql-5.6.21/scripts
./mysql_install_db --user=mysql --basedir=/software/mysql-5.6.21 --datadir=/software/mysql-5.6.21/data

4.配置文件
cd /software/mysql-5.6.21/support-files
cp my-default.cnf /etc/my.cnf
cp mysql.server /etc/init.d/mysql
vim /etc/init.d/mysql   #若mysql的安装目录是/usr/local/mysql,则可省略此步
修改文件中的两个变更值
basedir=/software/mysql-5.6.21
datadir=/software/mysql-5.6.21/data

5.配置环境变量
vim /etc/profile
export MYSQL_HOME="/software/mysql-5.6.21"
export PATH="$PATH:$MYSQL_HOME/bin"
source /etc/profile

6.添加自启动服务
chkconfig --add mysql
chkconfig mysql on

7.启动mysql
service mysql start

8.登录mysql及改密码与配置远程访问
mysqladmin -u root password 'your_password'     #修改root用户密码
mysql -u root -p     #登录mysql,需要输入密码
mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'your_password' WITH GRANT OPTION;     #允许root用户远程访问
mysql>FLUSH PRIVILEGES;     #刷新权限
安装mysql
1. 解压
tar zxvf  mariadb-5.5.31-linux-x86_64.tar.gz   
mv mariadb-5.5.31-linux-x86_64 /usr/local/mysql //必需这样,很多脚本或可执行程序都会直接访问这个目录

2. 权限
groupadd mysql             //增加 mysql 属组 
useradd -g mysql mysql     //增加 mysql 用户 并归于mysql 属组 
chown mysql:mysql -Rf  /usr/local/mysql    // 设置 mysql 目录的用户及用户组归属。 
chmod +x -Rf /usr/local/mysql    //赐予可执行权限 

3. 拷贝配置文件
cp /usr/local/mysql/support-files/my-medium.cnf /etc/my.cnf     //复制默认mysql配置 文件到/etc目录 

4. 初始化
/usr/local/mysql/scripts/mysql_install_db --user=mysql          //初始化数据库 
cp  /usr/local/mysql/support-files/mysql.server    /etc/init.d/mysql    //复制mysql服务程序 到系统目录 
chkconfig  mysql on     //添加mysql 至系统服务并设置为开机启动 
service  mysql  start  //启动mysql

5. 环境变量配置
vim /etc/profile   //编辑profile,将mysql的可执行路径加入系统PATH
export PATH=/usr/local/mysql/bin:$PATH 
source /etc/profile  //使PATH生效。

6. 账号密码
mysqladmin -u root password 'yourpassword' //设定root账号及密码
mysql -u root -p  //使用root用户登录mysql
use mysql  //切换至mysql数据库。
select user,host,password from user; //查看系统权限
drop user ''@'localhost'; //删除不安全的账户
drop user root@'::1';
drop user root@127.0.0.1;
select user,host,password from user; //再次查看系统权限,确保不安全的账户均被删除。
flush privileges;  //刷新权限

7. 一些必要的初始配置
1)修改字符集为UTF8
vi /etc/my.cnf
在[client]下面添加 default-character-set = utf8
在[mysqld]下面添加 character_set_server = utf8
2)增加错误日志
vi /etc/my.cnf
在[mysqld]下面添加:
log-error = /usr/local/mysql/log/error.log
general-log-file = /usr/local/mysql/log/mysql.log
3) 设置为不区分大小写,linux下默认会区分大小写。
vi /etc/my.cnf
在[mysqld]下面添加:
lower_case_table_name=1

修改完重启:#service  mysql  restart

源码安装mariadb
安装mariadb

Windows安装

#1、下载:MySQL Community Server 5.7.16
http://dev.mysql.com/downloads/mysql/

#2、解压
如果想要让MySQL安装在指定目录,那么就将解压后的文件夹移动到指定目录,如:C:\mysql-5.7.16-winx64

#3、添加环境变量
【右键计算机】--》【属性】--》【高级系统设置】--》【高级】--》【环境变量】--》【在第二个内容框中找到 变量名为Path 的一行,双击】 --> 【将MySQL的bin目录路径追加到变值值中,用 ; 分割】
 
#4、初始化
mysqld --initialize-insecure

#5、启动MySQL服务
mysqld # 启动MySQL服务

#6、启动MySQL客户端并连接MySQL服务
mysql -u root -p # 连接MySQL服务器
安装
上一步解决了一些问题,但不够彻底,因为在执行【mysqd】启动MySQL服务器时,当前终端会被hang住,那么做一下设置即可解决此问题:



注意:--install前,必须用mysql启动命令的绝对路径
# 制作MySQL的Windows服务,在终端执行此命令:
"c:\mysql-5.7.16-winx64\bin\mysqld" --install
 
# 移除MySQL的Windows服务,在终端执行此命令:
"c:\mysql-5.7.16-winx64\bin\mysqld" --remove



注册成服务之后,以后再启动和关闭MySQL服务时,仅需执行如下命令:
# 启动MySQL服务
net start mysql
 
# 关闭MySQL服务
net stop mysql
将MySQL服务端制成Windows服务

4、MySQL基本管理

1、启动查看

[root@egon ~]# systemctl start mariadb #启动
[root@egon ~]# systemctl enable mariadb #设置开机自启动
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
[root@egon ~]# ps aux |grep mysqld |grep -v grep #查看进程,mysqld_safe为启动mysql的脚本文件,内部调用mysqld命令
mysql     3329  0.0  0.0 113252  1592 ?        Ss   16:19   0:00 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
mysql     3488  0.0  2.3 839276 90380 ?        Sl   16:19   0:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock
[root@egon ~]# netstat -an |grep 3306 #查看端口
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN  
[root@egon ~]# ll -d /var/lib/mysql #权限不对,启动不成功,注意user和group
drwxr-xr-x 5 mysql mysql 4096 Jul 20 16:28 /var/lib/mysql
Linux平台查看
安装完mysql 之后,登陆以后,不管运行任何命令,总是提示这个
mac mysql error You must reset your password using ALTER USER statement before executing this statement.
解决方法:
step 1: SET PASSWORD = PASSWORD('your new password');
step 2: ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;
step 3: flush privileges;
View Code

2、登录、设置密码

初始状态下,管理员root,密码为空,默认只允许从本机登录localhost
设置密码
[root@egon ~]# mysqladmin -uroot password "123"        设置初始密码 由于原密码为空,因此-p可以不用
[root@egon ~]# mysqladmin -uroot -p"123" password "456"        修改mysql密码,因为已经有密码了,所以必须输入原密码才能设置新密码

命令格式:
[root@egon ~]# mysql -h172.31.0.2 -uroot -p456
[root@egon ~]# mysql -uroot -p
[root@egon ~]# mysql                    以root用户登录本机,密码为空

3、破解密码

Linux破解方式

[root@egon ~]# rm -rf /var/lib/mysql/mysql #所有授权信息全部丢失!!!
[root@egon ~]# systemctl restart mariadb
[root@egon ~]# mysql
第一种:删除授权库mysql,重新初始化
第二种:启动时,跳过授权库

Windows破解方式

#1 关闭mysql
#2 在cmd中执行:mysqld --skip-grant-tables
#3 在cmd中执行:mysql
#4 执行如下sql:
update mysql.user set authentication_string=password('') where user = 'root';
flush privileges;

#5 tskill mysqld #或taskkill -f /PID 7832
#6 重新启动mysql
第一种
#1. 关闭mysql,可以用tskill mysqld将其杀死
#2. 在解压目录下,新建mysql配置文件my.ini
#3. my.ini内容,指定
[mysqld]
skip-grant-tables

#4.启动mysqld
#5.在cmd里直接输入mysql登录,然后操作
update mysql.user set authentication_string=password('') where user='root and host='localhost';

flush privileges;

#6.注释my.ini中的skip-grant-tables,然后启动myqsld,然后就可以以新密码登录了
第二种

在Windows下为MySQL服务指定配置文件

强调:配置文件中的注释可以有中文,但是配置项中不能出现中文

#在mysql的解压目录下,新建my.ini,然后配置
#1. 在执行mysqld命令时,下列配置会生效,即mysql服务启动时生效
[mysqld]
;skip-grant-tables
port=3306
character_set_server=utf8
default-storage-engine=innodb
innodb_file_per_table=1


#解压的目录
basedir=E:\mysql-5.7.19-winx64
#data目录
datadir=E:\my_data #在mysqld --initialize时,就会将初始数据存入此处指定的目录,在初始化之后,启动mysql时,就会去这个目录里找数据



#2. 针对客户端命令的全局配置,当mysql客户端命令执行时,下列配置生效
[client]
port=3306
default-character-set=utf8
user=root
password=123

#3. 只针对mysql这个客户端的配置,2中的是全局配置,而此处的则是只针对mysql这个命令的局部配置
[mysql]
;port=3306
;default-character-set=utf8
user=egon
password=4573


#!!!如果没有[mysql],则用户在执行mysql命令时的配置以[client]为准
my.ini

统一字符编码

#1. 修改配置文件
[mysqld]
default-character-set=utf8 
[client]
default-character-set=utf8 
[mysql]
default-character-set=utf8

#mysql5.5以上:修改方式有所改动
[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8

#2. 重启服务
#3. 查看修改结果:
\s
show variables like '%char%'

sql语句

mysql服务端软件即mysqld帮我们管理好文件夹以及文件,前提是作为使用者的我们,需要下载mysql的客户端,或者其他模块来连接到mysqld,然后使用mysql软件规定的语法格式去提交自己命令,实现对文件夹或文件的管理。该语法即sql(Structured Query Language 即结构化查询语言)

SQL语言主要用于存取数据、查询数据、更新数据和管理关系数据库系统,SQL语言由IBM开发。SQL语言分为3种类型:
#1、DDL语句    数据库定义语言: 数据库、表、视图、索引、存储过程,例如CREATE DROP ALTER
#2、DML语句    数据库操纵语言: 插入数据INSERT、删除数据DELETE、更新数据UPDATE、查询数据SELECT
#3、DCL语句    数据库控制语言: 例如控制用户的访问权限GRANT、REVOKE
#1. 操作文件夹
        增:create database db1 charset utf8;
        查:show databases;
        改:alter database db1 charset latin1;
        删除: drop database db1;


#2. 操作文件
    先切换到文件夹下:use db1
        增:create table t1(id int,name char);
        查:show tables
        改:alter table t1 modify name char(3);
              alter table t1 change name name1 char(2);
        删:drop table t1;
    

#3. 操作文件中的内容/记录
        增:insert into t1 values(1,'egon1'),(2,'egon2'),(3,'egon3');
        查:select * from t1;
        改:update t1 set name='sb' where id=2;
        删:delete from t1 where id=1;

        清空表:
            delete from t1; #如果有自增id,新增的数据,仍然是以删除前的最后一样作为起始。
            truncate table t1;数据量大,删除速度比上一条快,且直接从零开始,

            auto_increment 表示:自增
            primary key 表示:约束(不能重复且不能为空);加速查找
View Code

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325564670&siteId=291194637
Recommended