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?
Related
I'm trying to download a sql dump of my MySQL database in RDS through my local terminal. I have security groups setup so that only my ec2 instances can access this RDS database. Is there a way to run the mysqldump command all in one line w/o having to ssh into the Ec2 box > run command there > then scp the dump out to my local machine?
Command I normally run inside ec2 instance is
mysqldump DBNAME -h RDSURL -u USERNAME -p -P PORT > ~/dump.sql
I ended up solving this by setting up port forwarding per Mark B's advice. In one terminal window I did
ssh -N -L 1234:RDSURL:PORT EC2USER#EC2URL -i LOCALPEMKEY
Then in 2nd terminal window I did
mysqldump DBNAME -h 127.0.0.1 -u USERNAME -p -P 1234 > ~/dump.sql
Worked like a charm. Thanks for pointing me in the right direction Mark B.
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.
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
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"
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