How to perform a MySQL Dump to a remote datastore - mariadb

I am trying to get the databases backed up from a local machine to a datastore. The local machine is a VM within VCenter and I need the DB to go onto one of it's datastores.
I know the command for MySQLDump is:
mysqldump -u (username) -p --all-databases > (backupfilename).sql.
What do I put in the second section to get it to connect and push the backup to the datastore as a file?
I tried the typical stuff you'd scp and rsync for but I'm not that versed in MariaDB, especially this version we have.

If you know login credentials and hostname (and whatever extra arguments you need) to connect to remote instance, you may replace > backupfilename.sql with | mysql -U user -P password -H host .... to pipe mysqldump directly or even use | tee somefile.sql | mysql .... to also have a local dump.
Mariadb vs mysql is almost inerchangeable, but dont bet your life on this assumption - check the outcome.
Nb: if remote server is only listening locally and not available via internet, you may use ssh port forwarding/socket forwarding to connect.

Related

How do I connect to a RDS MySQL instance from RStudio via a bastion host?

I would like to use RStudio for analysis of data on a MySQL instance. This is a AWS RDS MySql instance that is only accessible via a jump box / bastion host. I have the credentials necessary to connect to the jump box, and from the jump box to the RDS instance. What do I need to do be able to query this DB directly from within the RStudio console?
I can connect (using the Terminal tab in RStudio)to the jump box using:
ssh -p 22xx user#ip.add.re.ss
Then I can connect to RDS mysql using:
mysql -u username -p database -h hostname.us-east-1.rds.amazonaws.com
I can connect and do manual mysql commands from within RStudio terminal, but I don't seem to be able to do anything with the DB from the RStudio console.
Sorry for opening a 2yo thread, but for everyone dealing with this issue as I am - I found this thread and it looks like it works (connecting with MySQL via ssh from R Studio).
You should use something which is called port forwarding. Some details
are here
(https://help.ubuntu.com/community/SSH/OpenSSH/PortForwarding) For
example, say you wanted to connect from your laptop to
http://www.ubuntuforums.org using an SSH tunnel. You would use source
port number 8080 (the alternate http port), destination port 80 (the
http port), and destination server www.ubuntuforums.org. :
ssh -L 8080:www.ubuntuforums.org:80 Where should be
replaced by the name of your laptop.
This is done for whole computer so you dont need to do this from r
studio.
Offcourse you need to forward your port to 3036. But you need special
privilige on the server. Because on most hosting you can only connect
from localhost (for example from PHP)
Source: https://www.py4u.net/discuss/881859

Connecting to MariaDB + Bitnami's Multi-tier

I'm still new to AWS and this is my first attempt at working with MariaDB; I'm used to dealing with hosting providers that already have something like cPanel installed so please be nice. :)
I'm using Bitnami's WordPress Multi-Tier with Amazon RDS for MariaDB
Bitnami's documentation is usually quite good, but in this particular case I'm not finding anything. I've reached out to their support and the only reply I've received until now was something akin to: "use a WordPress plugin to make database exports" which is obviously isn't going to cut the mustard when it comes to importing.
What I want to accomplish:
Connect to my database
Export my database
Import (overwrite) a database
Essentially, I want to deploy my local WordPress to AWS...files are all good, but I'm lost when it comes to databases.
(NOTE: I want to get out of the habit of relying on phpMyAdmin and, ideally, don't want to have to go through installing it, etc)
I started here: [Connecting to a DB Instance Running the MariaDB Database Engine][2]
After SSH'ing in I've tried:
Command: mysql
Outputs: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/opt/bitnami/mariadb/tmp/mysql.sock' (2)
Command: mysql -h MY-DB-INSTANCE.us-east-1.rds.amazonaws.com -P 3306 -u bitnami
Outputs: Access denied for user 'bitnami'#'10.0.4.110' (using password: NO)
EDIT: I've split this thread into a separate one for other issues that I ran into.
Presumably your MySQL user bitnami actually has a password, so you may try this:
mysql -h MY-DB-INSTNACE.us-east-1.rds.amazonaws.com -P 3306 -u bitnami -p
^^^ add this
Your shell should prompt you for the password.
Beyond this, you need to make sure that you have opened your RDS instance to the IP from which you are trying to connect. You could open it to all IPs, but it is better practice to just open it to your dev machine, as well as the production machines which would be hitting the database. If you don't do this step, you also would not be able to connect.
Edit: If your user bitnami does not yet exist, then you may have to login as root and configure. Or perhaps you would have to reset the password if forgotten. You should always write down the admin credentials, as a last resort means of accessing your RDS instance.

R SSH Tunnel MySQL

I'm looking for a way that will allow me to use an SSH Tunnel to connect to a MySQL Server (as opposed to a file) within R; I'm assuming it'll require a combination of RCurl and RODBC, but I can't seem to get it to work properly.
I came across this post and this post that talk about utilizing SSH to connect to specific files or tables, but I'm hoping to use it as part of a Shiny app that will execute different SQL queries based on input from the user, which would require connecting into the server as opposed to specific files.
I'm assuming the code would look something along these lines x = scp("remote.ssh.host.com", "/home/dir/file.txt", "My.SCP.Passphrase", user="username"), but would I replace the "/home/dir/file.txt" piece with an odbcConnect() statement or replace it with the port number for the specific database I want to access?
Edit: The line I use for a regular odbcConnect() is odbcConnect(dsn, uid = "userid", pwd = "password"). Part of the problem is, I am developing it on Windows, but it will be deployed to a Linux server (handled be someone else) so I'm struggling to figure out what exactly will need to be used in my server.R code for connecting to the database.
Okay, so to test this on Windows, either grab Cygwin, or install OpenSSH so you can run ssh from the command line in Windows, like you would do in Linux.
Once you have ssh running on your Windows box, then try first making a tunnel through SSH. Run this from the command line:
ssh -f <server_user>#<server_ip> -L <unused_local_port>:localhost:<database_remote_port> -N
Obviously, replace everything in '<>' with the appropriate information. It will ask for the password, and remember that this isn't the database password, but the password to the server itself. Notably, the server_ip doesn't have to be the server with the database on it, just any server that is inside the proper subnet and that runs an SSH server, which is pretty much all Linux machines.
Now, setup an ODBC connection, except make the IP localhost, and the port unused_local_port. Now, try connecting to your new ODBC connection in R. If this works you're halfway there.
The next problem is the password, because you will have to enter a password to connect via SSH, but in R you won't be able to input it after a simple system command. So you have to setup some a public/private rsa key pair. Notably, this will make it so that anyone with access to your user/pass on your Windows box will now have automatic access to your server, so be careful. First, generate a SSH key:
ssh-keygen -t rsa
Don't make a passphrase, and save it in the default location. Now, create the directory for your public key on the remote host, and drop your public key in there.
# This creates a directory on the other machine if it wasn't already there. (Type in your password on the remote machine)
ssh <server_user>#<server_ip> mkdir -p .ssh
# This adds your public key to the list of accepted ones:
cat ~/.ssh/id_rsa.pub | ssh <server_user>#<server_ip> 'cat >> .ssh/authorized_keys'
Now try creating your tunnel again from the command line:
ssh -f <server_user>#<server_ip> -L <unused_local_port>:localhost:<database_remote_port> -N
If it doesn't ask you for the password, you have succeeded in creating your keypair. Now you are ready to run your ssh command from the command line. But before you do that, try and kill your ssh command, so you can make sure that R is actually creating the tunnel, and you aren't just reusing an old one. You can do it through Windows Task Manager (Ctrl+Alt+Esc), and just right click and End Process the ssh.exe.
So, just run:
system('ssh -f <server_user>#<server_ip> -L <unused_local_port>:localhost:<database_remote_port> -N')
And then connect to your new tunneled ODBC connection.

How do I refer to my local computer for scp'ing when logged into remote?

This must be a really simple question, but I am trying to move a file from a remote server to my local computer, while logged into the remote (via ssh).
All of the guides say to just use
scp name#remote:/path/to/file local/path/to/file
But as far as I can understand, that would be what I would use from my local machine. From the remote machine, I assume that I want to use something like
scp /path/to/file my_local_computer:/local/path/to/file
but (if that's even correct) how do I know what to put in for my_local_computer?
Thanks!
You can automatically figure out where you're logged in from by checking the environment variables SSH_CONNECTION and/or SSH_CLIENT. SSH_CONNECTION for example shows the client address, the outgoing port on the client, the server address and the incoming port on the server. See section ENVIRONMENT in man ssh
So, if you want to copy a file from the server to the client from which you're logged in from, the following (which infers the client ip by taking the first part of SSH_CONNECTION) should work:
scp /path/to/file $(echo $SSH_CONNECTION | cut -f 1 -d ' '):/local/path/to/file
Andreas
You are on the right track! The man page for scp should tell you how to do what you want: http://linux.die.net/man/1/scp
If you are having trouble understanding the man page, then I will attempt to instruct you:
If you want to push a file from your local machine to a remote machine
scp /path/to/local/file testuser#remote-host:/path/to/where/you/want/to/put/file
If you want to pull a file from a remote machine to your local machine
scp testuser#remote-host:/path/to/file/you/want/to/pull /path/on/local/machine/to/place/file
If you are logged into a remote machine and want to push a file to your local machine (assuming you have the ability to scp to the local machine in the first place)
scp /path/on/remote/machine/to/file testuser#local-host:/path/on/local/machine/to/put/file
Now, to determine what your local-host address is, you can check the IP address of your local machine or if your local machine has been provided a DNS entry, you could use it.
I.E., scp ~/myfile testuser#192.168.1.10:/home/testuser/myfile OR scp ~/myfile testuser#my-host:/home/testuser/myfile
For the DNS entry, provided you are on a correctly configured network, you would not need a fully qualified domain. Otherwise, you would need to do something like testuser#my-host.example.com:/home/testuser/myfile
Maybe you can build a solution around this:
who | grep $USER
When run on the remote computer, it should give a hint where you connected form.

Basic CouchDB Local-to-Remote Replication

I am playing around with CouchDB Replication and I'm wondering how to copy a local database of name "myDatabase" to a remote database on www.mySite.com that requires ssh access, and rename it to "myRemoteDatabase". How do I do that?
I am able to login to both the local and remote servers and run:
curl -X GET http://localhost:5984/
...and it will show me the result, but when I am on my local computer and I run:
curl -X GET http://my.ip.address:5984/
... or even
curl -X GET http://USER:PASSWORD#my.ip.address:5984/
I get:
curl: (7) couldn't connect to host
What am I missing in this picture?
Given the data you supplied, it can only be one thing.
You have configured CouchDB to listen on the localhost interface and no other. I'm unfamiliar with how CouchDB is configured, but it will be some option about binding to 127.0.0.1 or (in an IPv6 world) ::1. Many configurations do this by default as it is fairly safe. It will not allow access by any host other than localhost.
One possibility that I was thinking about is that the two machines have firewalls up that are not allowing connections from the outside world to port 5984. But almost no firewall protects a computer from access from itself. They can, but almost never do.
sudo vi /etc/couchdb/default.ini
Under [httpd], change
bind_address = 127.0.0.1
to that server's IP, then:
sudo service couchdb restart

Resources