Cloudron makes it easy to run web apps like WordPress, Nextcloud, GitLab on your server. Find out more or install now.


Connect minecraft server to LAMP Stack's MySQL



  • Title explains it already.

    I'm looking to connect my minecraft server to the lamp stack's database, but this isn't possible.

    Neither is it possible to connect to the MC Server's Database, as the hostname gets rewritten by cloudron automatically to localhost.

    Please fix this or let me know how I can make either database accessible.


  • Staff

    If minecraft is on Cloudron as well, then you can just use the MySQL credentials of the LAMP app directly. You can get the credentials by opening a web terminal for the LAMP app and then running this:

       env | grep CLOUDRON_MYSQL
    

    If minecraft is one a different server, it's more complicated because the MySQL addon is completely sandboxed in the sever. But it's possible (can explain more, if this is the case).


  • Staff

    Here's a more complete tutorial.

    • First open the web terminal for the lamp app. Get the MySQL access credentials. Note that the credentials only work inside Cloudron server.
    root@f6af0aef-2e29-4b53-a674-c2eed67eea36:/app/data# env | grep MYSQL
    MYSQL_PASSWORD=9458c46184b6337677f058fbd53c089a317e6b2f5e321b1d
    MYSQL_DATABASE=0133aefa8ed2d231
    MYSQL_PORT=3306
    MYSQL_HOST=mysql
    MYSQL_USERNAME=0133aefa8ed2d231
    MYSQL_URL=mysql://0133aefa8ed2d231:9458c46184b6337677f058fbd53c089a317e6b2f5e321b1d@mysql/0133aefa8ed2d231
    
    • SSH into the Cloudron server and get the IP address of the mysql container.
    root@localhost:~# docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' mysql
    172.18.0.5
    
    • SSH into the Cloudron server to verify that we can connect to the app's database. Carefully plug the values from the env vars into the command line below.
    root@localhost:~# mysql --user=0133aefa8ed2d231 --password=9458c46184b6337677f058fbd53c089a317e6b2f5e321b1d --host=172.18.0.5 --port=3306 0133aefa8ed2d231
    mysql: [Warning] Using a password on the command line interface can be insecure.
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 8037
    Server version: 5.7.29-0ubuntu0.18.04.1 (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.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql>  show tables;
    
    • Now to connect to this mysql server from another server or your laptop, you have to create a SSH tunnel. Run this command on the other server or your laptop:
    $ ssh -N -L 127.0.0.1:3306:172.18.0.5:3306 root@my.cloudron.domain
    

    What this command is doing is connecting to your Cloudron via SSH. The -L sets up port forwarding. It forward port 3306 of 127.0.0.1 (localhost) to port 3306 of 172.18.0.5 (mysql).

    • We can now connect to port 3306 of 127.0.0.1 to access mysql:
    $ mysql --user=0133aefa8ed2d231 --password=9458c46184b6337677f058fbd53c089a317e6b2f5e321b1d --host=127.0.0.1 --port=3306 0133aefa8ed2d231
    mysql: [Warning] Using a password on the command line interface can be insecure.
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 8038
    Server version: 5.7.29-0ubuntu0.18.04.1 (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.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql> show tables;
    Empty set (0.01 sec)
    


  • @girish the server is on a completely different host, and I do not have system access.


  • Staff

    Does minecraft support connecting to mysql via ssh+tcp connection? Like how mysql workbench does it - MySQL Server using TCP/IP over an SSH connection. See https://dev.mysql.com/doc/workbench/en/wb-mysql-connections-methods-ssh.html



  • @girish No, I simply have the option to enter IP/Hostname, port, username, db name and password.


  • Staff

    I see, I think this can be done with some socat forwarding. I will try tomorrow.


  • Staff

    How about this then. On Cloudron, install socat and also open up port 5555 in iptables. Then just forward port 5555 to the internal mysql.

    root@localhost:~# apt install socat
    root@localhost:~# iptables -I INPUT -p tcp --dport 5555 -j ACCEPT
    root@localhost:~# mkdir /etc/iptables
    root@localhost:~# iptables-save > /etc/iptables/rules.v4
    root@localhost:~# docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' mysql
    172.18.0.4
    root@localhost:~# socat TCP-LISTEN:5555,fork TCP:172.18.0.4:3306
    
    • Then you can connect from your PC or minecraft server to the sql server using port 5555 as:
    $ mysql --user=e89d41145799e97 --password=e97e3fb41f3e52dd2462997d6008ccc0e0b0cffb9443b4ff --host=my.cloudron.domain --port=5555 e89d41668599e97
    mysql> show tables;
    Empty set (0.01 sec)
    

    You have to install the socat as a systemd service or such to make it persist reboots. Otherwise, just use nohup socat TCP-LISTEN:5555,fork TCP:172.18.0.4:3306 &



  • Thank you so much!

    Maybe make it an option to disable/enable the public access to the mysql server. Would be really great. Thanks!


  • Staff

    @Trankery Yup, I will mark this as a feature request for a future release.