Can't connect to MySQL running in docker - mysql

I'm trying to build docker image with some legacy LAMP development stack for development purposes.
Basically I'm taking ubuntu image and installing bitnami LAMP stack.
Here's Dockerfile I have so far:
FROM ubuntu
EXPOSE 80 443 3306
WORKDIR /opt
COPY setup.sh .
RUN chmod +x setup.sh
RUN ./setup.sh # this bash script downloads and runs installer
CMD /opt/bitnami/ctlscript.sh start && tail -f /opt/bitnami/apache2/logs/access_log
Then I'm running that container like this:
docker run --name dev -d -p 8080:80 -p 3307:3306 -v "C:\\dev\\project:/opt/bitnami/apache2/htdocs" aburov/lamp5.6
All works as expected (app from c:\dev\project is accessible throug localhost:8080 and it can access database) except the fact tha I can't connect to MySQL from the host using mapped 3307 port.
I've tried connect from MySQL Workbench and JetBrains' DataGrip both failing with similar error:
Communications link failure with primary. No active connection found for master.
java.io.EOFException: unexpected end of stream, read 0 bytes from 4 (socket was closed by server).
I've tried:
Using map to another host's port (3306, 3308, 10123) assuming there some conflicts;
Using different MySQL drivers.
MySQL version is 5.6.
What I'm missing?
Thank you in advance!

After checking if the ports are mapped correctky and also verified da alocal mysqlclient can connect to the server, there is another possibiliry.
MySQL is in default configuration as security measure , only accepting access from the localhost.
So you have to control and change the following parameter in the my.cnf
[mysqld]
bind-address=0.0.0.0
That would allow access from every ip
Additionally by default root has privileges for localhost only so it's not allowed to connect with that user from another hosts. To fix that we can execute following SQL commands from within container:
GRANT CREATE USER ON *.* TO 'root'#'%';
FLUSH PRIVILEGES;
Another option would be to create separate user (by runnin corresponding SQL from within container).

Related

How to connect to MySQL database cluster

I tried to install a MySQL cluster with the Docker image below.
mysql/mysql-cluster - Docker Image | Docker Hub
The Docker image is pulled and run successfully.
Despite that I could connect to the cluster in the terminal (as shown in the screen capture below), I don't know how to connect to it with MySQL Workbench or DBeaver.
In your docker run command, you can use -p 3306:3306 (or any available port). Then you can use <host>:<port> from Workbench or Dbeaver connection URL.
I assume that you already know how to add new DB connection to MySQL Workbench or DBeaver. The information that you want is the connection URL and the username/password of an authenticated user that you need to use to connect to your MySQL cluster.
For the connection URL: 192.168.0.10 (no port in your example)
You need to have your MySQL Workbench or DBeaver connect to the URL of the MySQL node, which is mysql1 node in your example. As shown in your screen capture, it is 192.168.0.10 without any explicit port. But if you have troubles with the URL, you can run docker ps to check what host and port that your mysql1 is running and exposed at.
For the username/password: root/tpffnrtm1 (the password is the value of MYSQL_ROOT_PASSWORD as shown in your docker run of MySQL node command)
I assume that you just want to connect the DB cluster by any means (root or non-root privileges is totally fine for you).

Docker Can't connect to MySql server on 0.0.0.0 (10049)

Using latest Docker desktop and mariadb/latest image I have created a container, which runs OK, local MySQL command work successfully.
BUT whatever I try to connect to the container with HeidiSQL, I get
"Can't connect to MySql server on 0.0.0.0 (10049)"
I think I have read at least 50 different "solutions" here and on other sites, but whatever I do, I get the same error, whatever IP I use.
Running Windows 10 Pro freshly updated, as with latest Docker desktop.
Created the container with
docker run -p 3306:3306 --name demo -e MYSQL_ROOT_PASSWORD=xyz -d mariadb/server --log-bin --binlog-format=MIXED
Changed binding in my.cnf to 0.0.0.0
Granted all rights to 'root'#'%' thru local mysql cli.
Restarted the whole shebang NUMEROUS times
Tried connections on 0.0.0.0, 127.0.0.1, localhost, 172.17.0.2, etc, etc, etc.
"docker ps" says PORT: "3306/tcp"
Using bash in the container, I can reach out and apt update etc, etc.
I'm out of options. Is there a kind guru out there with a new suggestion?

Docker instance of MySQL is not setting root password -- any login without a password is allowed

I set up a new computer running macOS 10.14.5 and Docker Desktop community 2.0.0.3 (31259) and I noticed that my MySQL container wasn't being created properly. This is the command that I had run on my previous computer:
docker run --name virtual-mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=mypassword -d mysql:5.7.22
That worked: it would properly set the root password. But on the new computer, when I run that command, the root password is NOT set. I can log in as the root user, but only if I leave the password blank. Only the root user can create a database.
But more curiously, I can specify ANYTHING as the user, e.g. asdf and successfully log in (!!!). When I log in as a non-existent user, I can only view the information_schema and I cannot create a database, however -- I get an error: Access denied for user ''#'localhost' to database 'asdf' -- which suggests that perhaps my client isn't sending the login credentials correctly (I am using Sequel Pro 1.1.2 as my MySQL client).
Can someone explain this strange behavior?
After some helpful nudging, I figured out where this was going off the rails.
First problem: one of the apps I installed had installed an instance of MySQL onto the host OS. I didn't notice it because I had done which mysql from an open Terminal window that didn't have its PATH updated.
Second problem: mysqld kept running a process that bound to port 3306.
I didn't notice this until running lsof -Pnl +M -i4 | grep 'LISTEN'lsof -Pnl +M -i4 | grep 'LISTEN' revealed it. I had my suspicions when I could bring up new dockerized instances bound to other ports.
So the solution was to THOROUGHLY delete mysql (and mysqld), then REBOOT to shake any dangling connections. Once I had done that, my dockerized versions ran fine.

Can't connect Docker Wordpress container to MySQL on host

I have an Ubuntu VM that I've spun up on my Mac. In the VM I have MySQL and Docker installed and I'm trying to run a container from a Wordpress image and connect to MySQL on the host vm. The Wordpress image documentation says to use:
$ docker run --name some-wordpress -e WORDPRESS_DB_HOST=10.1.2.3:3306 \ -e WORDPRESS_DB_USER=... -e WORDPRESS_DB_PASSWORD=... -d wordpress
I've substituted in the IP address assigned to the host vm and the appropriate user, password and database name environment variables. The container comes up but then shuts down after a short while and the docker logs show:
vagrant#docker-blogs:/vagrant$ docker logs je-wordpress
WordPress not found in /var/www/html - copying now...
Complete! WordPress has been successfully copied to /var/www/html
Warning: mysqli::mysqli(): (HY000/2002): Connection refused in - on line 10
MySQL Connection Error: (2002) Connection refused
Warning: mysqli::mysqli(): (HY000/2002): Connection refused in - on line 10
MySQL Connection Error: (2002) Connection refused
These repeat several times then it terminates.
Should this be doable? If so, what do need I use as the IP address for the host vm and do I need to configure anything else?
I assume you have default setting on that mysql.
You are trying to connect to your mysql from DIFFERENT network. This is forbidden by default in mysql.
Look for the setting of mysql:
grep bind-address /etc/mysql/my.cnf
grep skip-networking /etc/mysql/my.cnf
Comment out both of them (#bind-address, ...), or delete them.
restart your mysql service
service mysql restart
Allow the user to connect from the remote network. Connect to mysql and execute:
GRANT ALL ON database.* TO user#'xxx.xxx.xxx.xxx' IDENTIFIED BY 'PASSWORD';
change the database, user, xxx.xxx.xxx.xxx for the IP you are connecting from, and PASSWORD.
For enabling the IDontGiveADamn mode, just execute
GRANT ALL ON *.* TO root#'%' IDENTIFIED BY 'monkey';

How can I access my docker maria db?

My main question is that after I have created a docker container for my mariadb with the command docker run --name db -e MYSQL_ROOT_PASSWORD=test -d -p 3306:3306 mariadb how can I access the sql db?
Somewhere I have seen a solution using a temporal (after exit the container is deleted) container, but cannot find it anymore.
I am searching for a command like: sudo docker exec -it [other flags] [command] db.
Just mysql-client, no extra docker container
Install the mysql client on your host,
apt-get install mysql-client
then use the following command to access your database container.
mysql -u<user> -p<pass> -h $(docker inspect --format '{{ .NetworkSettings.IPAddress }}' <db-container>)
The command will automatically get the IP of your docker container.
Make sure to replace <user>, <pass> and <db-container> with your respective values. In your case:
mysql -uroot -ptest -h $(docker inspect --format '{{ .NetworkSettings.IPAddress }}' db)
Your command lets mariadb run at the standard port 3306. If not, you have to tell the mysql command the new port.
from Official Mariadb website:
Connecting to MariaDB from Outside the Container
If we try to connect to the MariaDB server on localhost, the client will bypass networking and attempt to connect to the server using a socket file in the local filesystem. However, this doesn't work when MariaDB is running inside a container because the server's filesystem is isolated from the host. The client can't access the socket file which is inside the container, so it fails to connect.
Therefore connections to the MariaDB server must be made using TCP, even when the client is running on the same machine as the server container.
Most MariaDB images, including the official one, have external TCP connections disabled using the bind-address option in their #my.cnf# file. The docker image used in this guide is based on Ubuntu, so the file is located at /etc/mysql/my.cnf.
To use MariaDB we will need to edit the configuration file to change the appropriate option, and then restart the container.
Inside the container, edit the file my.cnf and check for the line that begins bind-address. Put a hash at the start of the line to comment it out:
#bind-address = 127.0.0.1
Save the file.
While still inside the container, send the shutdown command to MariaDB. This will shut down the server and also exit back out to the host:
mysqladmin -u root -p shutdown
Start the container again. This time the MariaDB server will have networking enabled:
docker start mariadbtest
Find the IP address that has been assigned to the container:
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' mariadbtest
You can now connect to the MariaDB server using a TCP connection to that IP address.
Forcing a TCP Connection
After enabling network connections in MariaDB as described above, we will be able to connect to the server from outside the container.
On the host, run the client and set the server address ("-h") to the container's IP address that you found in the previous step:
mysql -h 172.17.0.2 -u root -p
This simple form of the connection should work in most situations. Depending on your configuration, it may also be necessary to specify the port for the server or to force TCP mode:
mysql -h 172.17.0.2 -P 3306 --protocol=TCP -u root -p
Port Configuration for Clustered Containers and Replication
Multiple MariaDB servers running in separate Docker containers can connect to each other using TCP. This is useful for forming a Galera cluster or for replication.
When running a cluster or a replication setup via Docker, we will want the containers to use different ports. The fastest way to achieve this is mapping the containers ports to different port on our system. We can do this when creating the containers (docker run command), by using the -p option, several times if necessary. For example, for Galera nodes we will use a mapping similar to this one:
-p 4306:3306 -p 5567:5567 -p 5444:5444 -p 5568:5568
First access the container terminal
docker exec -it some-mariadb bash
'some-mariadb' is the mysql container name
Then access the db directly using the mysql terminal command
mysql -u root -p
Connect to MariaDB from the MySQL command line client
The following command starts another mariadb container instance and runs the mysql command line client against your original mariadb container, allowing you to execute SQL statements against your database instance:
$ docker run -it --link some-mariadb:mysql --rm mariadb sh -c 'exec mysql -h"$MYSQL_PORT_3306_TCP_ADDR" -P"$MYSQL_PORT_3306_TCP_PORT" -uroot -p"$MYSQL_ENV_MYSQL_ROOT_PASSWORD"'
... where some-mariadb is the name of your original mariadb container.
More information about the MySQL command line client can be found in the MySQL documentation
Refer: https://hub.docker.com/_/mariadb/
Slightly different syntax, docker 18.05.0-ce on ubuntu 18.04:
sudo docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' db