ĐĂNG KÝ ĐỂ NHẬN THÔNG TIN MỚI NHẤT

Tài liệu cài đặt Mysql và tự học với bộ dữ liệu mẫu và các lệnh cơ bản trên hệ điều hành Oracle Linux 7.7

Trong bài viết này tôi sẽ giúp bạn quy trình cài đặt MySQL để tự học  với các nội dung chính:

* Cài đặt MySQL trên Oracle Linux 7.7

* Tạo dữ liệu mẫu phục vụ cho việc dev/test

* Một số câu lệnh cơ bản thao tác với database

1. Download bộ software cài đặt MySQL

Download bộ cài đặt theo link : https://dev.mysql.com/downloads/mysql/5.5.html?os=31&version=5.1

Lựa chọn thông số để tải về như sau:

2. Tiến hành cài đặt MySQL

Thực hiện download và cài các package sau

Sau khi download các package cân thiết ở bước 1, ta thực hiện cài đặt các package đó như sau

  • Giải nén file package đã download:
[root@OL-LAB home]#tar -xvf mysql-8.0.31-1.el7.x86_64.rpm-bundle.tar
mysql-community-client-8.0.31-1.el7.x86_64.rpm
mysql-community-client-plugins-8.0.31-1.el7.x86_64.rpm
mysql-community-common-8.0.31-1.el7.x86_64.rpm
mysql-community-debuginfo-8.0.31-1.el7.x86_64.rpm
mysql-community-devel-8.0.31-1.el7.x86_64.rpm
mysql-community-embedded-compat-8.0.31-1.el7.x86_64.rpm
mysql-community-icu-data-files-8.0.31-1.el7.x86_64.rpm
mysql-community-libs-8.0.31-1.el7.x86_64.rpm
mysql-community-libs-compat-8.0.31-1.el7.x86_64.rpm
mysql-community-server-8.0.31-1.el7.x86_64.rpm
mysql-community-server-debug-8.0.31-1.el7.x86_64.rpm
mysql-community-test-8.0.31-1.el7.x86_64.rpm

  • Cài đặt các package :
[root@OL-LAB home]# rpm -Uvh mysql-community-common-8.0.31-1.el7.x86_64.rpm 

warning: mysql-community-common-8.0.31-1.el7.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID 3a79bd29: NOKEY

Preparing...                          ################################# [100%]

Updating / installing...

   1:mysql-community-common-8.0.31-1.e################################# [100%]

[root@OL-LAB home]# rpm -Uvh mysql-community-client-plugins-8.0.31-1.el7.x86_64.rpm 

warning: mysql-community-client-plugins-8.0.31-1.el7.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID 3a79bd29: NOKEY

Preparing...                          ################################# [100%]

Updating / installing...

   1:mysql-community-client-plugins-8.################################# [100%]

[root@OL-LAB home]# rpm -Uvh mysql-community-libs-*

warning: mysql-community-libs-8.0.31-1.el7.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID 3a79bd29: NOKEY

Preparing...                          ################################# [100%]

Updating / installing...

   1:mysql-community-libs-8.0.31-1.el7################################# [ 33%]

   2:mysql-community-libs-compat-8.0.3################################# [ 67%]

Cleaning up / removing...

   3:mariadb-libs-1:5.5.64-1.el7      ################################# [100%]

[root@OL-LAB home]# rpm -Uvh mysql-community-client-8.0.31-1.el7.x86_64.rpm 

warning: mysql-community-client-8.0.31-1.el7.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID 3a79bd29: NOKEY

Preparing...                          ################################# [100%]

Updating / installing...

   1:mysql-community-client-8.0.31-1.e################################# [100%]

[root@OL-LAB home]# rpm -Uvh mysql-community-server-8.0.31-1.el7.x86_64.rpm 

warning: mysql-community-server-8.0.31-1.el7.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID 3a79bd29: NOKEY

error: Failed dependencies:

mysql-community-icu-data-files = 8.0.31-1.el7 is needed by mysql-community-server-8.0.31-1.el7.x86_64

[root@OL-LAB home]# rpm -Uvh mysql-community-icu-data-files-8.0.31-1.el7.x86_64.rpm 

warning: mysql-community-icu-data-files-8.0.31-1.el7.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID 3a79bd29: NOKEY

Preparing...                          ################################# [100%]

Updating / installing...

   1:mysql-community-icu-data-files-8.################################# [100%]

[root@OL-LAB home]# rpm -Uvh mysql-community-server-8.0.31-1.el7.x86_64.rpm 

warning: mysql-community-server-8.0.31-1.el7.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID 3a79bd29: NOKEY

Preparing...                          ################################# [100%]

Updating / installing...

   1:mysql-community-server-8.0.31-1.e################################# [100%]

3. Thực hiện cấu hình và bật cơ sở dữ liệu MySQL

Bước 1: Bật cơ sở dữ liệu MySQL

[root@OL-LAB home]# systemctl enable mysqld

[root@OL-LAB home]# systemctl start  mysqld

[root@OL-LAB home]# systemctl status  mysqld

? mysqld.service - MySQL Server

   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)

   Active: active (running) since Wed 2022-11-09 16:14:15 +07; 7s ago

     Docs: man:mysqld(8)

           http://dev.mysql.com/doc/refman/en/using-systemd.html

  Process: 56037 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)

 Main PID: 56065 (mysqld)

   Status: "Server is operational"

    Tasks: 39

   CGroup: /system.slice/mysqld.service

           +-56065 /usr/sbin/mysqld


Nov 09 16:14:12 OL-LAB systemd[1]: Starting MySQL Server...

Nov 09 16:14:15 OL-LAB systemd[1]: Started MySQL Server.

Bước 2: Tạo các thư mục lưu trữ dữ lệu và binary logs

[root@OL-LAB ~]# mkdir -p /u01/mysql

[root@OL-LAB ~]# mkdir -p /u01/mysql/data

[root@OL-LAB ~]# mkdir -p /u01/mysql/log_bin

[root@OL-LAB ~]# mkdir -p /u01/mysql/tmpdir

[root@OL-LAB ~]# chown -R mysql:mysql /u01/mysql/

[root@OL-LAB ~]# chmod -R 755 /u01/mysql

Bước 3:  Chỉnh cấu hình để nhận các thư mục vừa chọn bên trên

[root@OL-LAB ~]# cat /etc/my.cnf

user=mysql

datadir=/u01/mysql/data

log_bin=/u01/mysql/log_bin

tmpdir=/u01/mysql/tmpdir

Bước 4: Restart MySQL

[root@OL-LAB ~]# systemctl stop mysqld

[root@OL-LAB ~]# systemctl start mysqld

4. Đổi mật khẩu user quản trị (root) của Mysql

Khi mới cài đặt xong, hệ thống sẽ tự động sinh ra một mật khẩu Random cho user root.

Kiểm tra mật khẩu này như sau:

[root@OL-LAB ~]# cat /var/log/mysqld.log |grep password

2022-11-09T09:31:47.575919Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: Ozre<Trk8e/I

Tiếp theo ta tiến hành đổi mật khẩu: 

[root@OL-LAB ~]# mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root:  <Chỗ này nhập mật khẩu tạm bên trên Ozre<Trk8e/I > 
The existing password for the user account root has expired. Please set a new password.
New password: 
Enter password for user root: 
The 'validate_password' component is installed on the server.
The subsequent steps will run with the existing configuration
of the component.
Using existing password for root.
Estimated strength of the password: 100 
Change the password for root ? ((Press y|Y for Yes, any other key for No) : n
 ... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : n
 ... skipping.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : n
 ... skipping.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.
All done!

5. Tạo database

Bước 1: Đăng nhập vào Mysql

[root@OL-LAB ~]# su - mysql

Last login: Wed Nov  9 17:00:07 +07 2022 on pts/0

-bash-4.2$ mysql -u root -p

Enter password: 

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

Your MySQL connection id is 24

Server version: 8.0.31 MySQL Community Server - GPL

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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> 

Bước 2: Tạo và kiểm tra database

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

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

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| wecommittest       |
+--------------------+
5 rows in set (0.00 sec)

mysql> use wecommittest;
Database changed

6. Tạo dữ liệu test

Bước 1: Thực hiện download bộ dữ liệu Test.

Bạn có thể download bộ dữ liệu test tại link sau: https://drive.google.com/file/d/16j8jW-WOpVF-sU5yFbmk2OKB7O35brVR/view?usp=sharing

Sử dụng WinSCP đưa bộ dữ liệu mẫu lên folder /data

Bước 2: Khởi tạo dữ liệu mẫu

Đăng nhập vào databasae và thực hiện câu lệnh sau:

mysql> source /data/mysqlsampledatabase.sql
Query OK, 0 rows affected, 1 warning (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 1 row affected (0.00 sec)
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
Query OK, 0 rows affected (0.02 sec)
Query OK, 0 rows affected, 2 warnings (0.22 sec)
Query OK, 122 rows affected (0.04 sec)
Records: 122  Duplicates: 0  Warnings: 0
Query OK, 0 rows affected (0.20 sec)
Query OK, 0 rows affected, 2 warnings (0.37 sec)
Query OK, 23 rows affected (0.02 sec)
Records: 23  Duplicates: 0  Warnings: 0
Query OK, 0 rows affected (0.22 sec)
Query OK, 0 rows affected (0.28 sec)
Query OK, 7 rows affected (0.00 sec)
Records: 7  Duplicates: 0  Warnings: 0
Query OK, 0 rows affected (0.15 sec)
Query OK, 0 rows affected, 3 warnings (0.28 sec)
Query OK, 2996 rows affected (0.29 sec)
Records: 2996  Duplicates: 0  Warnings: 0
Query OK, 0 rows affected (0.17 sec)
Query OK, 0 rows affected, 2 warnings (0.34 sec)
Query OK, 326 rows affected (0.05 sec)
Records: 326  Duplicates: 0  Warnings: 0
Query OK, 0 rows affected (0.19 sec)
Query OK, 0 rows affected, 1 warning (0.22 sec)
Query OK, 273 rows affected (0.03 sec)
Records: 273  Duplicates: 0  Warnings: 0
Query OK, 0 rows affected (0.17 sec)
Query OK, 0 rows affected (0.06 sec)
Query OK, 7 rows affected (0.00 sec)
Records: 7  Duplicates: 0  Warnings: 0
Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected, 1 warning (0.02 sec)
Query OK, 110 rows affected (0.01 sec)
Records: 110  Duplicates: 0  Warnings: 0
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)

Bước 3: Kiểm tra các objects đã được thêm vào bộ dữ liệu mẫu

mysql> show tables;
+-------------------------+
| Tables_in_classicmodels |
+-------------------------+
| customers               |
| employees               |
| offices                 |
| orderdetails            |
| orders                  |
| payments                |
| productlines            |
| products                |
+-------------------------+
8 rows in set (0.01 sec)

7. Một số câu lệnh thao tác cơ bản sau khi cài đặt MySQL

  • Kiệt kê các database
-bash-4.2$ mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 41
Server version: 8.0.31 MySQL Community Server - GPL
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
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           |
+--------------------+
| classicmodels      |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| wecommittest       |
+--------------------+
6 rows in set (0.00 sec)
  • Kết nối đến database
mysql> use wecommittest;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
  • Tạo User
mysql> CREATE USER 'tuantd123'@'OL-LAB' IDENTIFIED BY 'Tuantd@1234';
Query OK, 0 rows affected (0.01 sec)

mysql> select user from user;
+------------------+
| user             |
+------------------+
| mysql.infoschema |
| mysql.session    |
| mysql.sys        |
| root             |
| tuantd           |
| tuantd123        |
+------------------+
6 rows in set (0.00 sec)
  • Liệt kê tables
mysql> show full tables;
+------------------------+------------+
| Tables_in_wecommittest | Table_type |
+------------------------+------------+
| customers | BASE TABLE |
| employees | BASE TABLE |
| offices | BASE TABLE |
| orderdetails | BASE TABLE |
| orders | BASE TABLE |
| payments | BASE TABLE |
| productlines | BASE TABLE |
| products | BASE TABLE |
+------------------------+------------+
8 rows in set (0.01 sec)

  • Xem cấu trúc của 1 tables
mysql> desc customers;
+------------------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------------+---------------+------+-----+---------+-------+
| customerNumber | int | NO | PRI | NULL | |
| customerName | varchar(50) | NO | | NULL | |
| contactLastName | varchar(50) | NO | | NULL | |
| contactFirstName | varchar(50) | NO | | NULL | |
| phone | varchar(50) | NO | | NULL | |
| addressLine1 | varchar(50) | NO | | NULL | |
| addressLine2 | varchar(50) | YES | | NULL | |
| city | varchar(50) | NO | | NULL | |
| state | varchar(50) | YES | | NULL | |
| postalCode | varchar(15) | YES | | NULL | |
| country | varchar(50) | NO | | NULL | |
| salesRepEmployeeNumber | int | YES | MUL | NULL | |
| creditLimit | decimal(10,2) | YES | | NULL | |
+------------------------+---------------+------+-----+---------+-------+
13 rows in set (0.01 sec)


Tại đây chúng ta đã hoàn thành việc Cài đặt MySQL và tạo bộ dữ liệu mẫu cho việc tự học.

8. Một số nội dung nâng cao khác

Views: 1415





    Câu hỏi bảo mật

    Trả lời

    Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *

    Ask ChatGPT
    Set ChatGPT API key
    Find your Secret API key in your ChatGPT User settings and paste it here to connect ChatGPT with your Tutor LMS website.