Dump mysql databases from mysql console? (Impossible the normal way) - mysql

For a number of reasons describe here:
STRANGE Situation -> Mysql ERROR 2002 (HY000)
I cannot use mysqldump. Is there another program or way to dump dbs through the mysql console? The only way I can get into the console is running
mysql -h 127.0.0.1 -P 3306 -u root -p
(due to problems stated above - or if I re-install mysql, which could be risky)
Hope someone help us save our data!

for the future, the SOLUTION:
mysql -h 127.0.0.1 -P 3306 -u root -p -e "show databases;" | tr -d "| "
mysql -h 127.0.0.1 -P 3306 -u root -p -e "show databases;" \
| tr -d "| " \
| egrep -v "(Database|mysql|information_schema)"
for db in \
mysql -h 127.0.0.1 -P 3306 -u root -p -e "show databases;" \
| tr -d "| " \
| egrep -v "(Database|mysql|information_schema)"; \
do mysqldump -h 127.0.0.1 -P 3306 -u root -p --opt --routines --databases $db "$db.sql"; done
I received this error in one of the databasese, but it hasn't affected my production ones, so Im cool with it.
mysqldump: Got error: 1142: SELECT,LOCK TABL command denied to user 'root'#'localhost' for table 'cond_instances' when using LOCK TABLES
Credits to:
http://vitobotta.com/smarter-faster-backups-restores-mysql-databases-with-mysqldump/

Related

how do I parameterize mysql login when using .my.cnf

I have the following shell script that relies on $HOME/.my.cnf to supply the password. The two calls to mysql are equivalent, but the second one prompts for a password whereas the first does not. I am trying to write some shell scripts with the connectivity details parameterized except for password. I tried using #!/bin/sh. There is no whitespace after any of the parameters. Any suggestions?
$ cat processlist-state.sh
#!/bin/bash
export HOSTNAME=mysql-us-east-1-123456789.abcdefghijklm.us-east-1.rds.amazonaws.com
export PORT=3306
export USERNAME=master_user
mysql -h mysql-us-east-1-123456789.abcdefghijklm.us-east-1.rds.amazonaws.com -P 3306 -u master_user -e "show processlist"
mysql -h ${HOSTNAME} -p ${PORT} -u ${USERNAME} -e "show processlist"
$ cat $HOME/.my.cnf
[client]
password=password
-p in the second call forces the password prompt.
-p = ask for password
-P = connection port
-p{PASSWORD} = use {PASSWORD} from commandline as password

wait for mysql to come online, do commands, and shut it down gracefully in bash

I'm working on a bash script that turns mysql on, performs some actions and than shuts it down.
I'm afraid that /usr/bin/mysqladmin ping -h 127.0.0.1 --silent is not a safe way to go about it. How do I make sure that mysql is ON or OFF so I can proceed?
p.s. this is docker container, so I don't have service.
if [[ ! -f /var/lib/mysql/status.secured ]]; then
echo "Starting MariaDB... to secure it!"
/usr/bin/mysqld_safe
while ! /usr/bin/mysqladmin ping -h 127.0.0.1 --silent; do
sleep 1
done
mysqladmin -u root password "$DATABASE_PASS"
mysql -u root -p"$DATABASE_PASS" -e "UPDATE mysql.user SET Password=PASSWORD('$DATABASE_PASS') WHERE User='root'"
mysql -u root -p"$DATABASE_PASS" -e "DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1')"
mysql -u root -p"$DATABASE_PASS" -e "DELETE FROM mysql.user WHERE User=''"
mysql -u root -p"$DATABASE_PASS" -e "DELETE FROM mysql.db WHERE Db='test' OR Db='test\_%'"
mysql -u root -p"$DATABASE_PASS" -e "FLUSH PRIVILEGES"
touch /var/lib/mysql/status.secured
mysqladmin -u root -p"$DATABASE_PASS" -h 127.0.0.1 --protocol=tcp shutdown
while /usr/bin/mysqladmin ping -h 127.0.0.1 --silent; do
sleep 1
done
fi
I'm specially worried for shutdown part, maybe its better to check if PID exists? or grep ps aux ?
I can suggest two options to try:
Use mysqladmin and check the exit status
mysqladmin -h server_ip -u username -p passsword processlist
if [ $? -ne 0 ] then; ... system down
Use grep and check for process mysql
STATUS=$(grep mysql | wc -l);
if [ "STATUS" -ne 1 ] then; ... system down

Unable to connect to mysql docker container

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.

How do i import into mysql daily via cron or ssh

I want to write a cron job to INSERT INTO 6 tables. I know that :
The tables will always exist and will be empty, awaiting records
I will have 6 .sql dump files On the server which will be ready to be imported.
The format of the files are exactly as created by the phpMyAdmin export tool
i.e. all already have a single INSERT INTO tablename line and multiple parenthisised value lines.
All have foreign-key constraints set to off before the insert statement and toggled on again after the inserts.
i wanted to do all from within PHP but on a shared hosting the exec() func is disabled and so is the FILES priviledge so my options are dwindling.
I'm looking for a way to do the above from cPanel cron or SSH tools in cPanel. CRON would be my first choice since i'm unfamiliar with SSH.
Do i run this line 6 times (for each file) or can i consolidate it into one cron or ssh command? p.s. also do i use absolute or relative paths to the .sql dump files
mysql -h localhost -u myusername -p mypassword mycatalogdb < 'sqlcronfiles/dump-1.sql'
mysql -h localhost -u myusername -p mypassword mycatalogdb < 'sqlcronfiles/dump-2.sql'
mysql -h localhost -u myusername -p mypassword mycatalogdb < 'sqlcronfiles/dump-3.sql'
mysql -h ... etc ...
thanks
You can create a task.sh file located at /home/youruser/ and inside you can put the following:
#!/bin/bash
mysql -h localhost -u myusername -p mypassword mycatalogdb < 'sqlcronfiles/dump-1.sql'
mysql -h localhost -u myusername -p mypassword mycatalogdb < 'sqlcronfiles/dump-2.sql'
mysql -h localhost -u myusername -p mypassword mycatalogdb < 'sqlcronfiles/dump-3.sql'
mysql -h localhost -u myusername -p mypassword mycatalogdb < 'sqlcronfiles/dump-4.sql'
mysql -h localhost -u myusername -p mypassword mycatalogdb < 'sqlcronfiles/dump-5.sql'
mysql -h localhost -u myusername -p mypassword mycatalogdb < 'sqlcronfiles/dump-6.sql'
Then, to execute this cron, from cPanel, you create a Cronjob, configure the time as needed and the command it should execute is:
/home/youruser/task.sh
And you are done.
I hope it works for you

Database master-slave replication error

I am trying to replicate my mysql database using the master slave replication. On the slave machine, when i try to run this query, i get stuck.
mysqldump -h 10.124.2.34 bank --password='' --user='root' | mysql bank --password='' --user='root'
Any idea what I am doing wrong?
Try this :
Total DB dump :
mysqldump -uroot -h -p pwd -P db_name | mysql -uroot -p pwd -P db_name
Only Table copy
mysqldump -uroot -h -p pwd -P db_name | mysql -uroot -p pwd -P db_name
It's working for me.