Trang này chưa có bản Tiếng Việt. Đang hiển thị bằng tiếng Anh.
Reach a MySQL server through Localtonet
Trong trang này 9 mục
- How it works
- Before you start
- Create the tunnel
- Open the TCP - UDP page
- Fill in the form
- Start the tunnel and copy the address
- Connect from outside
- MySQL settings that matter behind a tunnel
- The bind address can stay local
- The account's host is the Localtonet client, not the visitor
- Keep the same address
- Secure it
- Common problems
- Related pages
Connect to a MySQL or MariaDB server on your machine from another network: a TCP tunnel gives port 3306 a public host name and port that any MySQL client can use.
How it works #
The Localtonet client runs on the machine that hosts MySQL, or on another device in the same network. A TCP tunnel points to the MySQL port, 3306. Localtonet gives the tunnel a public address made of a host name and a port. A MySQL client that connects to that address reaches your server. The details of TCP tunnels are on the TCP page.
Do not expose a database without access control. A started tunnel is reachable by anyone who has its address. Read Secure it before you put real data behind a tunnel.
Before you start #
- A Localtonet account and the Localtonet client connected with your AuthToken. See Quick start.
- MySQL running on the device. Check that
mysql -h 127.0.0.1 -P 3306 -u <DB_USER> -pworks on the device itself. - A MySQL account for remote use, with a strong password and only the privileges it needs. Don't use
root.
Create the tunnel #
-
Open the TCP - UDP page #
Open My Tunnels › TCP - UDP and expand Create TCP - UDP Tunnel.
-
Fill in the form #
Field What to enter Protocol TCP Device token The token the Localtonet client on the device uses. Server region Choose a region close to your device for lower latency. Local IP address 127.0.0.1when MySQL runs on the same device as the client. For a MySQL server on another machine in the network, enter that machine's address.Local port 3306, or theportvalue of your MySQL configuration.URL type Random subdomain. Custom subdomain lets you choose the name and needs Balance. Select Create tunnel.
-
Start the tunnel and copy the address #
The tunnel appears under Your tunnels. Select Start. The URL column shows the public address as
host:port, and the Server Port column shows the port on its own.
Connect from outside #
With the mysql command-line client, the host goes after -h and the port after an upper-case -P. The lower-case -p asks for the password.
mysql -h <TUNNEL_HOST> -P <TUNNEL_PORT> -u <DB_USER> -p <DB_NAME>Applications usually take a connection URL:
mysql://<DB_USER>:<DB_PASSWORD>@<TUNNEL_HOST>:<TUNNEL_PORT>/<DB_NAME>In a graphical client such as MySQL Workbench or DBeaver, enter the tunnel's host name as the host and the tunnel's port as the port. Don't leave the port at 3306 unless you set the tunnel's Server Port to 3306.
MySQL settings that matter behind a tunnel #
The bind address can stay local #
The Localtonet client connects to MySQL from your own device. MySQL doesn't have to listen on a public interface: bind-address = 127.0.0.1 is enough when the client runs on the same machine. Leave it that way, so the tunnel is the only path from outside.
The account's host is the Localtonet client, not the visitor #
MySQL accounts are a user name plus the host the connection comes from. Through the tunnel, every connection comes from the Localtonet client. When the client runs on the MySQL machine, that host is localhost:
CREATE USER '<DB_USER>'@'localhost' IDENTIFIED BY '<DB_PASSWORD>';
GRANT SELECT, INSERT, UPDATE, DELETE ON <DB_NAME>.* TO '<DB_USER>'@'localhost';If the server runs with skip_name_resolve on, use '127.0.0.1' instead of 'localhost'. If the client runs on another device, use that device's address in the network.
This also means that host rules in MySQL can't tell visitors apart: everyone who reaches the tunnel arrives as the same host. Limit the visitors on the tunnel instead, with IP Restriction.
Keep the same address #
The host name stays with the tunnel. The port is assigned by the server when the tunnel starts. With Is Reserved? on in the tunnel's Settings, the port stays the same when the tunnel or the Localtonet client restarts, which is what a connection string in an application needs. With it off, the port changes on a restart. Tunnels that you create while your account has Balance start with Is Reserved? on. Turning it on needs Balance and a started tunnel.
With Balance you can also choose the port under Server Port › Change, for example 3306 if no other tunnel on that server uses it. See Ports.
Free plan. A TCP tunnel stops after 30 minutes on the Free plan and can be started again. The port can be different after each start. See Plans and Balance.
Secure it #
- IP Restriction. In the tunnel's Settings, select IP Restriction › Manage, choose Allow only these and add the addresses of the applications and people that connect. A single address, a range such as
10.0.0.0/16, or several at once are accepted. Changes apply right away. IP Restriction needs Balance. See Access control. - A dedicated account with a strong password and the smallest set of privileges, as in the example above. Remove accounts without a password.
- TLS in MySQL. Tunnels are not encrypted end to end by default, so let MySQL encrypt the session: create the account with
REQUIRE SSLand connect with--ssl-mode=REQUIRED(the MariaDB client uses--ssl). - Limits. Expiration Date, Bandwidth Limit and Speed Limits in the tunnel's Settings cap how long and how much the tunnel can be used. See Limits and expiration.
- Request Log › Show lists the connections made to the tunnel with the visitor's IP address.
Common problems #
ERROR 2003: Can't connect to MySQL server
The client could not open a connection. Check that the tunnel is started and its Connection column shows ON, that you use the current port from Server Port, and that MySQL answers on the device at the tunnel's Local IP address and Local port. If you use IP Restriction, check that your current address is in the list.
ERROR 1045: Access denied for user '…'@'localhost'
The tunnel works and MySQL answered. The host in the message is the Localtonet client, as explained above. Check the password, and check that an account exists for exactly that user and host.
ERROR 1130: Host '…' is not allowed to connect to this MySQL server
No account matches the host that MySQL sees. Create the account for the host named in the message.
The application lost its connection after a restart
The port is not reserved, so it changed when the tunnel or the client restarted. Turn on Is Reserved?, or update the port in the application.
The tunnel stopped by itself after half an hour
On the Free plan a TCP tunnel stops after 30 minutes. Select Start to begin another session, or add Balance to remove the session limit.
"You need to purchase balance to use this feature."
Is Reserved?, Server Port, IP Restriction and Custom subdomain need Balance. Add it under Balance.