Can't connect Docker Wordpress container to MySQL on host - mysql

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';

Related

MYSQL refuses remote connection with error "ERROR 2003 (HY000)" and TCP error 111

I have installed MySQL 8.0.25 on Ubuntu 20.04 LTS (ARM) instance on AWS.
I can access it using 127.0.0.1 address locally, but can't access it remotely from another instance.
"Bind-address" was commented out originally, I uncommented it and changed to "0.0.0.0" (mysql service was restarted) - didn't help, so I commented it back.
"Skip-networking" is not in the cnf file.
I changed the port to 3307 just to be sure that I'm looking at the right cnf, and now MYSQL does listen to port 3307:
locally I can connect using port 3307, but not remotely:
Here are the iptables:
I know that firewall works well, because if I remove port 3307 from the rules, the error is different:
As you can see, TCP error 111 ("Connection Refused") became error 113 ("No Route to Host").
Telnet connection is refused with the same error:
I've rebooted the MySQL instance - no change.
Why would MYSQL refuse remote connection, if "bind-address" is commented out, and firewall is open?
#stdunbar's comment pointed me in the right direction.
The problem happened because I followed the MYSQL installation instructions at https://www.digitalocean.com/community/tutorials/how-to-install-mysql-on-ubuntu-20-04 , and ran the security script while configuring MYSQL:
sudo mysql_secure_installation
For some reason, this script causes MYSQL to ignore the bind-address setting in the cnf file, while it still uses the port setting - very confusing!
When I installed MYSQL without running the security script, the "local address" of the MYSQL daemon in the netstat -lnp | grep mysql command changed from 127.0.0.1:
to 0:0:0:0 :
After this, MYSQL started to accept remote connections.
Of course, I still had to create a remote user:
CREATE USER 'root'#'remotehostname' IDENTIFIED WITH caching_sha2_password BY 'newpassword';
GRANT ALL PRIVILEGES ON *.* TO 'root'#'remotehostname' WITH GRANT OPTION;

Can't connect to MySQL running in docker

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

docker can't connect to mariadb from localhost with different user

Hi I have followed this docker tutorial for mariabdb and also checked this stackoverflow question as well. I can start to mariadb container without any issues and can connect with root and mypass password with command mysql -h0.0.0.0 -uroot -pmypass. However, after I create a new user and grant all on it, I am not able to connect with that specific username and password. I am getting access denied error.
Here is the docker ps command output;
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
268c36eac13a mariadb/server:10.3 "docker-entrypoint.s…" 31 minutes ago Up 31 minutes 0.0.0.0:3306->3306/tcp mariadbtest
I connect to mariadb container with docker exec -it 268c36eac13a /bin/sh and run these commands;
mysql -uroot -pmypass
create database emails;
grant all on emails.* to 'testUser'#'localhost' identified by '123456';
Then, from localhost when I am run mysql -h0.0.0.0 -utestUser -123456 I am getting Access denied for user 'testUser'#'172.17.0.1' (using password: YES) error.
Thanks for help!
I think this is a problem with networking : you allow user testUser to connect to your database from localhost but this means he will be able to connect only from client running inside the container. As you can see when you are logging to your MariaDB container from host machine you get your host docker address 172.17.0.1.
One solution is to allow your user to connect from any ip :
grant all on emails.* to 'testUser'#'%' identified by '123456';
or IP from given subnet. You can check how to do it in CREATE USER docs.
Another solution would be to add user for IP of host machine. You can get the host IP from within the container and add user for this IP. On how to get host IP from the container check this question.

How to connect to WSL mysql from Host Windows

I am trying to connect HeidiSql from the host to my WSL Mysql but I could not get it to connect it
Error "can't connect to Mysql server on '127.0.0.1'"
Tried SSH too but could not connect to the server
I'm also hosting mysql-server on WSL and running MySQL workbench on Windows.
I had to get the IP inside of WSL
And use this IP in MySQL Workbench
By default, MySql only listens on 127.0.0.1. If you are connecting using the IP address returned by wsl hostname -I (as mentioned by Permana) then you need to change /etc/mysql/mysql.conf.d/mysqld.cnf to listen on all IPs or your specific IP.
Edit your MySql config by typing this in your Ubuntu instance:
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
Change the line:
bind-address = 127.0.0.1
To:
bind-address = 0.0.0.0
or change 0.0.0.0 to the IP returned by wsl hostname -I.
Restart MySql using: sudo service mysql restart
Since your question is asked before WSL2 release, I assume you were using WSL1.
For WSL1
You can access WSL1 MySQL directly from Windows, but you were attempting access in a wrong way.
In the Network type, you should choose MariaDb or MySQL(TCP/IP) instead of MySQL (SSH Tunnel).
For WSL2
Check this WSL github issue. Save #edwindijas's powershell script and execute it by administrator. If you still cannot access MySQL and got access denied for user ... <you-computer-name>.mshome.net, you need to allow this user access from this host.
For example: let's say root, you need execute this in mysql cli:
GRANT ALL PRIVILEGES ON *.* TO 'root'#'%<you-computer-name>.mshome.net' IDENTIFIED BY '<password>';
Or allow root user access WSL2 MySQL from any host:
GRANT ALL PRIVILEGES ON *.* TO 'root'#'%' IDENTIFIED BY 'password';
As for me who's also using WSL for making web based application
first make sure mysql is running on WSL like sudo service mysql start
then once started, open HeidiSql and simply connect to it, here the example on my part
make sure the IP address is 127.0.0.1 not any IP, not your IP used to connect on the internet

Fix configuration problems with MacPorts MySQL

I have mysql/mysqli installed on my local machine via MacPorts and can access mysql/mysqli commands from the command line ONLY when using locahost.
If I try running:
mysql -u root -h 127.0.0.1 -p
I get the following error:
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (61)
When running:
mysql -u root -h localhost -p
Everything is fine.
The reason this bothers me is that I am trying to use mysqli_connect in a PHP script running on localhost. When I enter the host as localhost, it gives me this error:
Warning: mysqli_connect(): (HY000/2002): No such file or directory in ... on line 2
bool(false) Failed to connect to MySQL: No such file or directory
And when I enter the host as 127.0.0.1, I get a different error:
Warning: mysqli_connect(): (HY000/2002): Connection refused in /Users/raph/coding/webroot/rugapp/qbo-palmer/docs/partner_platform/example_app_ipp_v3/test.php on line 2
bool(false) Failed to connect to MySQL: Connection refused
It seems as though I have some configuration problems. What is the best way to fix them?
The connection to localhost uses a unix socket. It seems the correct socket path is built into the mysql binary you use, which explains why it works on the command line. For the unix socket connection to work from PHP, you need to specify MacPorts MySQL's non-standard socket path as you would normally specify a port number.
For the failure to connect using 127.0.0.1 (which forces IPv4 and avoids the unix socket), see mysql running but cannot connect to 127.0.0.1.