[MySQL] Environment installation and basic use of MySQL in Linux


Students who need cloud servers and other cloud products to learn Linux can move to / --> Tencent Cloud <-- / --> Alibaba Cloud <-- / --> Huawei Cloud <-- / official website, lightweight cloud servers are low-cost to 112 yuan/year, and new users can enjoy ultra-low discounts on their first order.


 Table of contents

1. Installation of MySQL environment

1. MySQL environment installation

2. Problems when installing MySQL

3. Log in to MySQl

3.1 Option 1

3.2 Option 2 

4. Modify the MySQL configuration file

5. Optional setting: MySQL will start automatically when the computer is turned on (the cloud server will generally not shut down if it is fine)

2. MySQL database basics

1. Some concepts

2. Basic use

2.1 Display a list of all databases in the current MySQL instance

2.2 Create database

2.3 Using database

2.4 Create database table 

2.5 Insert data into the table

2.6 Query data in the table 

3. Classification of SQL 

4. Storage engine


1. Installation of MySQL environment

1. MySQL environment installation

#打印当前机器的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. Problems when installing 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. Log in to MySQl

3.1 Option 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
  • -h: Specify the host name or IP address of the MySQL database to be connected. Here, 127.0.0.1 indicates that the host to be connected is the local host, that is, the MySQL database on the local machine is connected. If -h is not specified, it will connect to MySQL built on the local server by default.
  • -P: Specify the port number of the MySQL database to be connected. Here, 3306 represents the default port number of the MySQL database. If -P is not specified, the port number specified in the configuration file will be used by default to connect to MySQL.
  • -u: Specify the user name to connect to the MySQL database. Here, root means using the root username to connect to the MySQL database.
  • -p: Indicates that a password is required when connecting to the MySQL database. Here, no password is specified after -p, so you will be prompted for a password.

After you enter the complete command and press the Enter key, you will be prompted to enter the password. After entering the correct password, you can connect to the MySQL database.

3.2 Option 2 

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

4. Modify the MySQL configuration file

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

5. Optional setting: MySQL will start automatically when the computer is turned on (the cloud server will generally not shut down if it is fine)

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

2. MySQL database basics

1. Some concepts

MySQL is essentially a network service based on the C(mysql)S(mysqld) mode.

mysqld: It is the server side of the database (it is a daemon process)

mysql: It is the client of the database

A database is a structured data file stored on disk or in memory.

Database service: mysqld

2. Basic use

2.1 Display a list of all databases in the current MySQL instance

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

The database is stored in the /var/lib/mysql directory.

2.2 Create database

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

Creating a database is essentially creating a directory under Linux .

2.3 Using database

mysql> use helloworld;
Database changed

2.4 Create database table 

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

Creating a table in the database is essentially creating the corresponding file in Linux .

2.5 Insert data into the table

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

2.6 Query data in the table 

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

3. Classification of SQL 

DDL [data definition language]: Data definition language, used to maintain the structure of stored data

Representative commands: create, drop, alter

DML [data manipulation language]: Data manipulation language, used to operate data

Representative instructions: insert, delete, update

DML is divided into a separate DQL: data query language, which represents the instruction: select

DCL [Data Control Language]: Data control language, mainly responsible for permission management and transactions

Representative instructions: grant, revoke, commit

4. Storage engine

        The storage engine is the implementation method of how the database management system stores data, how to index the stored data, and how to update and query data.

The core of MySQL is the plug-in storage engine, which supports multiple storage engines.

#查看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)

Guess you like

Origin blog.csdn.net/gfdxx/article/details/131133278
Recommended