connecting mysqld-exporter in docker - mysql_up 0 - mysql

What did I do:
1. docker run --net minha-rede --name mysql01 -e MYSQL_ROOT_PASSWORD=Password1234 -d mysql
2. docker run --net minha-rede --name wordpress01 --link mysql01 -p 8080:80 -e WORDPRESS_DB_HOST=mysql01:3306 -e WORDPRESS_DB_USER=root -e WORDPRESS_DB_PASSWORD=Password1234 -e WORDPRESS_DB_NAME=wordpress -e WORDPRESS_TABLE_PREFIX=wp_ -d wordpress
3. docker exec -it mysql01 bash
4. mysql -u root -p
5. CREATE USER 'luckerman'#'localhost' IDENTIFIED BY 'onboard' WITH MAX_USER_CONNECTIONS 3;
6. GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'luckerman'#'localhost';
7. exit
8. exit
9. docker run -d \
-p 9104:9104 --name mysqlexp01\
--network minha-rede \
-e DATA_SOURCE_NAME="luckerman:onboard#(minha-rede:3306)/" \
prom/mysqld-exporter
But when I run the http://localhost:9104/metrics I have the message # TYPE mysql_exporter_last_scrape_error gauge
mysql_exporter_last_scrape_error 1
and when I run docker logs mysqlexp01 it shows me this:
time="2018-09-11T20:57:40Z" level=info msg="Starting mysqld_exporter (version=0.11.0, branch=HEAD, revision=5d7179615695a61ecc3b5bf90a2a7c76a9592cdd)" source="mysqld_exporter.go:206"
time="2018-09-11T20:57:40Z" level=info msg="Build context (go=go1.10.3, user=root#3d3ff666b0e4, date=20180629-15:00:35)" source="mysqld_exporter.go:207"
time="2018-09-11T20:57:40Z" level=info msg="Enabled scrapers:" source="mysqld_exporter.go:218"
time="2018-09-11T20:57:40Z" level=info msg=" --collect.info_schema.tables" source="mysqld_exporter.go:222"
time="2018-09-11T20:57:40Z" level=info msg=" --collect.global_status" source="mysqld_exporter.go:222"
time="2018-09-11T20:57:40Z" level=info msg=" --collect.global_variables" source="mysqld_exporter.go:222"
time="2018-09-11T20:57:40Z" level=info msg=" --collect.slave_status" source="mysqld_exporter.go:222"
time="2018-09-11T20:57:40Z" level=info msg="Listening on :9104" source="mysqld_exporter.go:232"
time="2018-09-11T20:57:44Z" level=error msg="Error pinging mysqld: dial tcp 127.0.0.1:3306: connect: connection refused" source="exporter.go:119"
What I did wrong? I tried many forums, sites, etc...

It worked! I did this:
docker network inspect minha-rede
Then I found the ip of my-sql (in my case 172.23.0.2)
Then I entered in my-sql and I did these commands:
CREATE USER 'luckerman'#'172.23.0.2' IDENTIFIED BY 'onboard' WITH MAX_USER_CONNECTIONS 3;
GRANT PROCESS, REPLICATION CLIENT, SELECT ON . TO 'luckerman'#'172.23.0.2';
Thank you #alex-karshin!

Related

Mysql login gives error: Keyring migration failed

I'm trying to set up a docker container running mysql server. I'm following the steps from the official image.
More precisely, I'm running the following commands, each in their own powershell prompt:
docker run -it --rm --name MySql_EC `
-e MYSQL_ROOT_PASSWORD=ecdev `
-v MySql_ec:/var/lib/mysql:rw `
mysql
and
docker run -it --rm --name mysql2 `
mysql -h localhost -u root -p
When I run the second command, I'm prompted for a password. I enter ecdev, and then I get the following error:
2023-01-17 08:01:05+00:00 [ERROR] [Entrypoint]: mysqld failed while attempting to check config
command was: mysqld -h localhost -u root -p --verbose --help --log-bin-index=/tmp/tmp.ZpxXEJPHAJ
mysqld: Can not perform keyring migration : Invalid --keyring-migration-source option.
2023-01-17T08:01:05.180474Z 0 [ERROR] [MY-011084] [Server] Keyring migration failed.
2023-01-17T08:01:05.181137Z 0 [ERROR] [MY-010119] [Server] Aborting
I was expecting a MySql repl. Instead I'm back at powershell.
So, assuming you want to do the "Connect to MySQL from the MySQL command line client" part of your linked documentation, you have two problems.
Your second Docker run command is incomplete as you don't name the image.
You want to connect to localhost, but with the second command you create a second container which refers to itself by localhost.
The following should work:
docker network create mysql-net
docker run -it --rm --name MySql_EC --network mysql-net `
-e MYSQL_ROOT_PASSWORD=ecdev `
-v MySql_ec:/var/lib/mysql:rw `
mysql:latest
docker run -it --rm --name mysql2 --network mysql-net `
mysql:latest mysql -h MySql_EC -u root -p
I only wrote mysql:latest in order to help you to distinguish between the image name parameter and the command that will be executed in the container after its creation. You can replace mysql:latest with mysql.
If you don't want to create and assign a network, you cannot refer to your parent container with the container name. However, you can do a docker inspect to get the IP of the container and connect through that.
Docker MySQL Access command
docker exec -it <cantainer_id> mysql -u<user> -p<password>

django.db.utils.OperationalError when running MySQL/MariaDB in Docker: Lost connection to MySQL server at 'reading initial communication packet'

Running MySQL/MariaDB in a Docker container:
docker run -p 3306:3306 --name $(DATABASE_NAME) -v /tmp/mysql:/var/lib/mysql -e MYSQL_DATABASE=$(DATABASE_NAME) -e MYSQL_USER=$(DATABASE_USER) -e MYSQL_ROOT_PASSWORD=$(DATABASE_PASSWORD) -d mariadb:latest > /dev/null
And then running Django version 4 locally with:
manage.py runserver 127.0.0.1:8000
Error
django.db.utils.OperationalError: (2013, "Lost connection to MySQL server at 'reading initial communication packet', system error: 2")
I can successfully connect to the database with MySQL Workbench as well as the command:
mysql -h 127.0.0.1 -P 3306 -u root -p <database>
I am launching Django and the MySQL/MariaDB Docker container from a Makefile.
Makefile
SHELL := /bin/bash
.PHONY: dj-start-local
dj-start-local: start-mysql
PYTHONPATH=. django_project/src/manage.py runserver 127.0.0.1:8000
.PHONY: start-mysql
start-mysql:
docker run -p 3306:3306 --name $(DATABASE_NAME) \
-v /tmp/mysql:/var/lib/mysql \
-e MYSQL_DATABASE=$(DATABASE_NAME) \
-e MYSQL_USER=$(DATABASE_USER) \
-e MYSQL_ROOT_PASSWORD=$(DATABASE_PASSWORD) \
-d mariadb:latest > /dev/null
Use the healthcheck.sh in the container. Use the MARIADB_MYSQL_LOCALHOST_USER=1 to create a mysql#localhost user that the script can use to access the database,
The healthcheck waits until its fully started regardless of the time.
Makefile:
.PHONY: start-mariadb
start-mariadb:
docker run -p 3306:3306 --name $(DATABASE_NAME) \
-e MARIADB_DATABASE=$(DATABASE_NAME) \
-e MARIADB_USER=$(DATABASE_USER) \
-e MARIADB_PASSWORD=$(DATABASE_PASSWORD) \
-e MARIADB_ROOT_PASSWORD=$(DATABASE_PASSWORD) \
-e MARIADB_MYSQL_LOCALHOST_USER=1 \
-v /tmp/mysql:/var/lib/mysql \
-d mariadb:latest
while ! docker exec $(DATABASE_NAME) healthcheck.sh --su=mysql --connect --innodb_initialized; do sleep 1; done
docker exec --user mysql $(DATABASE_NAME) mariadb -e 'select "hello world"'
Test run:
$ make start-mariadb
docker run -p 3306:3306 --name dd \
-e MARIADB_DATABASE=dd \
-e MARIADB_USER=dd \
-e MARIADB_PASSWORD=dd \
-e MARIADB_ROOT_PASSWORD=dd \
-e MARIADB_MYSQL_LOCALHOST_USER=1 \
-d mariadb:latest
53066fffa293ed061743024e387bd7fb6f1c664efd603c7a657ba88e307be308
while ! docker exec dd healthcheck.sh --su=mysql --connect --innodb_initialized; do sleep 1; done
healthcheck connect failed
healthcheck connect failed
healthcheck connect failed
healthcheck connect failed
docker exec --user mysql dd mariadb -e 'select "hello world"'
hello world
hello world
Note: added MARIADB_PASSWORD otherwise the database/user wouldn't be created.
The issue may be due to a race condition, where Django is attempting to connect to the database before it is ready. Try waiting a few seconds after starting the Docker container.
Simple Solution
Makefile
.PHONY: start-mysql
start-mysql:
docker run -p 3306:3306 --name $(DATABASE_NAME) \
-v /tmp/mysql:/var/lib/mysql \
-e MYSQL_DATABASE=$(DATABASE_NAME) \
-e MYSQL_USER=$(DATABASE_USER) \
-e MYSQL_ROOT_PASSWORD=$(DATABASE_PASSWORD) \
-d mariadb:latest
sleep 4
Better Solution using healthcheck.sh
In Dan Black's answer, he pointed out that the MariaDB docker container includes a script healthcheck.sh.
If you only want to check if the MariaDB container can receive connections, then healthcheck.sh can be run without specifying a user (avoiding authorization complications).
The Makefile below will check for a database connection every second until the database is ready.
Makefile (advanced version)
.PHONY: mysql-start
mysql-start: mysql-stop
docker run -p 3306:3306 --name $(DATABASE_NAME) \
-v /tmp/mysql:/var/lib/mysql \
-e MYSQL_DATABASE=$(DATABASE_NAME) \
-e MYSQL_USER=$(DATABASE_USER) \
-e MYSQL_ROOT_PASSWORD=$(DATABASE_PASSWORD) \
-d mariadb:latest
while ! docker exec $(DATABASE_NAME) healthcheck.sh --connect 2> /dev/null; do sleep 1; done

Bash script for creating default user in docker MySQL

I am developing a "one file script" for run MySQL database inside Docker and many useful thinks. The script should create a container with MySQL and after that create a new user with maximum privileges.
The problem occurs when I try to connect to MySQL command line from the bash script. Part of the script:
#!/bin/bash
mysqlContainerName=project-mysql
mysqlRootUsername=root
mysqlRootPassword=root_pass
mysqlUsername=db_user
mysqlPassword=db_pass
mysqlDb=project_db
echo -e "Creating container \e[31m$mysqlContainerName\e[0m"
docker run --rm -d \
--name=$mysqlContainerName \
--network api-network \
-p 3306:3306 \
-v /opt/docker-data/mysql-volumes:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=$mysqlRootPassword \
-e MYSQL_DATABASE=$mysqlDb \
-t mysql:8.0.19
docker exec $mysqlContainerName \
mysql -h 127.0.0.1 -P 3306 --protocol=tcp \
-uroot -p$mysqlRootPassword -e "CREATE USER '$mysqlUsername'#'localhost' IDENTIFIED BY
'$mysqlPassword';"
So, container successfully created, but all the time I get an error message: ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111)
Any suggestions?
So, after a few hours of searching, I found a solution.
Added bash -c parameter and in quotes connection to MySQL and query.
docker exec $mysqlContainerName bash -c "mysql -h172.21.0.1 -P 3306 --protocol=tcp -u$mysqlRootUsername -p$mysqlRootPassword -e \"CREATE USER '$mysqlUsername'#'localhost' IDENTIFIED BY '$mysqlPassword';\"; exit;"

mysqld_exporter refusing to connect to mariadb

I am running MariaDB and myqld_exporter both from docker. I run them like so :
docker run -p 127.0.0.1:3306:3306 --name mariadbtest -e MYSQL_ROOT_PASSWORD=mypass -d mariadb
docker run -p 9104:9104 -e DATA_SOURCE_NAME="root:mypass#(127.0.0.1:3306)/" prom/mysqld-exporter
Replacing the 127.0.0.1 IP with localhost or my docker-machine ip default of 192.168.99.100 makes no difference either, as I always get the following output after executing the second command :
$ docker run -p 9104:9104 -e DATA_SOURCE_NAME="root:mypass#(127.0.0.1:3306)/" prom/mysqld-exporter
time="2017-10-17T12:26:57Z" level=info msg="Starting mysqld_exporter (version=0.10.0, branch=master, revision=80680068f15474f87847c8ee8f18a2939a26196a)" so
urce="mysqld_exporter.go:460"
time="2017-10-17T12:26:57Z" level=info msg="Build context (go=go1.8.1, user=root#3b0154cd9e8e, date=20170425-11:24:12)" source="mysqld_exporter.go:461"
time="2017-10-17T12:26:57Z" level=error msg="Error pinging mysqld: dial tcp 127.0.0.1:3306: getsockopt: connection refused" source="mysqld_exporter.go:268"
time="2017-10-17T12:26:57Z" level=info msg="Listening on :9104" source="mysqld_exporter.go:479"
My intention is to have Prometheus use the exporter metrics to monitor MariaDB
Should anyone find it useful, I figured out the commands that do work :
docker run -p 3306:3306 --name mariadbtest -e MYSQL_ROOT_PASSWORD=mypass -d mariadb
docker run -p 9104:9104 -e DATA_SOURCE_NAME="root:mypass#(192.168.99.100:3306)/" prom/mysqld-exporter
it's a bit nicer to use links instead of ip adresses. You can start the exporter with docker run -p 9104:9104 --link mariadbtest -e DATA_SOURCE_NAME="root:mypass#(mariadbtest:3306)/" prom/mysqld-exporter. See https://docs.docker.com/engine/userguide/networking/default_network/dockerlinks/#connect-with-the-linking-system

Docker. Connect from one container to another container with mysql

I have a network:
docker network create -d bridge --subnet 172.25.0.0/16 container_network
I have one container with mysql running. Run command:
docker run -tid -p 3306:3306 --name container_mysql --network container_network container_mysql
And one container with code:
docker run -tid -v $(pwd):/code -p 5000:5000 --name container_code --network container_network container_code
I am trying to access mysql DB from my container with code, but nothing works:
mysql -h 172.17.0.1 -P 3306 -u root -p
mysql -h container_mysql -P 3306 -u root -p
mysql -h 127.0.0.1 -P 3306 -u root -p
mysql -h 0.0.0.0 -P 3306 -u root -p
First command gives me:
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0
Others:
ERROR 2003 (HY000): Can't connect to MySQL server on <host> (111)
Any ideas how to achieve the goal?
Edit 1
Dockerfile for Mysql container:
FROM ubuntu:16.04
RUN apt-get update
RUN echo "mysql-server mysql-server/root_password password 1234" | debconf-set-selections
RUN echo "mysql-server mysql-server/root_password_again password 1234" | debconf-set-selections
RUN apt-get install mysql-server -y
First of all, you should consider to use networks insted of link. First, link should be deprecated very soon. Second, when you put 2 container on the same network, docker add to the /etc/hosts of both containers a line for resolve the IP from container name. This is for allows you to access to a container with their name. In your case you can access to the mysql container from code container like this
mysql -h container_mysql
Look this link for more details about docker networking. This link for more info about docker-compose and networking.