【MySQL】MySQL在Linux中的环境安装与基本使用-LMLPHP


目录

一、MySQL环境的安装

1、MySQL环境安装

2、安装MySQL出现的问题

3、登录MySQl

3.1方案一

3.2方案二 

4、修改MySQL配置文件

5、可选设置:开机自启动MySQL(云服务器没事一般不会关机)

二、MySQL数据库基础

1、一些概念

2、基本使用

2.1显示当前 MySQL 实例中所有的数据库列表

2.2创建数据库

2.3使用数据库

2.4创建数据库表 

2.5在表中插入数据

2.6在表中查询数据 

3、SQL的分类 

4、存储引擎


一、MySQL环境的安装

1、MySQL环境安装

#打印当前机器的Linux版本
[root@VM-4-11-centos ~]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core) 
#在官网下载和Linux版本对应的rpm包
http://repo.mysql.com/
#安装rpm包
rpm -ivh mysql57-community-release-el7.rpm
#查看Linux的yum源是否被安装好
[root@VM-4-11-centos MySQL]# ls /etc/yum.repos.d/ -l
total 24
-rw-r--r-- 1 root root 1838 Apr 27  2017 mysql-community.repo
-rw-r--r-- 1 root root 1885 Apr 27  2017 mysql-community-source.repo
#使用yum安装mysql
yum install -y mysql-community-server
#查看MySQL是否安装成功
[root@VM-4-11-centos MySQL]# ls /etc/my.cnf #查看MySQL配置文件是否存在
/etc/my.cnf
[root@VM-4-11-centos MySQL]# which mysqld #查看MySQL服务端是否存在
/sbin/mysqld
[root@VM-4-11-centos MySQL]# which mysql #查看MySQL客户端是否存在
/bin/mysql
#启动MySQL服务器
systemctl start mysqld

2、安装MySQL出现的问题

安装遇到秘钥过期的问题:
Failing package is: mysql-community-common-5.7.42-1.el7.x86_64
GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
解决⽅案:
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022

3、登录MySQl

3.1方案一

#获取临时密码
[root@VM-4-11-centos MySQL]# sudo grep 'temporary password' /var/log/mysqld.log
2023-06-07T02:58:34.085200Z 1 [Note] A temporary password is generated for root@localhost: kQs***7OrS_M
#使用临时密码登录
[root@VM-4-11-centos MySQL]# mysql -h 127.0.0.1 -P 3306 -u root -p
Enter password: 
#退出MySQl
\q

3.2方案二 

#打开MySQl配置文件
vim /etc/my.cnf
#尾行加入此句
skip-grant-tables
#重启MySQl服务
systemctl restart mysqld
#直接登录
mysql -uroot

4、修改MySQL配置文件

#vim打开配置文件
vim /etc/my.cnf
#新增如下字段
port=3306
character-set-server=utf8
default-storage-engine=innodb
#重启MySQl服务,生效配置
systemctl restart mysqld

5、可选设置:开机自启动MySQL(云服务器没事一般不会关机)

#开启开机自启动
systemctl enable mysqld
systemctl daemon-reload

二、MySQL数据库基础

1、一些概念

MySQL本质是基于C(mysql)S(mysqld)模式的一种网络服务。

2、基本使用

2.1显示当前 MySQL 实例中所有的数据库列表

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.02 sec)

2.2创建数据库

mysql> create database helloworld;
Query OK, 1 row affected (0.00 sec)

【MySQL】MySQL在Linux中的环境安装与基本使用-LMLPHP

创建数据库,本质上就是在创建Linux下创建一个目录

2.3使用数据库

mysql> use helloworld;
Database changed

2.4创建数据库表 

mysql> create table students(
    -> name varchar(32),
    -> age int,
    -> gender varchar(2)
    -> );
Query OK, 0 rows affected (0.26 sec)

【MySQL】MySQL在Linux中的环境安装与基本使用-LMLPHP

在数据库中建立表,本质上就是在Linux中创建对应的文件

2.5在表中插入数据

mysql> insert into students (name, age, gender) values ('张三',22,'男');
Query OK, 1 row affected (0.05 sec)

2.6在表中查询数据 

mysql> select* from students;
+--------+------+--------+
| name   | age  | gender |
+--------+------+--------+
| 张三   |   22 | 男     |
| 王五   |   23 | 女     |
| 王五   |   23 | 女     |
+--------+------+--------+
3 rows in set (0.01 sec)

3、SQL的分类 

4、存储引擎

        存储引擎是:数据库管理系统如何存储数据、如何为存储的数据建立索引和如何更新、查询数据等技术的实现方法。

MySQL的核心就是插件式存储引擎,支持多种存储引擎。

#查看MySQL的存储引擎
mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine             | Support | Comment                                                        | Transactions | XA   | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| InnoDB             | DEFAULT | Supports transactions, row-level locking, and foreign keys     | YES          | YES  | YES        |
| MRG_MYISAM         | YES     | Collection of identical MyISAM tables                          | NO           | NO   | NO         |
| MEMORY             | YES     | Hash based, stored in memory, useful for temporary tables      | NO           | NO   | NO         |
| BLACKHOLE          | YES     | /dev/null storage engine (anything you write to it disappears) | NO           | NO   | NO         |
| MyISAM             | YES     | MyISAM storage engine                                          | NO           | NO   | NO         |
| CSV                | YES     | CSV storage engine                                             | NO           | NO   | NO         |
| ARCHIVE            | YES     | Archive storage engine                                         | NO           | NO   | NO         |
| PERFORMANCE_SCHEMA | YES     | Performance Schema                                             | NO           | NO   | NO         |
| FEDERATED          | NO      | Federated MySQL storage engine                                 | NULL         | NULL | NULL       |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec)
06-09 23:01