Unable to connect to mysql docker container - mysql

I have a Dockerfile that I am working on that pulls Mysql 5.6 and configures it (mostly with a bash and sql script). I am able to build and run it but when I try to connect to the database in the container I always get:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
I have tried accessing the mysql database by using:
mysql -u root -p
mysql -u root -h 127.0.0.1 -p
I have tried everything I could think of and looked up articles on the internet but nothing works. Can someone tell me why? Here is my Dockerfile and bash script respectively:
FROM mysql:5.6
MAINTAINER Ryan K.
USER root
ADD mysqlAddUser.sh /tmp/
CMD ["/tmp/mysqlAddUser.sh"]
ADD foo.sql /docker-entrypoint-initdb.d/foo.sql
EXPOSE 3306
## Starting mysqld and running Database Scripts
CMD ["/usr/bin/mysqld_safe"]
Bash script:
#!/bin/bash
DATABASE_PASSWORD=test
/usr/bin/mysqld_safe &
mysqladmin --login-path=local -uroot -p"$DATABASE_PASSWORD"
mysqladmin password "$DATABASE_PASSWORD"
mysql -uroot -p"$DATABASE_PASSWORD" -e "UPDATE mysql.user SET Password=PASSWORD('$DATABASE_PASSWORD') WHERE User='root'"
mysql -uroot -p"$DATABASE_PASSWORD" -e "DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1')"
mysql -uroot -p"$DATABASE_PASSWORD" -e "DELETE FROM mysql.user WHERE User=''"
mysql -uroot -p"$DATABASE_PASSWORD" -e "DELETE FROM mysql.db WHERE Db='test' OR Db='test\_%'"
mysql -uroot -p"$DATABASE_PASSWORD" -e "FLUSH PRIVILEGES"
DB_ROOT_PASS=TEST
DB=portal
mysql --login-path=local -uroot -p"$DB_ROOT_PASS"
mysql -uroot -p"$DATABASE_PASWORD" -e "CREATE DATABASE portal";
mysql -uroot -p"$DATABASE_PASSWORD" -e "CREATE USER portaluser#'localhost' IDENTIFIED BY 'testing'";
mysql -uroot -p"$DATABASE_PASSWORD" -e "GRANT ALL PRIVILEGES ON portal.* TO portaluser#'localhost'";
mysql -uroot -p"$DATABASE_PASSWORD" -e "FLUSH PRIVILEGES";
mysql -uroot -p"$DATABASE_PASSWORD" $DB < /tmp/foo.sql

Was struggling with the same issue with mysql in a docker container. Sometimes I could connect with the mysql client, but more often not. Switched to mariadb and had the same problem.
What seems to have fixed it for me is to add some sleep commands in my scripts that create, start and destroy the docker containers. After commands like 'docker run' and 'docker stop' I added 'sleep 10' and that seems to help.

Related

Duplicate / clone mysql database on RDS from a shell script on EC2

I have a database called 'X' on RDS.
I have an EC2 instance where I wish to run a .sh script that would do following:
Connect to the said RDS Instance
Create database 'Y' (drop if exists) on same RDS instance
Copy all data from 'x' to 'Y'
I have installed mysql-client on EC2 and tried out following code:
#!/bin/bash
PRODUCTION_DB=X
COPY_DB=Y
ERROR=~/mysql_dump_error.log
mysql -h xxxx.rds.amazonaws.com -P 3306 -u xxxx --password=xxxx -e "drop database if exists $COPY_DB;" --force && mysql -h xxxx.rds.amazonaws.com -P 3306 -u xxxx --password=xxxx -e "create database $COPY_DB;" && mysqldump --force --log-error=$ERROR -h xxxx.rds.amazonaws.com -P 3306 -u xxxx --password=xxxx $PRODUCTION_DB | mysql -h xxxx.rds.amazonaws.com -P 3306 -u xxxx --password=xxxx $COPY_DB
Thanks to https://www.harecoded.com/copycloneduplicate-mysql-database-script-2184438/
Dropping & creating new DB works fine but during mysql dump, I get following error:
Access denied; you need (at least one of) the SUPER privilege(s) for this operation
How do I go about adding privilege to my use? Is it the right thing to do? Or I should find some another way?

mysql_secure_installation executes, but does not change tables

I have a problem that after clean latest mysql or mariadb (10.0.34-MariaDB-0ubuntu0.16.04.1) install on clean Ubuntu 16.04 (on separate VMs), mysql_secure_installation runs happily but does not change a thing! Exactly the same result (to be precise no result whatsoever) if I run SQL:
sudo mysqladmin -u root password "my_pass"
sudo mysql -u root -p"my_pass" -e "UPDATE mysql.user SET Password=PASSWORD('my_pass') WHERE User='root'"
sudo mysql -u root -p"my_pass" -e "DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1')"
sudo mysql -u root -p"my_pass" -e "DELETE FROM mysql.user WHERE User=''"
sudo mysql -u root -p"my_pass" -e "DELETE FROM mysql.db WHERE Db='test' OR Db='test\_%'"
sudo mysql -u root -p"my_pass" -e "FLUSH PRIVILEGES"
No error, it runs smoothly with no output. But it has no impact on user table. like I never run it.
It is a mystery for me. Obviously I'm missing something very basic, but can't see what. I'd appreciate help.
It printed those lines out as suggestions of what should be done.
It was not so rash as to do them for you; that could cause grief if there is, for example some reason to fix, rather than DELETE one of those 'users'.

Run MySQL on different port with different config

I'm trying to run another instance of MySQL on separate port with different config.
Attempt 1
docker run --name dbname -v /home/custom-mysql-configs/dbname-config-folder:/etc/mysql -p 0.0.0.0:3312:3306 -e MYSQL_ROOT_PASSWORD=mysupersecretpassword -d mysql:5.5
When I run SHOW VARIABLES; in mysql -u root -p --host=127.0.0.1 --port=3312 I get the same variables as mysql -u root -p --host=127.0.0.1 --port=3306 that are set in /etc/mysql/my.cnf
Attempt 2
docker run --name dbname -v /home/custom-mysql-configs/dbname-config-folder:/etc/mysql/conf.d -p 0.0.0.0:3312:3306 -e MYSQL_ROOT_PASSWORD=mysupersecretpassword -d mysql:5.5
Then when I ran
mysql -u root -p --host=127.0.0.1 --port=3306
I got the following error:
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading
initial communication packet', system error: 0
What am I doing wrong and how should I run the new MySQL instance?
If you're running your new container with:
$ docker run --name dbname -v /home/custom-mysql-configs/dbname-config-folder:/etc/mysql/conf.d -p 0.0.0.0:3312:3306 -e MYSQL_ROOT_PASSWORD=mysupersecretpassword -d mysql:5.5
You should be connecting to it through:
$ mysql -u root -p --host=127.0.0.1 --port=3312
Your command was connecting to your original container, on port 3306.
I'd also suggest showing some of your MySQL conf.d files, as it might help further.

mysql syntax error near expected new line in terminal mac

I am running the following command as given on web to connect the mysql data base but it gives syntax error new line expected here is the command i am entering.
mysql -<hivelettest.c0e9graawyhr.us-west-2.rds.amazonaws.com -p 3306 -u <user> -p <pass>
The <> parts of the command were given to show where the username and password should go. They shouldn't be in the command:
mysql -hmydbserver.co.uk -P3306 -u username -pmypassword
Try this, and make sure that there is no space between -u or -p. And if you are using PORT you need to write it differently. The password parameter must then be --password=
mysql -hmydbserver.co.uk -P 3306 -uUsername --password=yourpassword
Since 3306 is the standard one you could leave it out, then you can write it like this.
mysql -hmydbserver.co.uk -uUsername -pYourpassword
If you want to pass with a command, do it like this.
mysql -hmydbserver.co.uk -P 3306 -uUsername --password=yourpassword nameofdatabase
-e "SELECT * FROM tablename"

Terminal mysql queries

I'm looking to use terminal to execute mysql queries. I currently connect to a sql db via the sql workbench but would like to do this via terminal. Is this possible?
I've installed mysql via homebrew
when I hit mysql in terminal it says command not found, maybe I need to do something else besides the homebrew setup?
Go to the directory:
mysql/bin
To execute the query from command line:
mysql -u [username] -p [dbname] -e [query]
example:
mysql -u root -p database -e "select * from user"
mysql --help
You can do also:
cat dump.sql | mysql -u user -p password database
Or:
echo "select * from abc;" | mysql -u user -p password database