Cài đặt Mysql 8 trên Amazon linux 2023

Photo of author

By Admin

Tiếp theo bài viết cài đặt LEMP stack trên Amazon Linux 2023, bài viết này chúng ta sẽ tiến hành cài đặt Mysql 8 trên Amazon Linux 2023.

Tiến hành cài đặt Mysql 8 trên Amazon Linux 2023

Đầu tiên, kiểm tra phiên bản hệ điều hành bạn đang sử dụng, ở đây mình sử dùng HĐH fedora

[ec2-user@ip-172-31-32-xxx ~]$ cat /etc/os-release
NAME="Amazon Linux"
VERSION="2023"
ID="amzn"
ID_LIKE="fedora"
VERSION_ID="2023"
PLATFORM_ID="platform:al2023"
PRETTY_NAME="Amazon Linux 2023"
ANSI_COLOR="0;33"
CPE_NAME="cpe:2.3:o:amazon:amazon_linux:2023"
HOME_URL="https://aws.amazon.com/linux/"
BUG_REPORT_URL="https://github.com/amazonlinux/amazon-linux-2023"
SUPPORT_END="2028-03-01"

Thêm Mysql repository

Trước tiên, chúng ta cần thêm kho lưu trữ MySQL Yum vào danh sách kho lưu trữ của máy chủ Amazon Linux 2023. Thao tác này chỉ được thực hiện một lần và cung cấp tất cả các kho phiên bản gói MySQL.

[ec2-user@ip-172-31-32-xxx ~]$ pwd
/home/ec2-user

Sử dung wget để tải về repository bằng command:

sudo wget https://dev.mysql.com/get/mysql80-community-release-el9-1.noarch.rpm 

Nhập y để confirm tải package:

Dependencies Resolved

======================================================================================================================================================================================================
 Package                                               Arch                               Version                           Repository                                                           Size
======================================================================================================================================================================================================
Installing:
 mysql80-community-release                             noarch                             el7-5                             /mysql80-community-release-el7-5.noarch                             9.1 k

Transaction Summary
======================================================================================================================================================================================================
Install  1 Package

Total size: 9.1 k
Installed size: 9.1 k
Is this ok [y/d/N]: y

Kiểm tra repo đã tải thành công chưa với câu lệnh

sudo ls -lrt
[ec2-user@ip-172-31-32-xxx ~]$ sudo ls -lrt
total 12
-rw-r--r--. 1 root root 10534 Jul 25  2022 mysql80-community-release-el9-1.noarch.rpm

Kiểm tra các yum repository đã có trong Amazon Linux 2023 với lệnh:

sudo yum repolist
ls /etc/yum.repos.d

Kết quả hiển thị các repository có trong Amazon Linux 2023 của bạn:

[ec2-user@ip-172-31-32-xxx ~]$ ls /etc/yum.repos.d
amazonlinux.repo       mysql-community-debuginfo.repo  mysql-community.repo
kernel-livepatch.repo  mysql-community-source.repo

Cài đặt Mysql 8.0

sudo dnf install mysql80-community-release-el9-1.noarch.rpm
dnf repolist enabled | grep "mysql.*-community.*"
sudo dnf install mysql-community-server

Sau khi hoàn tất các câu lệnh cài đặt, ta thực hiện start mysql:

sudo systemctl start mysqld

Kiểm tra phiên bản mysql hiện tại:

[ec2-user@ip-172-31-32-xxx]$ sudo mysql -V
mysql  Ver 8.0.34 for Linux on x86_64 (MySQL Community Server - GPL)

Thiết lập cấu hình cho Mysql 8

Đổi mật khẩu cho người dùng root

Như bạn đã biết, từ Mysql 8.0 thì việc sử dụng đăng nhập không dùng mật khẩu với người dùng root đã bị loại bỏ. Vậy để tìm mật khẩu cho người dùng root khi đăng nhập lần đầu ta sẽ tìm trong file log của mysql. Mật khẩu tạm thời được tìm thấy khi thực hiện câu lệnh:

sudo grep 'temporary password' /var/log/mysqld.log
[ec2-user@ip-172-31-32-xxx ~]$  sudo grep 'temporary password' /var/log/mysqld.log
2023-07-28T09:06:43.641665Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: D6%=yjkDGnp8

Tiếp theo ta thực hiện đăng nhập vào mysql và nhập password tạm thời của người dùng root trong câu lệnh bên dưới: (Mật khẩu root: D6%=yjkDGnp8)

sudo mysql_secure_installation -p

Nhập mật khẩu mới cho người dùng root và nhập y cuối mỗi câu để loại bỏ các user test.

Lưu ý khi đặt mật khẩu cần tuân theo quy tắt sau:

  • Chứa ít nhất 1 ký tự in hoa
  • Chứa ít nhất 1 ký tự thường
  • Chứa ít nhất 1 số
  • Chứa ít nhất 1 ký tự đặc biệt
  • Tổng số ký tự phải lớn hơn 8
Securing the MySQL server deployment.


The existing password for the user account root has expired. Please set a new password.

New password: <MẬT KHẨU MỚI>

Re-enter new password: <NHẬP LẠI MẬT KHẨU>
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) :

 ... 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) : y
Success.

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) : y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

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!

Sau khi hoàn tất bước đổi mật khẩu ở trên, chúng ta có thể thực hiện đăng nhập lại với mật khẩu vừa đổi:

mysql -uroot -pMatKhauVuaThayDoi2023@@

Tạo DB và người dùng cho DB của ứng dụng web

Để tạo database mới:

create database db_new;

Bước tiếp theo chúng ta sẽ thực hiện tạo một user cho ứng dụng web.

CREATE USER 'user_db'@'localhost' IDENTIFIED BY 'Matkhau2023@@';

Thêm full quyền quản lý DB cho user vừa tạo ở trên

GRANT ALL PRIVILEGES ON db_new.* TO 'user_db'@'localhost';
FLUSH PRIVILEGES;
exit;

Để kiểm tra bước tạo ở trên đã chuẩn chưa thì chúng ta thực hiện đăng nhập lại với user và password vừa tạo:

mysql -uuser_db -pMatkhau2023@@

Kiểm tra trạng thái của mysql:

sudo service mysqld status
Redirecting to /bin/systemctl status mysqld.service
● mysqld.service - MySQL Server
     Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; preset: d>
     Active: active (running) since Mon 2023-07-31 10:10:56 UTC; 2 weeks 2 days>
       Docs: man:mysqld(8)
             http://dev.mysql.com/doc/refman/en/using-systemd.html
   Main PID: 250921 (mysqld)
     Status: "Server is operational"
      Tasks: 46 (limit: 2322)
     Memory: 575.6M
        CPU: 2h 19min 306ms
     CGroup: /system.slice/mysqld.service
             └─250921 /usr/sbin/mysqld

Đến bước này là chúng ta đã cài đặt xong Mysql server 8.0 trên Amazon Linux 2023. Ngoài ra để thực hiện import sql có sẵn vào DB vừa tạo thì chúng ta có thể cài đặt PhpMyadmin hoặc sử dụng command import của mysql.

Để biết cách cài đặt PhpMyadmin cho Amazon Linux 2023 hoặc Amazon Linux EC2 vui lòng xem bài viết:

Hướng dẫn cài đặt phpMyAdmin cho Amazon Linux 2023

Import và Export Mysql

Để Export Mysql, ở đây ta sẽ sử dụng mysqldump, thực hiện câu lệnh sau tại terminal local:

mysqldump -u user_db -pMatkhau2023@@ db_export > ./db_backup.sql

Lưu ý để sử dụng được DB và import vào DB mới bạn cần xóa warning của mysqldump trong file db_backup.sql vừa export ra. Tìm dòng sau trong file và xóa:

mysqldump: [Warning] Using a password on the command line interface can be insecure.

Để Import Mysql vào db mới ta thực hiện lệnh:

mysql -u username -p db_export < db_backup.sql

Tổng kết

Trên đây là toàn bộ quá trình cài đặt Mysql 8 trên Amazon Linux 2023. Các bước hướng dẫn trên được tóm gọn dành cho người có kinh nghiệm khi cài đặt server. Nếu bạn có vấn đề gì trong quá trình cài đặt hãy để lại comment hoặc inbox mình sẽ hướng dẫn cho nhé.

Chúc bạn thành công.

reference:

https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-lamp-amazon-linux-2023.html

Viết một bình luận