Hướng dẫn how do i make my mysql database accessible remotely? - làm cách nào để tôi có thể truy cập cơ sở dữ liệu mysql của mình từ xa?

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:

  1. 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ị

. . .
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 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
. . .
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:

  1. 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:

  1. 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:

  1. 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:

  1. 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:

  1. 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,

. . .
[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. 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
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 đó:

  1. 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
. . .
0

Sau đó 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
  1. sudo systemctl restart mysql
0,
  1. sudo systemctl restart mysql
1 và
  1. 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
  1. sudo systemctl restart mysql
3, tạo các khóa ngoại bằng từ khóa
  1. sudo systemctl restart mysql
4 và thực hiện các hoạt động
  1. sudo systemctl restart mysql
5 với đặc quyền
  1. 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
. . .
1

Sau đó, nó thực hành tốt để chạy lệnh

  1. 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
  1. sudo systemctl restart mysql
8 và
  1. 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
. . .
2

Sau đó, 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
. . .
3

Cuố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

  1. 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ế

  1. 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
. . .
4

Nế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
. . .
5

Theo 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
. . .
6

Nế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.

Làm cách nào để kết nối từ xa với cơ sở dữ liệu?

Thông qua CPanel để thêm máy tính của bạn làm máy chủ truy cập: Đăng nhập vào CPanel. Trong phần cơ sở dữ liệu, nhấp vào biểu tượng MySQL® từ xa. Trên trang MySQL® từ xa, nhập địa chỉ IP kết nối, sau đó nhấp vào Thêm máy chủ.

Làm cách nào để làm cho cơ sở dữ liệu MySQL của tôi có thể truy cập Windows từ xa?

Mở một kết nối thiết bị đầu cuối hoặc SSH và loại sudo iptables -a đầu vào -p tcp -s x.x.x.xTHERDport yyyy -j chấp nhận. Thay thế x.x.x.x bằng địa chỉ IP cho thiết bị bạn muốn cho phép các kết nối MySQL từ và thay thế Yyyy bằng giá trị cổng phù hợp từ tệp cấu hình MySQL của bạn (ví dụ: 3306).

Công cụ nào được sử dụng để kết nối với máy chủ MySQL từ xa?

Sử dụng MySQL Workbench để truy cập cơ sở dữ liệu MySQL từ xa của bạn thông qua đường hầm SSH là một cách đơn giản và an toàn để quản lý cơ sở dữ liệu của bạn từ sự thoải mái của máy tính cục bộ của bạn.MySQL Workbench to access your remote MySQL database through an SSH tunnel is a simple and secure way to manage your databases from the comfort of your local computer.

Làm thế nào để bạn kết nối từ xa với cơ sở dữ liệu MySQL nằm trên máy chủ chia sẻ của chúng tôi?

Cách kết nối từ xa với cơ sở dữ liệu MySQL nằm trên máy chủ chung của chúng tôi.Vì lý do bảo mật, kết nối MySQL từ xa bị vô hiệu hóa trên các máy chủ lưu trữ được chia sẻ của chúng tôi.Tuy nhiên, bạn có thể dễ dàng thiết lập một đường hầm SSH giữa máy tính của bạn và máy chủ của chúng tôi bằng cách sử dụng ứng dụng khách SSH với chuyển tiếp Cổng MySQL 3306.set up an SSH tunnel between your computer and our server using an SSH client with the MySQL port 3306 forwarding.

Làm cách nào để kết nối với cơ sở dữ liệu MySQL bằng địa chỉ IP?

Chọn các kết nối từ menu điều hướng SQL. Trong phần mạng được ủy quyền, nhấp vào Thêm mạng và nhập địa chỉ IP của máy nơi máy khách được cài đặt.Lưu ý: Địa chỉ IP của phiên bản và địa chỉ IP của máy khách MySQL mà bạn ủy quyền phải là cùng một phiên bản IP: IPv4 hoặc IPv6.Nhấp vào xong. In the Authorized networks section, click Add network and enter the IP address of the machine where the client is installed. Note: The IP address of the instance and the mysql client IP address you authorize must be the same IP version: either IPv4 or IPv6. Click Done.