Cirrus CI - Can't connect to MySQL additional container - mysql

I'm running a container on Cirrus CI, and in my .cirrus.yml, I've defined an additional_container to run a MySQL instance to test against as per the docs:
.cirrus.yml
container:
image: node:latest
additional_containers:
- name: mysql
image: mysql:8
port: 3306
cpu: 1.0
memory: 512Mi
env:
MYSQL_ROOT_PASSWORD: "pa55w0rd"
I'm trying to run a CREATE DATABASE command against that instance in one of my setup tasks:
...
mysql_setup_script:
- mysql -uroot -ppa55w0rd -P3306 -hlocalhost -e "CREATE DATABASE voluble_test;"
...
I've installed the MySQL client (but not the server, as this would defeat the object) on my testing container. However, MySQL acts as if it were connecting to a true localhost DB and looks for a locally-installed MySQL server, it appears - and fails with the following error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2 "No such file or directory")
That said, the Cirrus CI docs state that the MySQL instance should be available at localhost:
Tests will be able to access MySQL instance via localhost:3306.
I can't see an obvious way around this - any advice?

Using localhost will cause mysql not to use the network.
Use either 127.0.0.1 or your docker host IP, depending on network configs.
Notice that the error is not a network error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'
"through socket '/var/run/mysqld/mysqld.sock'", that file does not point to a mysql socket inside your docker container.
Grab your docker host IP with:
$ docker network inspect bridge --format='{{ (index .IPAM.Config 0).Gateway}}'

Related

Nextcloud in Docker & MySQL on Host

I'm running docker on my Raspberry Pi. I'm intending to run a few services locally (NextCloud, Bitwarden, etc) and want to use one MariaDB Instance, not one for each as most tutorial show. I've been trying to figure out how to make that work.
I installed mariadb on my RPI and nextcloud via Docker. I passed the MySQL environment variables to that container:
environment:
- MYSQL_PASSWORD=xxx
- MYSQL_DATABASE=dbname
- MYSQL_USER=user
- MYSQL_HOST=192.168.178.36
When I go to the NC initialization page and enter the data, it says Failed to connect to the database: An exception occurred in driver: SQLSTATE[HY000] [2002] Connection refused".
I addded "user#IP" to MySQL and granted it all access to the DB. The IP is the Docker-internal IP of the Container (172.xxx)

Docker nodejs with mysql communication

I'm using 2 docker images one with my nodeJS backend server the other with my MySQL database. On the docker-compose file I'm defining the passwords, ports and hostnames correctly:
sql:
image: mysql:5.7.22
hostname: sql
ports:
- 3306:3306
secrets:
- db_root_pass
- db_user_pass
environment:
MYSQL_USER: user
MYSQL_PASSWORD_FILE: /run/secrets/db_user_pass
MYSQL_ROOT_PASSWORD_FILE: /run/secrets/db_root_pass
provider:
image: monokilho/app:dev
hostname: provider
ports:
- 3000:3001
- 9221:9229
secrets:
- db_user_pass
command: node --inspect=0.0.0.0:9229 appModule.js
And on my DB_config.js file for NodeJS I have the connection setup like so:
db_config.host = 'sql';
db_config.port = '3306';
db_config.user = 'user';
db_config.password = fs.readFileSync('/run/secrets/db_user_pass', 'utf8');
db_config.database = 'app';
db_config.multipleStatements = true;
Problem is that although, using this exact configurations, docker connects Node to MySQL just fine on my local windows machine, when I upload the images to my remote linux server I continue to get:
Access denied for user 'user'#'8b2e56e566b2.network_default'
I've already remade the secrets, tried manually adding the passwords to the config on NodeJS and nothing... what makes it even weirder is that if I go on the MySQL container to connect directly or if I make another MySQL container and remotely connect it works, so I know the password input on MySQL config is correct and it is accepting remote connections.
Any suggestion what might be the difference between windows and linux for this behavior to happen? Thanks in advance.
PS: If needed windows is windows 10 and linux distro is ububtu 16.04.
EDIT: The access denied error appears on the mysql logs so the nodejs docker can reach the mysql docker and the network should be fine.
Apparently the mysql config was ignoring a sneaky \n on the password file allowing it to work normally with a command line connection, while on the nodejs it was bugging the connection.

cannot start vault container with mysql storage

I'm trying to start Vault docker container with mysql storage using this command:
docker run --cap-add=IPC_LOCK -e 'VAULT_LOCAL_CONFIG={"storage": {"mysql": {"username":"root", "password":"hello", "database":"vault", "address":"127.0.0.1:3306"}}, "listener": {"tcp":{"address":"127.0.0.1:8200", "tls_disable":"1"}}, "default_lease_ttl": "168h", "max_lease_ttl": "720h"}' -e VAULT_SKIP_VERIFY=true vault server
This is the error I'm getting:
Error initializing storage of type mysql: failed to check mysql schema
exist: dial tcp 127.0.0.1:3306: connect: connection refused
I can connect to mysql using the username and password I am supplying to the previous command.
I also made sure that the mysql is running on the 3306 port
[root#jwahba]# netstat -tlpn | grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 39552/mysqld
I checked out the vault official document (here) but it's not obvious what is wrong in my configuration. Any suggestions please ?
You are trying to connect to a db on localhost from a Docker container, but they are on different network stacks. Use --net="host" in your docker run command; 127.0.0.1 in your docker container will now point to your docker host.
Source: From inside of a Docker container, how do I connect to the localhost of the machine?

Can't connect to mysql container from localhost

I'm trying to set up a MySQL container for developing.
So I used docker-compose to set it up.
The container and the mysql looks OK. The thing is that I want to connect to it from a DBeaver client and I can't find how I do it.
Here is my docker-compose.yml:
version: '2'
services:
db:
image: 'mysql:5.7'
volumes:
- '~/dev/dbs-data/mysql:/var/lib/mysql'
restart: 'always'
expose:
- '3306'
ports:
- '3306:3306'
environment:
MYSQL_ROOT_PASSWORD: 'pass'
MYSQL_DATABASE: 'db'
MYSQL_USER: 'user'
MYSQL_PASSWORD: 'pass'
When I try to connect it from DBeaver I get:
java.sql.SQLException: null, message from server:
"Host '172.18.0.1' is not allowed to connect to this MySQL server"
UPDATE
I'm not trying to connect using the IP 172.18.0.1. I tried to connect using localhost:3306, 127.0.0.1:3306 and with the sub IP docker gave it 0.0.0.0:3306
UPDATE
After having success connecting on my Mac, I tried again with DBeaver on my linux and again:
Tried to connect with other tool, mysql workbench:
As you can see in the official image documention :
MYSQL_ROOT_HOST : By default, MySQL creates the 'root'#'localhost' account. This account can only be connected to from inside the container, requiring the use of the docker exec command as noted under Connect to MySQL from the MySQL Command Line Client. To allow connections from other hosts, set this environment variable. As an example, the value "172.17.0.1", which is the default Docker gateway IP, will allow connections from the Docker host machine.
So you have to set the MYSQL_ROOT_HOST variable with the address 172.18.0.1 that you can see in the error message.
On Docker, run this command to create a MySql container and it will expose the ports to the outside world of docker.
docker run --name <mysql-container-name> -p 3306:3306 -e MYSQL_ROOT_PASSWORD=<root-password> -e MYSQL_USER=root -e MYSQL_PASSWORD=<user-password> -d mysql:latest
Few points to note:
You may see below error when trying to connect with DBeaver:
Public Key Retrieval is not allowed
Solution: When creating a new connection on DBeaver, go to Driver Properties look for allowPublicKeyRetrievel and set it to TRUE. Also, if needed set useSSL to FALSE.
Test your connection from DBeaver or any other clients, and it should be working.
I am new to docker and was experiencing the same issue in Linux, it was an issue with the addresses allowed to accept connection; here is what worked out for me:
Find the MySql configuration file named mysqld.cnf
It would be: /etc/mysql/mysql.conf.d/mysqld.cnfOr if you have your own configuration file.
Edit the bind-address key in it. Set it as: bind-address = 0.0.0.0
This will allow to accept connections from any IP address Restart docker-compose by $ docker-compose down$ docker-compose up
Wait for MySQL to start, it should work fine now.

connecting error mysql db from host to docker container

I have a mysql server running on docker container.When I say docker-compose ps I see mysql is up. I have an ubuntu 16.04 host machine. I want to connect to mysql with command line.
I am trying this :
mysql --host=localhost --user=abc --password=123 abc --port=3310
but I am taking this error :
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
I dont want to go container and connect. And I should use port number 3310. Here is my related docker-compose.yml about mysql:
mysql-db:
image: mysql:5.7.11
command: mysqld --lower_case_table_names=1
ports:
- "3310:3306"
I am new on these things. How can I connect to mysql? Where I am wrong?
Thanks