Unable to connect to host mysql in docker container [duplicate] - mysql

This question already has answers here:
From inside of a Docker container, how do I connect to the localhost of the machine?
(40 answers)
Closed 3 months ago.
I want to connect to host mysql in container, and I have tried some methods but none of them work.
Container: python & mysql client
Host: mysql service
Host mysql setting:
sudo apt install -y mysql-server
apt install -y default-libmysqlclient-dev
# And modify bind address: /etc/mysql/mysql.conf.d/mysqld.cnf
# bind-address = 0.0.0.0
Docker container run command's setting:
RUN apt-get update &&\
apt install -y default-mysql-client &&\
apt-get install -y gcc &&\
apt-get install -y default-libmysqlclient-dev &&\
/usr/local/bin/python -m pip install --upgrade pip &&\
pip install --no-cache-dir SQLAlchemy
My python file in container:
from sqlalchemy import create_engine
from sqlalchemy import text
SQLALCHEMY_DATABASE_URI = "mysql://root:pwd#localhost:3306/mysql?charset=utf8mb4"
engine = create_engine(SQLALCHEMY_DATABASE_URI, encoding='utf8', pool_pre_ping=True) # for mysql
with engine.connect() as connection:
result = connection.execute(text("show tables;"))
print(result.all())
Envrionment:
Ubuntu: 20.04
Docker version: 20.10
Mysql version: 8.0
My purpose is to connect to mysql throught sqlalchemy in container, but not even through mysql-client.
Therefore, I try to use mysql-client to connect, but still failed as same reason.
I have tried several method:
allow bind-address
run container with: --add-host host.docker.internal:host-gateway
without --add-host, try IP address
Most of the solution just say add --add-host host.docker.internal:host-gateway and it can work,
But not work for me.
Does anyone solve this problem?
Thanks!
Update:
I have tried a method that works.
run container --network=host
(in container)# mysql -h 127.0.0.1 -u root ...
But I don't want to use --network=host, how can I connect to host mysql with "host.docker.internal"?
Update:
Problem solved.
I run container with --add-host host.docker.internal:host-gateway
connect to db with: mysql -h host.docker.internal
And in mysql, update mysql.user root's host to '%'.
(allow non-localhost connect)

A couple things:
Using mysql without -h or --host by default tries to connect to localhost via a socket file.
Using mysql -h host.docker.internal -u root -p is correct. However, as the error message states, you'll need to provide permission for the root user to connect via non-localhost. If using the mysql container, run with -e MYSQL_ROOT_HOST=% or similar set.
I don't know which command you're using to start the mysql container. Make sure you're publishing the ports with -p 3306:3306 to all for cross-container communication.

Related

How to set up root password while installing mysql in docker?

I am trying to install mysql in docker and setting default password of root as root using following Dockerfile.
Dockerfile:
FROM ubuntu:20.04
COPY mysql_setup.sh /software/mysql_setup.sh
RUN sh /software/mysql_setup.sh
CMD ["/bin/sh"]
Where /software/mysql_setup.sh
# Download and Install the Latest Updates for the OS
apt-get update && apt-get upgrade -y
# Set the Server Timezone to CST
echo "America/Chicago" > /etc/timezone
dpkg-reconfigure -f noninteractive tzdata
# Enable Ubuntu Firewall and allow SSH & MySQL Ports
ufw enable
ufw allow 22
ufw allow 3306
# Install essential packages
apt-get -y install zsh htop
# Install MySQL Server in a Non-Interactive mode. Default root password will be "root"
echo "mysql-server mysql-server-8.0.26/root_password password root" | sudo debconf-set-selections
echo "mysql-server mysql-server-8.0.26/root_password_again password root" | sudo debconf-set-selections
apt-get -y install mysql-server
# Run the MySQL Secure Installation wizard
mysql_secure_installation
sed -i 's/127\.0\.0\.1/0\.0\.0\.0/g' /etc/mysql/my.cnf
mysql -uroot -p -e 'USE mysql; UPDATE `user` SET `Host`="%" WHERE `User`="root" AND `Host`="localhost"; DELETE FROM `user` WHERE `Host` != "%" AND `User`="root"; FLUSH PRIVILEGES;'
service mysql restart
But above code does not seem to be working as when i enter into docker and use and use root as password it gives an error.
root#839f2946bfdf:/# mysql -u root -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111)
How can i fix this error?
You cannot run a mysql install inside a container in a normal way. Thing like ufw cannot be done. The entire RUN step is at build time only, so service mysql restart won't be running when the container is started, only CMD is.
Recommend just using the Docker Library mysql container. There's a standard MYSQL_ROOT_PASSWORD variable for setting the root password.

How to start mysql and set up the mysql root password for a docker container?

I am trying to host a web server via docker.The web server needs mysql database.But I am failing to change the root password of mysql and also start the mysql and apache2 service.
So can someone help me to set the root password and start start the services.
Here is my try to setup the docker container.
FROM ubuntu:16.04
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get install apache2
RUN apt-get -q -y install mysql-server
CMD service mysql start

MariaDB never starts within docker image

I'm creating a docker image with ubuntu trusty and MariaDB 5.5 but I can never get MariaDB to start unless I actually connect to the running VM.
Dockerfile:
FROM ubuntu:trusty
# Upgrade packages
RUN apt-get update && apt-get upgrade -y
# So we can add a repo to apt
RUN apt-get install -y software-properties-common
# Add MariaDB repo to aptitude
RUN apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xcbcb082a1bb943db
RUN add-apt-repository 'deb http://mirror.jmu.edu/pub/mariadb/repo/5.5/ubuntu trusty main'
RUN apt-get update
# Install MariaDB
RUN DEBIAN_FRONTEND=noninteractive debconf-set-selections << 'mariadb-server-5.5 mysql-server/root_password password PASS'
RUN DEBIAN_FRONTEND=noninteractive debconf-set-selections << 'mariadb-server-5.5 mysql-server/root_password_again password PASS'
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y mariadb-server
# Start MariaDB
RUN service mysql start
# Configure MariaDB User permissions
RUN echo "CREATE USER 'ubuntu'#'localhost'" | mysql -u root
The command I'm running to create:
docker build -t ebth-com-trusty --file `pwd`/Dockerfile `pwd` --no-cache
The create command will always fail due to:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111)
But if I comment out the CREATE USER command, and then connect to the VM, I can connect to MariaDB just fine after running a manual service mysql start.
It is as if the RUN service mysql start just doesn't work, and I'm not sure how to debug this any further.
Every command in a Dockerfile is run in it's own container.
You can think of the process like:
new container is spun up using the previous image
the command is executed
the container is spun down
an image of that container is taken for the next command to run on
This means that the command RUN service mysql start will spin up a new container, start MariaDB, then shut down the container including shutting down MariaDB.
Instead try using CMD and ENTRYPOINT. You can think of them as setting the default executable, command and parameters executed once the container is spun up. However, the difference between the two is a bit more neuanced. Check out the docs: CMD and ENTRYPOINT
It would look something like this:
# Create default user
RUN service mysql start && \
echo "CREATE USER 'ubuntu'#'localhost'" | mysql -u root
# Start MariaDB
ENTRYPOINT ["/bin/bash"]
CMD ["service", "mysql", "start"]

Connect to RDS MySQL instance from ec2 Linux AMI - mysql command not found

I try to connect to my RDS MySQL instance from an SSH connection to my ec2 server (through PUTTY) as outlined here:
http://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_ConnectToInstance.html#USER_ConnectToInstance.CLI
But I get:
-bash: mysql: command not found
I think maybe I have to install the MySQL Utility client on my ec2 linux server? If this is the case how do I do this?
I try to run the mysql command from my /home/ec2-user directory.
When using Amazon Linux 2 AMI, we need to install mysql. Use this command to install mysql on the instance:
sudo yum install mysql
And then you can connect using this command:
mysql -h change-to-your-rds-endpoint.rds.amazonaws.com -u <USER> -p
Install mysql client on AWS Linux EC2:
$ sudo yum install mysql
Connect:
$ mysql -h endpoint -P port -u masteruser -p
If you enter your password and nothing happens, check your security groups. You may need to edit your inbound rules.
First install mysql into your ec2 instace using below command
sudo apt-get install mysql-server mysql-client
After installing mysql try with below cmd to connect RDS
mysql -h hostname -u username - p password
You'll need to install MySQL. Amazon AMIs use yum instead of apt so use:
sudo yum install mysql-server mysql-client
if you are working to setup a LAMP (Linux, Apache, MySQL, PHP) server then follow these instructions http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/install-LAMP.html
Install the mysql client lib with the package manager of your system, for example on Ubuntu:
apt-get install mysqlclient

How to start mysql server in docker container

I am creating docker container and base image is ubuntu:14.04. I have to start mysql server in that container and then I have to create databases and I have to give the permission to the users. Docker is new for me. I tried a lot but still whenever I go to that image and check for mysql server is running or not. Every time what I got is mysql server is stopped and my dbs are also not created.
This is my dockerfile
FROM ubuntu:14.04
MAINTAINER <name> <emailid>
RUN sudo apt-get update
RUN sudo apt-get install -y apache2 mysql-server libapache2-mod-auth-mysql php5-mysql php5 git
RUN sudo apt-get install -y vim
CMD sudo /usr/sbin/mysqld -u mysql
I tried a lot but i am not able to run mysql server in docker image.
Did you manually install it in your container?
Why do you not simply use:
docker run -d --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=secret -e MYSQL_DATABASE=mySchema mysql:5
That would start a container named mysql running a mysql daemon, setting the default root password to secret, creating a new schema called mySchema and expose the MySQL port 3306 to clients so that they could connect.
Then you could connect to that with any MySQL client using the root user with the specified password secret and create your tables within the created schema mySchema.
I had similar issue for ubuntu :14.04 while setting up druid cluster in the docker container, using CMD to start mysql fixed it for me.
CMD mysql start \
Druid related stuff
&& mysql stop
docker exec -it container_name/id bash
service mysql status to check on the service status
service mysql start to start the mysql service