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.



  • 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).



  • 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)
    

Log in to reply