-1

I've written scripts to connect to mysql databases in Javascript. One I've done is like:

function connectToSQL() {
    return new Promise((resolve,reject)=>{
        try {
            var connection = mysql.createConnection({
                host:"0.0.0.0",
                user: "username",
                password: "password",
                database: "mydb"
            });
        } catch(e) {
            reject(e);
        }
        if(!connection) {
            reject(new Error("No Connection"));
        }
        connection.connect(function(err) {
            if(err) reject(err);
            resolve(connection);
        });
    });
}

However, that was a standalone Mysql, not inside a docker container, I was connecting to. Inside my Docker, the Sql container can be found:

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                                                     NAMES
fcd8a5277248        mysql:5.6           "docker-entrypoint..."   2 weeks ago         Up 2 weeks          0.0.0.0:3306->3306/tcp                                                    db-mysql

How would I write a JS script to get into this to run queries?

AviG
  • 362
  • 2
  • 14

1 Answers1

1
PORTS
0.0.0.0:3306->3306/tcp

This means that you'd like to listen on every network interface on your computer on port 3306 and redirect any traffic there to inside your Docker container (also on port 3306).

This means that connecting to 127.0.0.1:3306 will result in connecting to [the Docker instance]:3306.

Note that sometimes 'localhost' and '127.0.0.1' are not the same thing for MySQL's connection driver—it depends on what you're using—so use the IP to connect.

RickN
  • 12,537
  • 4
  • 24
  • 28