Cách cài đặt MySQL trên Ubuntu 20.04

Giới thiệu

MySQL là một hệ thống quản lý cơ sở dữ liệu nguồn mở, thường được cài đặt như một phần của ngăn xếp đèn phổ biến (Linux, Apache, MySQL, PHP / PYTHON / PERL). Nó thực hiện mô hình quan hệ và sử dụng ngôn ngữ truy vấn có cấu trúc (được gọi là SQL) tốt hơn để quản lý dữ liệu của nó.

Hướng dẫn này sẽ tiến qua cách cài đặt MySQL phiên bản 8.0 trên máy chủ Ubuntu 20.04. Bằng cách hoàn thành nó, bạn sẽ có một cơ sở dữ liệu quan hệ làm việc mà bạn có thể sử dụng để xây dựng trang web hoặc ứng dụng tiếp theo của mình.

Điều kiện tiên quyết

Để theo hướng dẫn này, bạn sẽ cần:

  • Một máy chủ Ubuntu 20.04 với người dùng quản trị không gốc và tường lửa được cấu hình với UFW. Để thiết lập điều này, hãy làm theo hướng dẫn thiết lập máy chủ ban đầu của chúng tôi cho Ubuntu 20.04.

Bước 1 - Cài đặt MySQL

Trên Ubuntu 20.04, bạn có thể cài đặt MySQL bằng kho lưu trữ gói apt. Tại thời điểm viết bài này, phiên bản MySQL có sẵn trong kho Ubuntu mặc định là phiên bản 8.0.27.

Để cài đặt nó, hãy cập nhật chỉ mục gói trên máy chủ của bạn nếu bạn chưa làm như vậy gần đây:

  • Cập nhật Sudo Apt.

Sau đó cài đặt mysql-server Gói:

  • sudo apt cài đặt mysql-server

Điều này sẽ cài đặt MySQL, nhưng sẽ không nhắc bạn đặt mật khẩu hoặc thực hiện bất kỳ thay đổi cấu hình nào khác. Bởi vì điều này để lại cài đặt MySQL không an toàn của bạn, chúng tôi sẽ giải quyết điều này tiếp theo.

Bước 2 - Cấu hình MySQL

Để cài đặt mới MySQL, bạn sẽ muốn chạy tập lệnh bảo mật bao gồm của DBMS. Tập lệnh này thay đổi một số tùy chọn mặc định ít bảo mật hơn cho những thứ như đăng nhập gốc từ xa và người dùng mẫu.

Chạy tập lệnh bảo mật với sudo :

  • sudo mysql_secure_installation.

Điều này sẽ đưa bạn qua một loạt các lời nhắc nơi bạn có thể thực hiện một số thay đổi đối với các tùy chọn bảo mật của cài đặt MySQL. Lời nhắc đầu tiên sẽ hỏi liệu bạn có muốn thiết lập plugin mật khẩu xác thực, có thể được sử dụng để kiểm tra cường độ mật khẩu của người dùng MySQL mới trước khi thấy chúng hợp lệ.

Nếu bạn chọn thiết lập plugin mật khẩu xác thực, bất kỳ người dùng mysql nào bạn tạo xác thực với mật khẩu sẽ được yêu cầu để có mật khẩu đáp ứng chính sách bạn chọn. Mức chính sách mạnh nhất - mà bạn có thể chọn bằng cách nhập 2 will require passwords to be at least eight characters long and include a mix of uppercase, lowercase, numeric, and special characters:

Đầu ra
Đảm bảo triển khai máy chủ MySQL. Kết nối với MySQL bằng mật khẩu trống. Xác thực thành phần mật khẩu có thể được sử dụng để kiểm tra mật khẩu và cải thiện an ninh. Nó kiểm tra sức mạnh của mật khẩu và cho phép người dùng chỉ đặt những mật khẩu đó Đủ an toàn. Bạn có muốn thiết lập xác thực thành phần mật khẩu? Nhấn Y | Y cho Có, bất kỳ phím nào khác không: y Có ba cấp chính sách xác thực mật khẩu: Chiều dài thấp> = 8 Độ dài trung bình> = 8, số, Ốp lưng, và các ký tự đặc biệt Chiều dài mạnh> = 8, số, vỏ hỗn hợp, ký tự đặc biệt và tệp từ điển Vui lòng nhập 0 = thấp, 1 = trung bình và 2 = mạnh: 2

Bất kể bạn chọn thiết lập plugin xác thực mật khẩu, dấu nhắc tiếp theo sẽ là đặt mật khẩu cho MySQL root . Nhập và sau đó xác nhận mật khẩu an toàn của sự lựa chọn của bạn: root user. Enter and then confirm a secure password of your choice:

Đầu ra
Please set the password for root here. New password: Re-enter new password:

Đảm bảo triển khai máy chủ MySQL.root MySQL user, this user is not currently configured to authenticate with a password when connecting to the MySQL shell.

If you used the Validate Password Plugin, youll receive feedback on the strength of your new password. Then the script will ask if you want to continue with the password you just entered or if you want to enter a new one. Assuming youre satisfied with the strength of the password you just entered, enter Y to continue the script:

Đầu ra
Đảm bảo triển khai máy chủ MySQL.Y

Kết nối với MySQL bằng mật khẩu trống.Y and then ENTER to accept the defaults for all the subsequent questions. This will remove some anonymous users and the test database, disable remote root logins, and load these new rules so that MySQL immediately respects the changes you have made.

Once the script completes, your MySQL installation will be secured. You can now move on to creating a dedicated database user with the MySQL client.

Xác thực thành phần mật khẩu có thể được sử dụng để kiểm tra mật khẩu

Khi cài đặt, MySQL tạo root Tài khoản người dùng mà bạn có thể sử dụng để quản lý cơ sở dữ liệu của mình. Người dùng này có đầy đủ các đặc quyền trên máy chủ MySQL, có nghĩa là nó có toàn quyền kiểm soát mọi cơ sở dữ liệu, bảng, người dùng, v.v. Bởi vì điều này, tốt nhất là tránh sử dụng tài khoản này bên ngoài các chức năng quản trị. Bước này phác thảo cách sử dụng root Người dùng MySQL để tạo tài khoản người dùng mới và cấp cho nó đặc quyền.

Trong Ubuntu Systems Chạy MySQL 5.7 (và các phiên bản mới hơn), root Người dùng MySQL được đặt để xác thực bằng auth_socket theo mặc định thay vì mật khẩu. Plugin này yêu cầu tên của người dùng hệ điều hành gọi máy khách MySQL phù hợp với tên của người dùng MySQL được chỉ định trong lệnh, vì vậy bạn phải gọi mysql Với sudo Đặc quyền để có quyền truy cập vào Root MySQL: < / a >.5.7 (and later versions), the root MySQL user is set to authenticate using the auth_socket plugin by default rather than with a password. This plugin requires that the name of the operating system user that invokes the MySQL client matches the name of the MySQL user specified in the command, so you must invoke mysql with sudo privileges to gain access to the root MySQL user:

  • sudo mysql

Note: If you installed MySQL with another tutorial and enabled password authentication for root, you will need to use a different command to access the MySQL shell. The following will run your MySQL client with regular user privileges, and you will only gain administrator privileges within the database by authenticating:

  • mysql -u root -p

Khi bạn có quyền truy cập vào dấu nhắc mysql, bạn có thể tạo một người dùng mới có Tạo người dùng . Những thứ này theo cú pháp chung này:

  • "> Tạo người dùng ' Tên người dùng ' @ ' HOST 'Đã xác định bằng xác thực_plugin bởi' Mật khẩu '; .username'@'host' IDENTIFIED WITH authentication_plugin BY 'password';

sau Tạo người dùng , bạn chỉ định tên người dùng. Điều này ngay lập tức theo sau là @ và sau đó là tên máy chủ mà người dùng này sẽ kết nối. Nếu bạn chỉ có kế hoạch truy cập người dùng này cục bộ từ máy chủ Ubuntu của bạn, bạn có thể chỉ định localhost . Gói cả tên người dùng và máy chủ trong các trích dẫn đơn không phải lúc nào cũng cần thiết, nhưng làm như vậy có thể giúp ngăn ngừa lỗi.

Bạn có một số tùy chọn khi chọn plugin xác thực của người dùng. auth_socket Plugin đã đề cập trước đó có thể thuận tiện, vì nó cung cấp bảo mật mạnh mà không cần người dùng hợp lệ nhập mật khẩu để truy cập cơ sở dữ liệu. Nhưng nó cũng ngăn chặn các kết nối từ xa, có thể làm phức tạp mọi thứ khi các chương trình bên ngoài cần tương tác với MySQL.

Thay thế, bạn có thể bỏ qua với xác thực_Plugin phần của cú pháp hoàn toàn có Người dùng xác thực với plugin mặc định của MySQL, cacheing_sha2_password . Tài liệu MySQL khuyến nghị plugin này cho người dùng muốn đăng nhập bằng mật khẩu do các tính năng bảo mật mạnh mẽ của nó. WITH authentication_plugin portion of the syntax entirely to have the user authenticate with MySQLs default plugin, caching_sha2_password. The MySQL documentation recommends this plugin for users who want to log in with a password due to its strong security features.

Chạy lệnh sau để tạo người dùng xác thực với bộ đệm_sha2_password . Hãy chắc chắn để thay đổi sammy to your preferred username and password to a strong password of your choosing:

  • "> Tạo người dùng ' Sammy ' @ 'localhost' được xác định bởi ' Mật khẩu '; Lưu ý : Có một vấn đề đã biết với một số phiên bản PHP gây ra sự cố với cache_sha2_password . Nếu bạn có kế hoạch sử dụng cơ sở dữ liệu này bằng ứng dụng PHP - ví dụ: phpMyAdmin - bạn có thể muốn tạo người dùng sẽ xác thực với người già, mặc dù vẫn an toàn, mysql_native_password < AI = 10> Plugin thay thế: sammy'@'localhost' IDENTIFIED BY 'password';

Note: There is a known issue with some versions of PHP that causes problems with caching_sha2_password. If you plan to use this database with a PHP application phpMyAdmin, for example you may want to create a user that will authenticate with the older, though still secure, mysql_native_password plugin instead:

  • "> Tạo người dùng ' Sammy ' @ 'localhost' được xác định bằng mysql_native_password bằng ' Mật khẩu < / a> '; .sammy'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

Nếu bạn không chắc chắn, bạn luôn có thể tạo người dùng xác thực với cache_sha2_plugin và sau đó Thay đổi sau đó với lệnh này: caching_sha2_plugin and then ALTER it later on with this command:

  • "> thay đổi người dùng ' sammy ' @ 'localhost' được xác định bằng mysql_native_password bởi ' Mật khẩu < / a> '; .sammy'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

Sau khi tạo người dùng mới của bạn, bạn có thể cấp cho họ các đặc quyền thích hợp. Cú pháp chung để cấp đặc quyền người dùng như sau:

  • "> Grant Đặc quyền BẬT Cơ sở dữ liệu . Bảng Để ' Tên người dùng ' Chủ nhà '; .PRIVILEGE ON database.table TO 'username'@'host';

The PRIVILEGE value in this example syntax defines what actions the user is allowed to perform on the specified database and table. You can grant multiple privileges to the same user in one command by separating each with a comma. You can also grant a user privileges globally by entering asterisks (*) in place of the database and table names. In SQL, asterisks are special characters used to represent all databases or tables.

To illustrate, the following command grants a user global privileges to CREATE, ALTER, and DROP databases, tables, and users, as well as the power to INSERT, UPDATE, and DELETE data from any table on the server. It also grants the user the ability to query data with SELECT, create foreign keys with the REFERENCES keyword, and perform FLUSH operations with the RELOAD privilege. However, you should only grant users the permissions they need, so feel free to adjust your own users privileges as necessary.

You can find the full list of available privileges in the official MySQL documentation.

Run this GRANT statement, replacing sammy with your own MySQL users name, to grant these privileges to your user:

  • ">GRANT CREATE, ALTER, DROP, INSERT, UPDATE, DELETE, SELECT, REFERENCES, RELOAD on *.* TO 'sammy'@'localhost' WITH GRANT OPTION;

Note that this statement also includes WITH GRANT OPTION. This will allow your MySQL user to grant any permissions that it has to other users on the system.

Warning: Some users may want to grant their MySQL user the ALL PRIVILEGES privilege, which will provide them with broad superuser privileges akin to the root users privileges, like so:

  • ">GRANT ALL PRIVILEGES ON *.* TO 'sammy'@'localhost' WITH GRANT OPTION;

Such broad privileges should not be granted lightly, as anyone with access to this MySQL user will have complete control over every database on the server.

Following this, its good practice to run the FLUSH PRIVILEGES command. This will free up any memory that the server cached as a result of the preceding CREATE USER and GRANT statements:

  • ">FLUSH PRIVILEGES;

Then you can exit the MySQL client:

  • ">exit

In the future, to log in as your new MySQL user, youd use a command like the following:

  • mysql -u sammy -p

The -p flag will cause the MySQL client to prompt you for your MySQL users password in order to authenticate.

Finally, lets test the MySQL installation.

Step 4 Testing MySQL

Regardless of how you installed it, MySQL should have started running automatically. To test this, check its status.

  • systemctl status mysql.service

Youll see output similar to the following:

Output
mysql.service - MySQL Community Server Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled) Active: active (running) since Tue 2020-04-21 12:56:48 UTC; 6min ago Main PID: 10382 (mysqld) Status: "Server is operational" Tasks: 39 (limit: 1137) Memory: 370.0M CGroup: /system.slice/mysql.service 10382 /usr/sbin/mysqld

If MySQL isnt running, you can start it with sudo systemctl start mysql.

For an additional check, you can try connecting to the database using the mysqladmin tool, which is a client that lets you run administrative commands. For example, this command says to connect as a MySQL user named sammy (-u sammy), prompt for a password (-p), and return the version. Be sure to change sammy to the name of your dedicated MySQL user, and enter that users password when prompted:

  • sudo mysqladmin -p -u sammy version

You should see output similar to this:

Output
mysqladmin Ver 8.0.19-0ubuntu5 for Linux on x86_64 ((Ubuntu)) Copyright (c) 2000, 2020, 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. Server version 8.0.19-0ubuntu5 Protocol version 10 Connection Localhost via UNIX socket UNIX socket /var/run/mysqld/mysqld.sock Uptime: 10 min 44 sec Threads: 2 Questions: 25 Slow queries: 0 Opens: 149 Flush tables: 3 Open tables: 69 Queries per second avg: 0.038

This means MySQL is up and running.

Conclusion

You now have a basic MySQL setup installed on your server. Here are a few examples of next steps you can take:

  • Set up a LAMP stack or a LEMP stack
  • Practice running queries with SQL
  • Manage your MySQL installation with phpMyAdmin