Can't connect to local MySQL server using Docker - mysql

Please help me to solve this problem:
I am using docker to create a mysql container and initialize a database in it automatically based on mysql:8.0.19. But when I get into the container and enter the pass word after 'mysql -u root -p', it is said that:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'
There are four files: Dockerfile, schema.sql, privileges.sql, setup.sh
Dockerfile:
FROM mysql:8.0.19
# allow no password
ENV MYSQL_ALLOW_EMPTY_PASSWORD yes
# copy file into container
COPY setup.sh /mysql/setup.sh
COPY schema.sql /mysql/schema.sql
COPY privileges.sql /mysql/privileges.sql
# exec these command when container start up
CMD ["sh", "/mysql/setup.sh"]
setup.sh:
#!/bin/bash
echo 'checking mysql status.'
service mysql status
echo '1.start mysql....'
service mysql start
sleep 3
service mysql status
echo '2.start importing data....'
mysql < /mysql/schema.sql
echo '3.end importing data....'
sleep 3
service mysql status
echo '4.start changing password....'
mysql < /mysql/privileges.sql
echo '5.end changing password....'
sleep 3
service mysql status
echo 'mysql is ready'
tail -f /dev/null
privileges.sql:
use mysql;
select host, user, plugin from user;
ALTER USER 'root'#'localhost' IDENTIFIED BY '123456';
FLUSH PRIVILEGES;
schema.sql:
create database `collector`;
use collector;
DROP TABLE IF EXISTS `EVENT`;
CREATE TABLE `EVENT` (
`ID` VARCHAR(50) NOT NULL,
`EventType`VARCHAR(50) DEFAULT NULL,
`EventID` VARCHAR(50) DEFAULT NULL,
`EpcID` VARCHAR(50) DEFAULT NULL,
`TimeStamp` FLOAT DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `EVENT` (`ID`, `EventType`, `EventID`, `EpcID`, `TimeStamp`)
VALUES('0','0','0','0',0.0);
I think privileges.sql might be the one that causing the problem, but i have no idea how to solve it.
By the way I use:
docker build . -t mysql_service:1.0
docker run -p 3306:3306 --name mysql_service -e MYSQL_ROOT_PASSWORD=123456 -d mysql_service:1.0
to build and run.

To set a root password use MYSQL_ROOT_PASSWORD=123456.
MYSQL_DATABASE=collector is also available to create the database and use it as the default.
FLUSH PRIVILEGES aren't needed for ALTER USER.
The docker container doesn't have a init so service ... commands won't work. Also the mysqld isn't running in the build stage.
https://hub.docker.com/_/mysql/ under Initializing a fresh instance you can put SQL files in /docker-entrypoint-initdb.d to initialize the database.

Related

Ansible : Ansible playbook / tasks for MySQL operations for windows machine

Good Day..
As per the ansible documentation, I can not find MySQL related tasks for Wondows machine.
I need to create manual tasks to perform automated tasks for windows machine.
I have performed these tasks successfully
- name: Install mysql-connector
win_chocolatey:
name: mysql-connector
version: '8.0.28'
state: present
- name: Connection
raw: mysql -h localhost --user=root --password= --execute "CREATE DATABASE testDB"
This tasks installs mysql and creates database named testDB in my virtual windows machine.
Now I want to create table inside that database. I have tried 2 ways. both are having issues for me
1.
- name: Create Table
raw : mysql -h localhost --user=root --password= --execute "CREATE TABLE `testDB`.`test1` (`employeeName` VARCHAR(20) NULL,`email` VARCHAR(45) NULL);"
-- Here, ` sign is behaving odd.
Error: Unknown database 'estDB'\r\n", // It is omitting first character from testDB
2.
I am trying to use dumped .sql file and want to import it directly. So that it can import DB with tables and fields.
- name: Table creation1
raw: mysql -h localhost --user=root --password= --execute "test < teamManagementApp.sql"
But here, < this character shows as reserved character for windows machine
Can someone help here?

MySQL 8 host not privileged

I'm installing MySQL 8 on an Arm EC2 instance and I cannot get it to work with remote connections. Here's the steps I do to install MySQL:
yum -y install https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm --nogpgcheck
yum -y install mysql-community-server --nogpgcheck
I then start MySQL with:
systemctl start mysqld
Then I login as root to MySQL and create a sample database with:
CREATE DATABASE gtfs DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;
USE gtfs;
CREATE TABLE agency (
agency_id VARCHAR(255) NOT NULL PRIMARY KEY,
agency_name VARCHAR(255),
agency_url VARCHAR(255),
agency_timezone VARCHAR(255),
agency_lang VARCHAR(255)
);
INSERT INTO agency VALUES ('id', 'name', 'url', 'timezone', 'lang');
So now I have a database created. If I try to connect from a client as root I get ER_HOST_NOT_PRIVILEGED. I've followed along here and created a new user with:
CREATE USER 'lambda'#'%' IDENTIFIED BY 'Password1!';
GRANT ALL ON *.* TO 'lambda'#'%';
and tried it out. I first get ER_NOT_SUPPORTED_AUTH_MODE which I can fix with:
UPDATE mysql.user SET plugin='mysql_native_password' WHERE User='lambda';
And then I'm back getting ER_HOST_NOT_PRIVILEGED with my lambda user. I've also added
bind-address=0.0.0.0
to my /etc/my.cnf file and restarted MySQL with systemctl restart mysqld and it still isn't working.
Running mysql -V outputs
mysql Ver 8.0.28 for Linux on aarch64 (MySQL Community Server - GPL)
Any help is massively appreciated. I'm tearing my hair out at this point. I've checked and I have port 3306 open on the EC2 instance. I cannot figure this out.

Metabase is not working (starting) with MySQL

I read the documentation about running metabase with docker from here and setting metabase up with mysql from here and followed all the instruction as per the documentation.
Here is the docker-compose.yml file I used to run metabase:
# docker-compose.yml
version: '3'
services:
metabase:
image: metabase/metabase:v0.39.1
environment:
- MB_DB_TYPE=mysql
- MB_DB_DBNAME=metabase
- MB_DB_PORT=3306
- MB_DB_USER=secretuser
- MB_DB_PASS=secretword
- MB_DB_HOST=secrethost
ports:
- 3000:3000
When I run docker-compose up I get the following error.
metabase_1 | 2021-05-17 08:27:59,084 ERROR metabase.core :: Metabase Initialization FAILED
metabase_1 | liquibase.exception.MigrationFailedException: Migration failed for change set migrations/000_migrations.yaml::1::agilliland:
metabase_1 | Reason: liquibase.exception.DatabaseException: (conn=32027) Table 'core_organization' already exists [Failed SQL: CREATE TABLE `metabase`.`core_organization` (`id` INT AUTO_INCREMENT NOT NULL, `slug` VARCHAR(254) NOT NULL, `name` VARCHAR(254) NOT NULL, `description` TEXT NULL, `logo_url` VARCHAR(254) NULL, `inherits` BIT(1) NOT NULL, CONSTRAINT `PK_CORE_ORGANIZATION` PRIMARY KEY (`id`), UNIQUE (`slug`)) ENGINE InnoDB CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci]
metabase_1 | at liquibase.changelog.ChangeSet.execute(ChangeSet.java:637)
..
..
metabase_1 | Caused by: java.sql.SQLException: Table 'core_organization' already exists
metabase_1 | at org.mariadb.jdbc.internal.protocol.AbstractQueryProtocol.readErrorPacket(AbstractQueryProtocol.java:1688)
metabase_1 | at org.mariadb.jdbc.internal.protocol.AbstractQueryProtocol.readPacket(AbstractQueryProtocol.java:1550)
metabase_1 | at org.mariadb.jdbc.internal.protocol.AbstractQueryProtocol.getResult(AbstractQueryProtocol.java:1513)
metabase_1 | at org.mariadb.jdbc.internal.protocol.AbstractQueryProtocol.executeQuery(AbstractQueryProtocol.java:256)
metabase_1 | ... 57 more
metabase_1 | 2021-05-17 08:27:59,109 INFO metabase.core :: Metabase Shutting Down ...
I also tried to run directly without using compose file using this command:
docker run -d -p 3000:3000 -e "MB_DB_TYPE=mysql" -e "MB_DB_DBNAME=metabase" \
-e "MB_DB_PORT=3306" -e "MB_DB_USER=secretuser" -e 'MB_DB_PASS=secretpass' \
-e "MB_DB_HOST=secrethost" --name metabase metabase/metabase:v0.39.1
I get the exactly same error as with the compose file.
On further searches I found a similar issue in github stating that "Table 'core_organization' already exists", however, they claim that it is already fixed but I'm still having the issue. In the issue page they suggest: java -jar metabase.jar migrate force. How can I run this from docker ?
NOTE: I have tried to drop all the table and start with fresh schema multiple times and it didn't work as well.
NOTE: the database I use is AWS RDS, just to make sure that it's not related with AWS.
The root of the problem was that I didn't have permissions to ALTER and REFERENCE in AWS RDS. Once I got those permissions everything worked fine.

problem with running of mysql image in docker

I have such a problem, I'm trying to do master-slave replication according to this tutorial https://developpaper.com/master-slave-replication-of-mysql-based-on-docker/ .
There is no problem with the building of the image, but there is a problem with running this container. I can't get access to the mysql commands of mysql container in docker, it seems to be built from mysql (image from docker hub), but can not be started.
There are the following files:
Dockerfile:
#Using MySQL image to create a new image
FROM mysql:latest
ENV MYSQL_ROOT_PASSWORD Kohc9hai
COPY start.sh /mysql/start.sh
COPY my.cnf /etc/mysql/my.cnf
COPY init.sql /mysql/init.sql
EXPOSE 6603
CMD ["sh", "/mysql/start.sh"]
init.sql:
--Create data_ Copy database
DROP DATABASE IF EXISTS `data_copy`;
CREATE DATABASE `data_copy` /*!40100 DEFAULT CHARACTER SET utf8mb4 collate utf8mb4_general_ci */;
--Create person table
USE `data_copy`;
DROP TABLE IF EXISTS `person`;
CREATE TABLE `person` (
`id` int(32) NOT NULL,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
my.cnf:
[mysqld]
log-bin = mysql-bin
server-id = 1
start.sh:
#!/bin/sh
echo "start MySQL"
service mysql start
sleep 5
echo "initialize database"
mysql -uroot -pKohc9hai < /mysql/init.sql
echo "initialization complete"
tail -f /dev/null
By getting deeper, I think that I determined that the problem is in the file start.sh (Maybe I'm wrong).
There are logs of this container:
start MySQL
mysql: unrecognized service
initialize database
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)
initialization complete
Maybe someone can help me with the solution to this problem.
I'v just started to study Docker.
mysql: unrecognized service
From next, you could see there is no mysql service in container, so you surely failure:
$ docker run --rm mysql ls /etc/init.d
hwclock.sh
In fact, mysql image use next to start mysql service:
exec gosu mysql "$BASH_SOURCE" "$#"
So you need to follow the same way.
BUT, for your scenario, looks you just customize start.sh to init some sql, while it's in fact already supported in official image:
Initializing a fresh instance
When a container is started for the first time, a new database with the specified name will be created and initialized with the provided configuration variables. Furthermore, it will execute files with extensions .sh, .sql and .sql.gz that are found in /docker-entrypoint-initdb.d. Files will be executed in alphabetical order. You can easily populate your mysql services by mounting a SQL dump into that directory and provide custom images with contributed data. SQL files will be imported by default to the database specified by the MYSQL_DATABASE variable.
So, what you need to do is next in Dockerfile (Maybe need to specify MYSQL_DATABASE also):
COPY init.sql /docker-entrypoint-initdb.d
The document is quite old (docker version 1.13.1! Now 19.03.13!!). No special setting is required to use MySQL image. All you need to do is:
docker run --name foo -d -p 6603:6603 mysql:latest

Docker - MySQL commands within Dockerfile using RUN (ERROR 2002)

I am using Docker to create a dockerfile with mysql as the base image:
FROM mysql
#set root pass
ENV MYSQL_ROOT_PASSWORD password
#update linux
RUN apt-get update
#create database
RUN mysql -u root -ppassword -e "CREATE DATABASE dbname"
#install vim
RUN apt-get install vim -y
The dockerfile fails on the step where I try to create a database, it doesn't finish building and i receive this error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'
When I remove the #create database run command the dockerfile will build and I am able to run a container from that image. I know that it isn't a problem with the mysql server as I can enter the container and run the mysql command manually with success and the service status is running.
Using an environment variable i.e MYSQL_ROOT_PASSWORD within the file also allows me to create a database successfully but this will only work with a single database, I need to be able to use the mysql command to make queries, such as creating additional databases / assigning users etc.
This may be because I need to specify the host and port of the docker container but this still does not allow me to connect
RUN mysql -u root -ppassword -h 127.0.0.1 -P 3308 -e "CREATE DATABASE dbname"
Strangely, doing this also often crashes the container and puts it in a state where it will crash again on start-up every time that I try to restart it again.
I think the issue might be that in the service hasn't started within the container used to build your Dockerfile.
Try starting and configuring MySQL server within a single step. As a reference please check this file: https://github.com/dockerfile/mysql/blob/master/Dockerfile
Use below-given commands in your Dockerfile:
RUN service mysql restart && echo 'CREATE DATABASE db_name;' | mysql -uroot -
pYOUR_ROOT_PASSWORD
Had the very same problem: When starting the container and running a set of RUN instructions, or .sh or .sql scripts in /docker-entrypoint-initdb.d/ no connection to the database server could be established.
I found the solution by a comment of #wpalmer on the mysql-image:
The init scripts run by the entrypoint, internally, use the variable "${mysql[#]}" to call mysql (for example, when loading .sql files placed in the docker-entrypoint-initdb.d directory. Any .sh files which are processed by the entrypoint are included by "sourcing" them, meaning that variable is available for use by any .sh files which are run).
So what this means for you, instead of providing the plain mysql command with user, pass etc. as in
RUN mysql -u root -ppassword -e "CREATE DATABASE dbname"
use the placeholder instead:
RUN "${mysql[#]}" -e "CREATE DATABASE dbname"
You can try to build other image and run the create DB from there.
Example of docker-compose.yml
web:
build: web
links:
- "db:db.local"
entrypoint: entrypoint.sh
db:
build: db
environment:
MYSQL_ROOT_PASSWORD: password
command: mysqld
For entrypoint.sh you put something like this:
#!/bin/sh
#this is a hack to wait until the DB image is up and the port is open
until mysqladmin -u root -ppassword -e -h db.local ping; do
echo "$(date) - waiting for mysql"
sleep 3
done
if ! mysql -u root -ppassword -e -h db -e 'use dbname'; then
mysql -u root -ppassword -e "CREATE DATABASE dbname"
fi
exec "$#"
You can copy your queries as .sql file into "/docker-entrypoint-initdb.d" container directory. mysql will execute them after starting container
COPY ./init/db.sql /docker-entrypoint-initdb.d/
read official doc https://hub.docker.com/_/mysql?tab=description&page=1
Initializing a fresh instance