Docker and MySQL can't connect - mysql

I can't for the life of me figure this out. I feel like I'm really close, but im just not quite there yet. Here's my Dockerfile:
FROM ubuntu:14.04
MAINTAINER Oscar Godson
RUN apt-get update
RUN apt-get install -y nodejs-legacy
RUN apt-get install -y npm
RUN apt-get install -y mysql-server
RUN apt-get install -y mysql-client
COPY . /src
WORKDIR /src
RUN service mysql start;\
mysql -u root < /src/bin/demo-data.sql;\
npm install
EXPOSE 4000
CMD ["node", "index.js"]
If i go into interactive mode and go into bash in that RUN service... line at the end I can see that mysql is running, the demo data was imported and everything works including starting my Node app with node index.js. If I then try to do another RUN or the CMD you see below it no longer can access MySQL. It's like it stops running.
The error I get Error: connect ECONNREFUSED which happens when it's down or the data isn't there. From me playing with it it looks like MySQL isn't running if I try to access it after the RUN where I start it.
P.S. The MySQL DB here is purely for development. On production we use AWS RDS, so thats why its using root without any password and importing data that way.

The problem was that RUN shouldn't be used for long running processes, however, you can only use one CMD option. For some reason it didn't click with me until a friend pointed it out, but I could just make a "installer.sh" script and call that with CMD. Now it's not losing the MySQL context. My update Dockerfile looks something like this:
FROM ubuntu:14.04
MAINTAINER Oscar Godson
RUN apt-get update
RUN apt-get install -y nodejs-legacy
RUN apt-get install -y npm
RUN apt-get install -y mysql-server
RUN apt-get install -y mysql-client
COPY . /src
WORKDIR /src
RUN cd /src;npm install
EXPOSE 4000
CMD ./bin/installer.sh
Installer than has
service mysql start
mysql -u root < ./bin/demo-data.sql
node index.js

Related

Dockerfile running from Mysql cannot access apt-get

When I run docker-compose up to install our MySQL server, I get the following error:
RUN apt-get -y update && apt-get upgrade -y:
/bin/sh: apt-get: command not found
from the relevant dockerfile code:
FROM mysql:5.7
RUN apt-get -y update && apt-get upgrade -y
This used to work fine a few months ago for my coworkers.
Apparently since Oracle bought MySQL in 2010, they have been converting everything over to their proprietary OS. In the last few months, they switched the default mysql package to Oracle OS from Debian.
See the packages here: https://hub.docker.com/_/mysql
You now need to specify the debian package like:
FROM mysql:5.7-debian
RUN apt-get -y update && apt-get upgrade -y

Installing workbench on ubuntu

I am trying to install MySQL Workbench on Ubuntu 20.04, and I run these commands:
sudo apt-get update && sudo apt-get upgrade
sudo apt install mysql-workbench
the error is:
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package mysql-workbench
I used this way to install before but now I do not know what to do, please help, I am new in Ubuntu.
try this:
download snapd service using apt-get
sudo apt-get install snapd
after that install workbench
sudo snap install mysql-workbench-community
when it's done You need to enter a command to allow this package to access the service. The command is:
sudo snap connect mysql-workbench-community:password-manager-service :password-manager-service
You have to add the repository in your source.list file:
echo "deb http://repo.mysql.com/apt/ubuntu/ eoan mysql-tools" | sudo tee /etc/apt/sources.list.d/mysql.list
then:
sudo apt update
sudo apt install mysql-workbench-community
MySQL has updated their repository for Focal Fossa and removed the repository for Eoan Ermine. To install MySQL Workbench on 20.04, either download the Workbench for 20.04 from MySQL archives or follow the alternate method mentioned below replacing eoan with focal.
Refer this:
https://askubuntu.com/questions/1230752/mysql-workbench-not-supporting-with-ubuntu-20-04-lts

How to install mysql-server in a Dockerfile?

I have a complex Dockerfile which install much more than just mysql-server so I cannot start from an existing mysql container.
When removing all the extra-stuff I get this Dockerfile
FROM ubuntu:latest
ENV MYSQL_ROOT_PASSWORD=root
ENV MYSQL_ROOT_USER=root
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update
RUN apt-get install -y apt-utils
RUN apt-get install -y mysql-server
RUN usermod -d /var/lib/mysql/ mysql
RUN service mysql start
Unfortunately, mysql does not want to start:
---> 57a66bd64c2c
Step 8/9 : RUN usermod -d /var/lib/mysql/ mysql
---> Running in 596df248c2e4
---> ee78442bcc56
Step 9/9 : RUN service mysql start
---> Running in 0d9e5803cf33
* Starting MySQL database server mysqld
...fail!
The command '/bin/sh -c service mysql start' returned a non-zero code: 1
What is my mistake?
Looks like you've removed the most important parts of your docker file. Here is the Official MySQL repo Docker file.
FROM oraclelinux:7-slim
ENV PACKAGE_URL https://repo.mysql.com/yum/mysql-8.0-community/docker/x86_64/mysql-community-server-minimal-8.0.2-0.1.dmr.el7.x86_64.rpm
# Install server
RUN rpmkeys --import http://repo.mysql.com/RPM-GPG-KEY-mysql \
&& yum install -y $PACKAGE_URL \
&& yum install -y libpwquality \
&& rm -rf /var/cache/yum/*
RUN mkdir /docker-entrypoint-initdb.d
VOLUME /var/lib/mysql
COPY docker-entrypoint.sh /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
EXPOSE 3306 33060
CMD ["mysqld"]
You need to include a proper source with correct version to pull the image from.
and expose right ports, separate out volumes for MySQL to run. your container maybe failing due to any of this. I'd say remove the MySQL part out of your dockerfile and run the rest of the container.
Use the official mySQL image and install it in separate container. and then you can connect the Database with other apps.

Could not install mysql-server inside docker container

I want to configure a container with MySQL, thereafter I will add a java application to the container. But, I could not install MySQL-server inside the container.
here is my dockerfile:
FROM ubuntu:latest
RUN apt-get -y update
RUN apt-get -y install mysql-server --fix-missing --fix-broken
EXPOSE 3306
CMD ["/usr/bin/mysqld_safe"]
I got the following error:
Errors were encountered while processing:
/var/cache/apt/archives/mysql-server-5.5_5.5.44-0ubuntu0.14.04.1_amd64.deb
E: Sub-process /usr/bin/dpkg returned an error code (1)
2015/09/01 13:17:36 The command [/bin/sh -c apt-get -y install mysql-server --fix-missing --fix-broken] returned a non-zero code: 100
What's probably happening is the apt-get install is asking for input, and failing. What happens if you just run those commands interactively in the base container? e.g. launch a shell with docker run -ti --rm ubuntu:latest /bin/bash
One thing you can try is to use the DEBIAN_FRONTEND=noninteractive environment variable, e.g.
RUN apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get -yq install mysql-server
I would suggest you use the official mysql docker image. When running that you have to provide the root password as an environment variable during the docker run command. That would be better.

Error installing mySQL in ubuntu - unable to locate package mysqld-server

I'm trying to install mysql server in ubuntu VM. I tried following commands:
sudo apt-get update && sudo apt-get upgrade
It gives an error saying that some files failed to download.
apt-cache search mysql
gives no result on mysql-server/mysqld-server
apt-get install mysqld-server
just gives the output in the title: unable to locate package mysqld-server
Could anyone give me a heads up? Thanks.
try this command
sudo apt-get install mysql-server-5.6