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) để quản lý dữ liệu của nó.
Hướng dẫn này sẽ đi qua cách cài đặt MySQL phiên bản 8.0 trên máy chủ Ubuntu 22.04. Bằng cách hoàn thành nó, bạn sẽ có một cơ sở dữ liệu quan hệ hoạt động 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
Để làm theo hướng dẫn này, bạn sẽ cần:
- Một máy chủ Ubuntu 22.04 với người dùng quản trị không root 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 22.04.
Bước 1 - Cài đặt MySQL
Trên Ubuntu 22.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 lưu trữ Ubuntu mặc định là phiên bản 8.0.28.
Để 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 hoàn thành gần đây:
- sudo apt update
Sau đó cài đặt gói
- sudo systemctl start mysql.service
- sudo apt install mysql-server
Đảm bảo rằng máy chủ đang chạy bằng lệnh
Output
... Failed! Error: SET PASSWORD has no significance for user 'root'@'localhost' as the authentication method used doesn't store authentication data in the MySQL server. Please consider using ALTER USER instead if you want to change authentication parameters. New password: 0:- sudo systemctl start mysql.service
Các lệnh này sẽ cài đặt và bắt đầu 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 sự cài đặt của bạn về sự không an toàn của MySQL, chúng tôi sẽ giải quyết vấn đề này tiếp theo.
Bước 2 - Định cấu hình MySQL
Để cài đặt mới của MySQL, bạn sẽ muốn chạy hệ thống quản lý cơ sở dữ liệu bao gồm tập lệnh bảo mật. Tập lệnh này thay đổi một số tùy chọn mặc định ít an toàn hơn cho những thứ như không thay đổi đăng nhập từ xa và xóa người dùng mẫu.root logins and removing sample users.
Cảnh báo: Kể từ tháng 7 năm 2022, sẽ xảy ra lỗi khi bạn chạy tập lệnh Output
Output
... Failed! Error: SET PASSWORD has no significance for user 'root'@'localhost' as the authentication method used doesn't store authentication data in the MySQL server. Please consider using ALTER USER instead if you want to change authentication parameters. New password: 1 script without some further configuration. The reason is that this script will attempt to set a password for the installation’s root MySQL account but, by default on Ubuntu installations, this account is not configured to connect using a password.Trước tháng 7 năm 2022, tập lệnh này sẽ âm thầm thất bại sau khi cố gắng đặt mật khẩu tài khoản gốc và tiếp tục với phần còn lại của lời nhắc. Tuy nhiên, theo cách viết này, tập lệnh sẽ trả về lỗi sau khi bạn nhập và xác nhận mật khẩu:root account password and continue on with the rest of the prompts. However, as of this writing the script will return the following error after you enter and confirm a password:
Output
... Failed! Error: SET PASSWORD has no significance for user 'root'@'localhost' as the authentication method used doesn't store authentication data in the MySQL server. Please consider using ALTER USER instead if you want to change authentication parameters. New password:Điều này sẽ dẫn kịch bản vào một vòng lặp đệ quy mà bạn chỉ có thể thoát ra bằng cách đóng cửa sổ thiết bị đầu cuối của mình.
Vì tập lệnh
Output
... Failed! Error: SET PASSWORD has no significance for user 'root'@'localhost' as the authentication method used doesn't store authentication data in the MySQL server. Please consider using ALTER USER instead if you want to change authentication parameters. New password: 1 thực hiện một số hành động khác hữu ích để giữ an toàn cài đặt MySQL của bạn, nên bạn vẫn khuyến nghị bạn chạy nó trước khi bạn bắt đầu sử dụng MySQL để quản lý dữ liệu của mình. Tuy nhiên, để tránh nhập vòng lặp đệ quy này, trước tiên bạn sẽ cần phải điều chỉnh cách người dùng MySQL gốc của bạn xác thực.root MySQL user authenticates.Đầu tiên, hãy mở dấu nhắc MySQL:
- sudo mysql
Sau đó, chạy lệnh
Output
... Failed! Error: SET PASSWORD has no significance for user 'root'@'localhost' as the authentication method used doesn't store authentication data in the MySQL server. Please consider using ALTER USER instead if you want to change authentication parameters. New password: 3 sau để thay đổi phương thức xác thực người dùng gốc thành phương thức sử dụng mật khẩu. Ví dụ sau đây thay đổi phương thức xác thực thànhOutput
... Failed! Error: SET PASSWORD has no significance for user 'root'@'localhost' as the authentication method used doesn't store authentication data in the MySQL server. Please consider using ALTER USER instead if you want to change authentication parameters. New password: 4:root user’s authentication method to one that uses a password. The following example changes the authentication method toOutput
... Failed! Error: SET PASSWORD has no significance for user 'root'@'localhost' as the authentication method used doesn't store authentication data in the MySQL server. Please consider using ALTER USER instead if you want to change authentication parameters. New password: 4:- ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
Sau khi thực hiện thay đổi này, hãy thoát lời nhắc MySQL:
- exit
Theo đó, bạn có thể chạy tập lệnh
Output
... Failed! Error: SET PASSWORD has no significance for user 'root'@'localhost' as the authentication method used doesn't store authentication data in the MySQL server. Please consider using ALTER USER instead if you want to change authentication parameters. New password: 1 mà không gặp vấn đề gì.Khi tập lệnh bảo mật hoàn tất, sau đó bạn có thể mở lại MySQL và thay đổi phương thức xác thực của người dùng gốc trở lại mặc định,
Output
... Failed! Error: SET PASSWORD has no significance for user 'root'@'localhost' as the authentication method used doesn't store authentication data in the MySQL server. Please consider using ALTER USER instead if you want to change authentication parameters. New password: 6. Để xác thực với tư cách là người dùng MySQL gốc sử dụng mật khẩu, hãy chạy lệnh này:root user’s authentication method back to the default,Output
... Failed! Error: SET PASSWORD has no significance for user 'root'@'localhost' as the authentication method used doesn't store authentication data in the MySQL server. Please consider using ALTER USER instead if you want to change authentication parameters. New password: 6. To authenticate as the root MySQL user using a password, run this command:- mysql -u root -p
Sau đó quay lại bằng phương thức xác thực mặc định bằng lệnh này:
- ALTER USER 'root'@'localhost' IDENTIFIED WITH auth_socket;
Điều này có nghĩa là bạn có thể một lần nữa kết nối với MySQL làm người dùng gốc của bạn bằng lệnh
Output
... Failed! Error: SET PASSWORD has no significance for user 'root'@'localhost' as the authentication method used doesn't store authentication data in the MySQL server. Please consider using ALTER USER instead if you want to change authentication parameters. New password: 7.root user using theOutput
... Failed! Error: SET PASSWORD has no significance for user 'root'@'localhost' as the authentication method used doesn't store authentication data in the MySQL server. Please consider using ALTER USER instead if you want to change authentication parameters. New password: 7 command.Chạy tập lệnh bảo mật với
Output
... Failed! Error: SET PASSWORD has no significance for user 'root'@'localhost' as the authentication method used doesn't store authentication data in the MySQL server. Please consider using ALTER USER instead if you want to change authentication parameters. New password: 8:- sudo mysql_secure_installation
Điều này sẽ đưa bạn thông 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ài đặt MySQL của bạn. 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 hay không, 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 coi 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 bằng mật khẩu sẽ được yêu cầu phải có mật khẩu thỏa mãn chính sách bạn chọn:
- sudo apt install mysql-server
Bất kể bạn có chọn thiết lập plugin Mật khẩu xác thực hay không, lời nhắc tiếp theo sẽ là đặt mật khẩu cho người dùng gốc MySQL. Nhập và sau đó xác nhận mật khẩu an toàn mà bạn chọn:root user. Enter and then confirm a secure password of your choice:
- sudo apt install mysql-server
Lưu ý rằng mặc dù bạn đã đặt mật khẩu cho người dùng MySQL gốc, nhưng người dùng này hiện không được cấu hình để xác thực bằng mật khẩu khi kết nối với vỏ MySQL.root MySQL user, this user is not currently configured to authenticate with a password when connecting to the MySQL shell.
Nếu bạn đã sử dụng plugin Mật khẩu xác thực, bạn sẽ nhận được phản hồi về sức mạnh của mật khẩu mới. Sau đó, tập lệnh sẽ hỏi nếu bạn muốn tiếp tục với mật khẩu bạn vừa nhập hoặc nếu bạn muốn nhập một bản mới. Giả sử bạn hài lòng với sức mạnh của mật khẩu bạn vừa nhập, nhập
Output
... Failed! Error: SET PASSWORD has no significance for user 'root'@'localhost' as the authentication method used doesn't store authentication data in the MySQL server. Please consider using ALTER USER instead if you want to change authentication parameters. New password: 9 để tiếp tục tập lệnh:- sudo apt install mysql-server
Từ đó, bạn có thể nhấn
Output
... Failed! Error: SET PASSWORD has no significance for user 'root'@'localhost' as the authentication method used doesn't store authentication data in the MySQL server. Please consider using ALTER USER instead if you want to change authentication parameters. New password: 9 và sau đó- sudo mysql
Khi tập lệnh hoàn tất, cài đặt MySQL của bạn sẽ được bảo mật. Bây giờ bạn có thể chuyển sang tạo người dùng cơ sở dữ liệu chuyên dụng với máy khách MySQL.
Bước 3 - Tạo người dùng MySQL chuyên dụng và cấp các đặc quyền
Khi cài đặt, MySQL tạo tài khoản người dùng gốc 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 đối với máy chủ MySQL, có nghĩa là nó có quyền kiểm soát hoàn toàn đối với 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à nên tránh sử dụng tài khoản này bên ngoài các chức năng hành chính. Bước này phác thảo cách sử dụng người dùng MySQL gốc để tạo tài khoản người dùng mới và cấp cho nó các đặc quyền.root user account which you can use to manage your database. This user has full privileges over the MySQL server, meaning it has complete control over every database, table, user, and so on. Because of this, it’s best to avoid using this account outside of administrative functions. This step outlines how to use the root MySQL user to create a new user account and grant it privileges.
Trong các hệ thống Ubuntu chạy MySQL 5.7 (và các phiên bản sau), người dùng MySQL gốc được đặt thành xác thực bằng plugin
Output
... Failed! Error: SET PASSWORD has no significance for user 'root'@'localhost' as the authentication method used doesn't store authentication data in the MySQL server. Please consider using ALTER USER instead if you want to change authentication parameters. New password: 6 theo mặc định thay vì bằng 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 khớp với tên của người dùng MySQL được chỉ định trong lệnh, do đó bạn phải gọi- sudo mysql
Output
... Failed! Error: SET PASSWORD has no significance for user 'root'@'localhost' as the authentication method used doesn't store authentication data in the MySQL server. Please consider using ALTER USER instead if you want to change authentication parameters. New password: 8 để có quyền truy cập vào người dùng MySQL gốc:root MySQL user is set to authenticate using theOutput
... Failed! Error: SET PASSWORD has no significance for user 'root'@'localhost' as the authentication method used doesn't store authentication data in the MySQL server. Please consider using ALTER USER instead if you want to change authentication parameters. New password: 6 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- sudo mysql
Output
... Failed! Error: SET PASSWORD has no significance for user 'root'@'localhost' as the authentication method used doesn't store authentication data in the MySQL server. Please consider using ALTER USER instead if you want to change authentication parameters. New password: 8 privileges to gain access to the root MySQL user:- sudo mysql
Lưu ý: Nếu bạn đã cài đặt MySQL với một hướng dẫn khác và xác thực mật khẩu cho Root, bạn sẽ cần sử dụng một lệnh khác để truy cập vào vỏ MySQL. Điều sau đây sẽ chạy máy khách MySQL của bạn với các đặc quyền người dùng thường xuyên và bạn sẽ chỉ có được các đặc quyền quản trị viên trong cơ sở dữ liệu bằng cách xác thực: 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 lời nhắc MySQL, bạn có thể tạo một người dùng mới với câu lệnh
- sudo mysql
- sudo apt install mysql-server
Sau
- sudo mysql
- sudo mysql
- sudo mysql
Bạn có một số tùy chọn khi chọn plugin xác thực người dùng của bạn. Plugin
Output
... Failed! Error: SET PASSWORD has no significance for user 'root'@'localhost' as the authentication method used doesn't store authentication data in the MySQL server. Please consider using ALTER USER instead if you want to change authentication parameters. New password: 6 đã đề cập trước đây có thể thuận tiện, vì nó cung cấp bảo mật mạnh mẽ mà không yêu cầu 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 vào đó, bạn có thể loại bỏ phần
- ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
- ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
Chạy lệnh sau để tạo người dùng xác thực với
- ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
- ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
- ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
- sudo apt install mysql-server
Lưu ý: Có một vấn đề đã biết với một số phiên bản PHP gây ra vấn đề với Output Output
1. Ví dụ, nếu bạn có kế hoạch sử dụng cơ sở dữ liệu này với ứng dụng PHP - PHPMyAdmin - bạn có thể muốn tạo người dùng sẽ xác thực với plugin
- ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
Output
... Failed! Error: SET PASSWORD has no significance for user 'root'@'localhost' as the authentication method used doesn't store authentication data in the MySQL server. Please consider using ALTER USER instead if you want to change authentication parameters. New password: 4 plugin instead:- sudo apt install mysql-server
Nếu bạn không chắc chắn, bạn luôn có thể tạo một người dùng xác thực bằng
- ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
- ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
- sudo apt install mysql-server
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ác đặc quyền của người dùng như sau:
- sudo apt install mysql-server
Giá trị
- ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
- exit
- exit
- exit
Để minh họa, lệnh sau đây cấp cho một đặc quyền toàn cầu của người dùng cho cơ sở dữ liệu, bảng và người dùng
- exit
- ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
- exit
- exit
- exit
- exit
- exit
- mysql -u root -p
- mysql -u root -p
- mysql -u root -p
Bạn có thể tìm thấy danh sách đầy đủ các đặc quyền có sẵn trong tài liệu MySQL chính thức.
Chạy câu lệnh
- mysql -u root -p
- ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
- sudo systemctl start mysql.service
Lưu ý rằng tuyên bố này cũng bao gồm
- mysql -u root -p
CẢNH BÁO: Một số người dùng có thể muốn cấp cho người dùng MySQL của họ đặc quyền
6, điều này sẽ cung cấp cho họ các đặc quyền của siêu người dùng rộng giống với các đặc quyền của người dùng gốc, như vậy:: Some users may want to grant their MySQL user the
- mysql -u root -p
- sudo systemctl start mysql.service
Các đặc quyền rộng lớn như vậy không nên được cấp nhẹ, vì bất kỳ ai có quyền truy cập vào người dùng MySQL này sẽ có quyền kiểm soát hoàn toàn đối với mọi cơ sở dữ liệu trên máy chủ.should not be granted lightly, as anyone with access to this MySQL user will have complete control over every database on the server.
Sau đó, nó thực hành tốt để chạy lệnh
- mysql -u root -p
- sudo mysql
- mysql -u root -p
- sudo systemctl start mysql.service
Sau đó, bạn có thể thoát khỏi máy khách MySQL:
- exit
Trong tương lai, để đăng nhập với tư cách là người dùng MySQL mới của bạn, bạn đã sử dụng một lệnh như sau:
- sudo systemctl start mysql.service
Cờ
- ALTER USER 'root'@'localhost' IDENTIFIED WITH auth_socket;
Cuối cùng, hãy để thử nghiệm cài đặt MySQL.
Bước 4 - Kiểm tra MySQL
Bất kể bạn đã cài đặt nó như thế nào, MySQL nên bắt đầu chạy tự động. Để kiểm tra điều này, kiểm tra trạng thái của nó.
- sudo systemctl start mysql.service
Đầu ra sẽ tương tự như sau:
- sudo systemctl start mysql.service
Nếu MySQL đang chạy, bạn có thể bắt đầu với
- ALTER USER 'root'@'localhost' IDENTIFIED WITH auth_socket;
Để kiểm tra bổ sung, bạn có thể thử kết nối với cơ sở dữ liệu bằng công cụ
- ALTER USER 'root'@'localhost' IDENTIFIED WITH auth_socket;
- ALTER USER 'root'@'localhost' IDENTIFIED WITH auth_socket;
- ALTER USER 'root'@'localhost' IDENTIFIED WITH auth_socket;
- ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
- ALTER USER 'root'@'localhost' IDENTIFIED WITH auth_socket;
- ALTER USER 'root'@'localhost' IDENTIFIED WITH auth_socket;
- ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
- sudo systemctl start mysql.service
Dưới đây là một ví dụ về đầu ra:
- sudo systemctl start mysql.service
Điều này có nghĩa là MySQL đang hoạt động.
Sự kết luận
Bây giờ bạn đã cài đặt MYSQL cơ bản được cài đặt trên máy chủ của bạn. Dưới đây là một vài ví dụ về các bước tiếp theo bạn có thể thực hiện:
- Thiết lập một ngăn xếp đèn hoặc ngăn xếp LEMP
- Thực hành chạy các truy vấn với SQL
- Quản lý cài đặt MySQL của bạn với phpmyadmin