Nhiều trang web và ứng dụng bắt đầu với phụ trợ máy chủ web và cơ sở dữ liệu của họ được lưu trữ trên cùng một máy. Tuy nhiên, với thời gian, một thiết lập như thế này có thể trở nên cồng kềnh và khó khăn để mở rộng quy mô. Một giải pháp phổ biến là tách các chức năng này bằng cách thiết lập cơ sở dữ liệu từ xa, cho phép máy chủ và cơ sở dữ liệu phát triển theo tốc độ của riêng chúng trên máy của chúng.
Một trong những vấn đề phổ biến hơn mà người dùng gặp phải khi cố gắng thiết lập cơ sở dữ liệu MySQL từ xa là phiên bản MySQL của họ chỉ được cấu hình để nghe các kết nối cục bộ. Đây là cài đặt mặc định của MySQL, nhưng nó đã giành được công việc cho một thiết lập cơ sở dữ liệu từ xa vì MYSQL phải có thể nghe địa chỉ IP bên ngoài nơi có thể tiếp cận với máy chủ. Để bật điều này, hãy mở tệp
. . .
lc-messages-dir = /usr/share/mysql
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 127.0.0.1
. . .
7 của bạn:- sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
Điều hướng đến dòng bắt đầu với Chỉ thị
. . .
lc-messages-dir = /usr/share/mysql
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 127.0.0.1
. . .
8. Nó sẽ trông giống thế này:/etc/mysql/mysql.conf.d/mysqld.cnf
. . .
lc-messages-dir = /usr/share/mysql
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 127.0.0.1
. . .
Theo mặc định, giá trị này được đặt thành
. . .
lc-messages-dir = /usr/share/mysql
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 127.0.0.1
. . .
9, có nghĩa là máy chủ sẽ chỉ tìm kiếm các kết nối cục bộ. Bạn sẽ cần thay đổi Chỉ thị này để tham khảo một địa chỉ IP bên ngoài. Đối với mục đích khắc phục sự cố, bạn có thể đặt chỉ thị này thành địa chỉ IP ký tự đại diện, . . .
lc-messages-dir = /usr/share/mysql
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 0.0.0.0
. . .
0, . . .
lc-messages-dir = /usr/share/mysql
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 0.0.0.0
. . .
1 hoặc . . .
lc-messages-dir = /usr/share/mysql
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 0.0.0.0
. . .
2:/etc/mysql/mysql.conf.d/mysqld.cnf
. . .
lc-messages-dir = /usr/share/mysql
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 0.0.0.0
. . .
Lưu ý: Trong một số phiên bản nhất định của MySQL, chỉ thị
8 có thể không có trong tệp . . .
lc-messages-dir = /usr/share/mysql
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 127.0.0.1
. . .
7 theo mặc định. Trong trường hợp này, thêm dòng được tô sáng sau vào cuối tệp: In certain versions of MySQL the . . .
lc-messages-dir = /usr/share/mysql
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 127.0.0.1
. . .
. . .
lc-messages-dir = /usr/share/mysql
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 127.0.0.1
. . .
8 directive may not be in the . . .
lc-messages-dir = /usr/share/mysql
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 127.0.0.1
. . .
7 file by default. In this case, add the following highlighted line to the bottom of the
file:
/etc/mysql/mysql.conf.d/mysqld.cnf
. . .
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
log-error = /var/log/mysql/error.log
bind-address = 0.0.0.0
Sau khi thay đổi dòng này, hãy lưu và đóng tệp [
. . .
lc-messages-dir = /usr/share/mysql
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 0.0.0.0
. . .
5, . . .
lc-messages-dir = /usr/share/mysql
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 0.0.0.0
. . .
6, sau đó . . .
lc-messages-dir = /usr/share/mysql
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 0.0.0.0
. . .
7 nếu bạn chỉnh sửa nó với . . .
lc-messages-dir = /usr/share/mysql
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 0.0.0.0
. . .
8].Sau đó, khởi động lại dịch vụ MySQL để đưa các thay đổi bạn đã thực hiện để
. . .
lc-messages-dir = /usr/share/mysql
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 127.0.0.1
. . .
7 có hiệu lực:- sudo systemctl restart mysql
Nếu bạn có tài khoản người dùng MySQL hiện tại mà bạn dự định sử dụng để kết nối với cơ sở dữ liệu từ máy chủ từ xa của mình, bạn sẽ cần phải cấu hình lại tài khoản đó để kết nối từ máy chủ từ xa thay vì localhost. Để làm như vậy, hãy mở máy khách MySQL với tư cách là người dùng MySQL gốc của bạn hoặc với một tài khoản người dùng đặc quyền khác:localhost. To do so, open up the MySQL client as your root MySQL user or with another privileged user account:
- sudo mysql
Nếu bạn đã kích hoạt xác thực mật khẩu cho Root, bạn sẽ cần sử dụng lệnh sau để truy cập shell MySQL thay thế:root, you will need to use the following command to access the MySQL shell instead:
- mysql -u root -p
Để thay đổi máy chủ của người dùng, bạn có thể sử dụng lệnh MySQLTHER
. . .
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
log-error = /var/log/mysql/error.log
bind-address = 0.0.0.0
0. Chạy lệnh sau, đảm bảo thay đổi . . .
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
log-error = /var/log/mysql/error.log
bind-address = 0.0.0.0
1 thành tên của tài khoản người dùng MySQL của bạn và . . .
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
log-error = /var/log/mysql/error.log
bind-address = 0.0.0.0
2 sang địa chỉ IP máy chủ từ xa của bạn:- RENAME USER 'sammy'@'localhost' TO 'sammy'@'remote_server_ip';
Ngoài ra, bạn có thể tạo một tài khoản người dùng mới sẽ chỉ kết nối từ máy chủ từ xa với lệnh sau:
- CREATE USER 'sammy'@'remote_server_ip' IDENTIFIED BY 'password';
Lưu ý: Lệnh này sẽ tạo một người dùng xác thực với plugin xác thực mặc định của MySQL,
3. Tuy nhiên, có một vấn đề đã biết với một số phiên bản PHP có thể gây ra sự cố với plugin này.: This command will create a user that authenticates with MySQL’s default authentication plugin, . . .
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
log-error = /var/log/mysql/error.log
bind-address = 0.0.0.0
. . .
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
log-error = /var/log/mysql/error.log
bind-address = 0.0.0.0
3. However, there is a known issue with some versions of PHP that can cause problems with this plugin.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 một người dùng từ xa sẽ xác thực với plugin
. . .
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
log-error = /var/log/mysql/error.log
bind-address = 0.0.0.0
4 cũ hơn, mặc dù vẫn an toàn, . . .
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
log-error = /var/log/mysql/error.log
bind-address = 0.0.0.0
4: thay vào đó:- CREATE USER 'sammy'@'remote_server_ip' 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 một người dùng xác thực bằng
. . .
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
log-error = /var/log/mysql/error.log
bind-address = 0.0.0.0
5 và sau đó . . .
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
log-error = /var/log/mysql/error.log
bind-address = 0.0.0.0
6 sau đó với lệnh này:. . .
lc-messages-dir = /usr/share/mysql
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 127.0.0.1
. . .
0Sau đó cấp cho người dùng mới các đặc quyền thích hợp cho các nhu cầu cụ thể của bạn. Ví dụ sau đây cho phép 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
. . .
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
log-error = /var/log/mysql/error.log
bind-address = 0.0.0.0
7, . . .
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
log-error = /var/log/mysql/error.log
bind-address = 0.0.0.0
6 và . . .
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
log-error = /var/log/mysql/error.log
bind-address = 0.0.0.0
9, cũng như nguồn điện cho - sudo systemctl restart mysql
0, - sudo systemctl restart mysql
1 và - sudo systemctl restart mysql
2 từ bất kỳ bảng nào trên máy chủ. Nó cũng cho phép người dùng khả năng truy vấn dữ liệu với - sudo systemctl restart mysql
3, tạo các khóa ngoại bằng từ khóa - sudo systemctl restart mysql
4 và thực hiện các hoạt động - sudo systemctl restart mysql
5 với đặc quyền - sudo systemctl restart mysql
6. Tuy nhiên, bạn chỉ nên cấp cho người dùng các quyền họ cần, vì vậy hãy thoải mái điều chỉnh các đặc quyền của người dùng của riêng bạn khi cần thiết.. . .
lc-messages-dir = /usr/share/mysql
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 127.0.0.1
. . .
1Sau đó, nó thực hành tốt để chạy lệnh
- sudo systemctl restart mysql
7. Điều này sẽ giải phóng bất kỳ bộ nhớ nào mà máy chủ đã lưu trữ do kết quả của các câu lệnh - sudo systemctl restart mysql
8 và - sudo systemctl restart mysql
9 trước đó:. . .
lc-messages-dir = /usr/share/mysql
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 127.0.0.1
. . .
2Sau đó, bạn có thể thoát khỏi máy khách MySQL:
. . .
lc-messages-dir = /usr/share/mysql
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 127.0.0.1
. . .
3Cuối cùng, giả sử bạn đã cấu hình tường lửa trên máy chủ cơ sở dữ liệu của mình, bạn cũng sẽ cần mở cổng
- sudo mysql
0 - cổng mặc định của MySQL, để cho phép lưu lượng truy cập đến MySQL.Nếu bạn chỉ có kế hoạch truy cập máy chủ cơ sở dữ liệu từ một máy cụ thể, bạn có thể cấp quyền độc quyền của máy đó để kết nối với cơ sở dữ liệu từ xa với lệnh sau. Đảm bảo thay thế
- sudo mysql
1 bằng địa chỉ IP thực tế của máy bạn dự định kết nối với:. . .
lc-messages-dir = /usr/share/mysql
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 127.0.0.1
. . .
4Nếu bạn cần truy cập cơ sở dữ liệu từ các máy khác trong tương lai, bạn có thể cấp cho họ quyền truy cập trên cơ sở đặc biệt với lệnh này. Chỉ cần nhớ bao gồm các địa chỉ IP tương ứng của họ.
Ngoài ra, bạn có thể cho phép các kết nối đến cơ sở dữ liệu MySQL của mình từ bất kỳ địa chỉ IP nào với lệnh sau:
CẢNH BÁO: Lệnh này sẽ cho phép mọi người truy cập cơ sở dữ liệu MySQL của bạn. Không chạy nó nếu cơ sở dữ liệu của bạn giữ bất kỳ dữ liệu nhạy cảm nào.: This command will enable anyone to access your MySQL database. Do not run it if your database holds any sensitive data.
. . .
lc-messages-dir = /usr/share/mysql
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 127.0.0.1
. . .
5Theo sau này, hãy thử truy cập cơ sở dữ liệu của bạn từ xa từ máy khác:
Lưu ý: Nếu bạn đã thêm quy tắc tường lửa vào chỉ cho phép các kết nối từ một địa chỉ IP cụ thể, bạn phải cố gắng truy cập cơ sở dữ liệu với máy được liên kết với địa chỉ đó.: If you added a firewall rule to only allow connections from a specific IP address, you must try to access the database with the machine associated with that address.
. . .
lc-messages-dir = /usr/share/mysql
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 127.0.0.1
. . .
6Nếu bạn có thể truy cập cơ sở dữ liệu của mình, nó xác nhận rằng Chỉ thị
. . .
lc-messages-dir = /usr/share/mysql
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 127.0.0.1
. . .
8 trong tệp cấu hình của bạn là vấn đề. Tuy nhiên, xin lưu ý rằng cài đặt . . .
lc-messages-dir = /usr/share/mysql
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 127.0.0.1
. . .
8 thành . . .
lc-messages-dir = /usr/share/mysql
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 0.0.0.0
. . .
2 không an toàn vì nó cho phép kết nối với máy chủ của bạn từ bất kỳ địa chỉ IP nào. Mặt khác, nếu bạn vẫn không thể truy cập cơ sở dữ liệu từ xa, thì một cái gì đó khác có thể gây ra vấn đề. Trong cả hai trường hợp, bạn có thể thấy hữu ích khi làm theo hướng dẫn của chúng tôi về cách thiết lập cơ sở dữ liệu từ xa để tối ưu hóa hiệu suất trang web với MySQL trên Ubuntu 18.04 để thiết lập cấu hình cơ sở dữ liệu từ xa an toàn hơn.