Linux下MySQL数据库的简单了解
MySQL数据库的安装
MySQL数据库是我们常用的关系型数据库,属于开源软件,用于存储我们的后台数据等,多用于企业使用,而MySQL数据库的安装也是极其简单的。
我们可以在MySQL官网上去下载其软件包,拥有软件包之后就可以进行安装了,
我这里是拥有MySQL的tar包,而且我已将其减压
[root@haha mysql]# ls
mysql-community-client-5.7.17-1.el7.x86_64.rpm
mysql-community-common-5.7.17-1.el7.x86_64.rpm
mysql-community-devel-5.7.17-1.el7.x86_64.rpm
mysql-community-embedded-5.7.17-1.el7.x86_64.rpm
mysql-community-embedded-compat-5.7.17-1.el7.x86_64.rpm
mysql-community-embedded-devel-5.7.17-1.el7.x86_64.rpm
mysql-community-libs-5.7.17-1.el7.x86_64.rpm
mysql-community-libs-compat-5.7.17-1.el7.x86_64.rpm
mysql-community-minimal-debuginfo-5.7.17-1.el7.x86_64.rpm
mysql-community-server-5.7.17-1.el7.x86_64.rpm
mysql-community-test-5.7.17-1.el7.x86_64.rpm
解压出来之后就会是这写rpm软件包,此时我们就可以利用yum或者apt进行软件包的安装;
[root@haha mysql]# yum install -y ./*
进行安装的时候要注意路径的问题,我这里是再解的tar包里面,所以利用的是”./*“的绝对路径,代表的是当前路径下的所有文件。
完毕之后需要开启以下MySQL,这次才会在他的日志之中出现系统默认的密码,我们才能完成首次登录;
#进行服务的开启:
[root@haha ~]# service mysqld start
Redirecting to /bin/systemctl start mysqld.service
#去MySQL的日志中查看root的初始密码:
[root@haha ~]# ls /var/log/mysqld.log | grep "password"
[root@haha ~]# cat /var/log/mysqld.log | grep "password"
2022-11-09T08:10:42.269655Z 1 [Note] A temporary password is generated for root@localhost: d18h%jt;r?Sp
2022-11-09T08:11:52.646236Z 3 [Note] Access denied for user 'root'@'localhost' (using password: YES)
就可以获取到MySQL数据库的初始密码了,然后我们进入MySQL中进行密码的修改。
简单了解MySQL结构和使用
在Linux系统中安装MySQL数据库,而在数据库中创建表格会自动在MySQL数据库的文件夹中生成存储文件,生成的存储文件的个数取决于你创建表格所使用的引擎,一般默认使用的innoDB引擎所生成以共享表空间文件(ibdata1)、独占表空间文件(ibd)、表结构文件(.frm)、以及日志文件(redo文件等)组成。
那我们由此来演示一下在数据库中创建一个表,他的数据会存储在哪里;
首先我们进入数据库:
[root@haha mysql]# mysql -hlocalhost -uroot -p用户密码 #这里我登陆的是本机的数据库所以是localhost而使用的用户是root,-p的话就是自己为root设立的密码
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 8
Server version: 5.7.17 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, 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>
#进入数据库之后我们可以进行库的创建:
mysql> create database test;
Query OK, 1 row affected (0.00 sec)
#然后我们进入创建的库中:
mysql> use test;
Database changed
#利用命令进行表格的创建,并且指定InnoDB引擎:
mysql> create table day1 (name char(100) , age int(30)) ENGINE = InnoDB;
Query OK, 0 rows affected (0.00 sec)
#表格创建完成后进行数据的插入:
mysql> insert into day1(name , age) values("zhumei" , 20);
Query OK, 1 row affected (0.01 sec)
mysql> insert into day1(name,age) values("huixin",22);
Query OK, 1 row affected (0.00 sec)
此时我们就已经完成的表格的创建,但是在Linux系统中默认会有一个/var/lib的ySQL文 件夹,用来存储数据库中的数据文件,我们需要查看这个表的数据文件的话,就需要去这里路径中;
[root@haha ~]# ls /var/lib/mysql
auto.cnf client-key.pem ib_logfile1 mysql.sock public_key.pem tedu
ca-key.pem ib_buffer_pool ibtmp1 mysql.sock.lock server-cert.pem test
ca.pem ibdata1 lele performance_schema server-key.pem
client-cert.pem ib_logfile0 mysql private_key.pem sys
进入之后就能看见之中的数据,我们会发现这其中有我们刚才创建的那个库test,那么我们进去就可以看到我们的表文件了;
[root@haha ~]# ls /var/lib/mysql/test/
day1.frm day1.ibd db.opt
那么默认InnoDB就会创建出这三个文件。
idb是表空间文件,一般的删除数据不会真正的把数据从表中删除,会转换成碎片。
frm文件是用来保存每个数据表的元数据信息,包括表结构的定义等。
opt文件主要用来存储当前数据库的默认字符集和字符校验规则。
而我们在进行数据的备份恢复时会用到这些文件