Mysql Dump RDS Trough Ec2 Instance - mysql

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.

Related

mysqldump via cmd syntax for dumping remote database

I have Mysql installed on my local PC and want to use mysqldump on my local pc to dump a remote database. So I open CMD and run the following command:
mysqldump -P 3306 -h 12.43.33.43 -u admin -p myDatabase > mydb.sql
So in the next line it should ask for my password? but instead i get the response 'Access is denied'. Am i missing something here?
ps.
If I try the following:
mysqldump -P 3306 -h 12.43.33.43 -u admin -p myDatabase
it will successfully ask for my password and start printing the dump in cmd, but this is no good as i need the response saved in a file.
I only wanted to know if you could connect.
The command is
mysqldump -P 3306 -h 12.43.33.43 -u admin -p --databases myDatabase --result-file=mydb.sql
As the connection is not secure, you send your credentials like a postcard, so that every one can read it.
Besides that having your server exposed to the internet, is very dangerous and can come to a disaster, so reconsider your security strategy and allow only access via ssh.

How to do a `mysqldump` from a remote server with the terminal and save locally

How can I do a mysqldump from a server by using the Terminal?
I did the following:
First of all I logged in:
ssh root#123.456.78.90 // then I enter my password
then I do:
mysql->show databases;
after that:
use the_database;
then I do:
mysqldump -u myusername -p the_database > db_backup.sql
after that, obviously, nothing happens and I do not get an error. So my question is, where does it save the backup and how can I get a the mysqldump on my local machine?
There are multiple ways of how and where to backup, but most probably you would not like to store your backup in the same server where you are running the database.
If you have ssh access to the server you could create an ssh-tunnel, the most basic example of this could be:
$ ssh -L 3307:localhost:3306 root#123.456.78.90
What this will do is to open port 3307 locally (just in case you already are using 3306) and forward it to localhost:3306 going through 123.456.78.90
Then you could do something like:
$ mysqldump -h localhost -P 3307 -u myusername -p the_database > db_backup.sql
To know your current path, run pwd it will print your current directory and in where the db_backup.sql will be created in case you don't specify and absolute path.
Without using ssh you could also try to connect directly by just specifying the host:
$ mysqldump -h 123.456.78.90 -u myusername -p the_database > db_backup.sql
In case the port is open probably you will need just to modify/grant the user permissions to allow your IP to access:
mysql> GRANT ALL ON the_database.* TO your_user#'123.456.78.90' IDENTIFIED BY 'secret';
Connect to your server using ssh command.
run below code
mysqldump -u userName -p databaseName > /path/to/backup.sql
the location path must be on same server

mysqldump via SSH to local computer

I have an SSH access to production server of the Rails app.
I want to make a mysqldump of production database to my Mac. Please help me to achieve this.
Direct method to dump mysql data from remote server to your local computer is:
ssh root#ipaddress "mysqldump -u dbuser -p dbname | gzip -9" > dblocal.sql.gz
Or
ssh -l root ipaddress "mysqldump -u dbuser -p dbname | gzip -9" > dblocal.sql.gz
Both command does the same work.
If you have password for ssh and database access there will two prompt for password or if you have no password for ssh then you will be asked to enter you database password.
Similarly, if you are using key from aws or cloud other service you can incorporate the key in the command as:
ssh -i key.pem root#ipaddress "mysqldump -u dbuser -p dbname | gzip -9" > dblocal.sql.gz
Connect to server via ssh: ssh remote_username#remote_host
Go to 'current' folder
Make a dump: mysqldump -u username -ppassword -h host database > dump.sql
Disconnect from server
Copy a dump.sql file to local computer: scp remote_username#remote_host:/path/to/dump.sql /Users/YourName/Documents/dump.sql
Connect to server via ssh again and go to 'current' folder
Remove dump.sql file: rm dump.sql
I couldn't get the other ones to work.
This is the solution I found for linux:
ssh username#ipadress "mysqldump -u USERNAME -pPASSWORD DB_NAME " > ~/dump.sql
This will prompt you to enter a password for your ssh server, and then dumps the database to the specified location on the SSH server.

Mysql Database Backup from Remote System

I am trying to get database backup from remote system, please anyone help me to get backup from remote system. Here is the query which I executed, but its giving error.
mysqldump --host <host or IP address> -P <port Number> -u <username> -p<password> databaseName > c.sql;
remove ; from your syntax and execute below command-
mysqldump -h<IP_address> -u <username> -p<password> databaseName > backup.sql
If you want to take backup in your working directory else give a path. I have removed port option here as assuming that it will be running on default port 3306 else add in command.
Make sure your user should have permission on db server.

how to run command "mysqladmin flush-hosts" on Amazon RDS database Server instance?

I got a database server failure, says host is blocked because of many connection errors. It ask me to unblock with 'mysqladmin flush-hosts'
how and where should I run this command to our amazon rds database server?
thank you
For normal MySQL, just connect as the 'root' administrative super user, and issue the command:
FLUSH HOSTS
Even in the case of too many connections, MySQL should be keeping a connection in reserve so that a super user can connect.
The mysqladmin client generally connects as root anyway and issues the above SQL.
Login to any other EC2 instance you have that has access to the RDS instance in question and has mysqladmin installed and run
mysqladmin -h <RDS ENDPOINT URL> -P 3306 -u <USER> -p flush-hosts
you will be prompted for your password
When an Amazon RDS instance is blocked because the value of max_connect_errors has been exceeded, you cannot use the host that generated the connection errors to issue the "flush hosts" command, as the MySQL Server running on the instance is at that point blocking connections from that host.
You therefore need to issue the "flush hosts" command from another EC2 instance or remote server that has access to that RDS instance.
mysqladmin -h [YOUR RDS END POINT URL] -P 3306 -u [DB USER] -p flush-hosts
If this involved launching a new instance, or creating/modifying security groups to permit external access, it may be quicker to simply login to the RDS user interface and reboot the RDS instance that is blocked.
I fixed this error on my RDS instance by rebooting it from the AWS management console. HTH
[edit: lol downvotes]
On Amazon RDS FLUSH HOSTS; can be executed from default user ("Master Username" in RDS info), and it helps.
Since the hosts is blocked. try connect it from other host and execute the mysqladmin flush-hosts command.
mysqladmin -h <RDS ENDPOINT URL> -P <PORT> -u <USER> -p flush-hosts
You will have to connect your RDS through a computer which as mysql installed on it
I used one of my hosting VPS using SSH
After i was logged in my VPS ( i used putty ) It was simple, in the prompt i entered the following command:
mysqladmin -h [YOUR RDS END POINT URL] -P 3306 -u [DB USER] -p flush-hosts
You can restart the database on RDS Admin.
You can flush hosts local MySQL using following command:
mysqladmin -u [username] -p flush-hosts
**** [MySQL password]
or
mysqladmin flush-hosts -u [username] -p
**** [MySQL password]
Though Amazon RDS database server is on network then use the following command as like as flush network MySQL server:
mysqladmin -h <RDS ENDPOINT URL> -P <PORT> -u <USER> -p flush-hosts
mysqladmin -h [YOUR RDS END POINT URL] -P 3306 -u [DB USER] -p flush-hosts
In additional suggestion
you can permanently solve blocked of many connections error problem by editing
my.ini file[Mysql configuration file]
change variables max_connections = 10000;
or
login into MySQL using command line -
mysql -u [username] -p
**** [MySQL password]
put the below command into MySQL window
SET GLOBAL max_connect_errors=10000;
set global max_connections = 200;
check veritable using command-
show variables like "max_connections";
show variables like "max_connect_errors";
got this error today on a customer rds while they were using Heidi Sql client.
We simply used 'mysqlroot' on the ec2 that talks to the rds in question to connect, followed by issuing the 'flush hosts;' cmd.