How do I remotely access an SQL database? - mysql

This question has been edited for clarity...
Say I have a MySQL database, what are my options for performing queries from a remote computer and having access to the resulting data.
A convoluted solution I can think of is to have a web service that receives a query from a request and responds with an xml file of the results, but this seems like a bad way of doing it. What other options do I have of accessing a database remotely?

So, if you are going to write a Java application to connect to a MySQL database you have a few options:
use JDBC directly as described here: https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-usagenotes-basic.html
Hibernate (http://docs.jboss.org/hibernate/orm/current/quickstart/html_single/) but that may be a little bit too complicated
Spring Data, here is a nice tutorial: https://spring.io/guides/gs/accessing-data-mysql/
JDBI (http://jdbi.org/) extremely simple ;)

There are 2 ways to do it.
1)
mysql -u {username} -p{password} -h {remote server ip} {DB name}
2)
In Windows :
-- Login to another system using putty or rdesktop
-- Access mysql database after login
In Linux:
-- ssh to another system using "ssh user#ip"
-- Access mysql database after login
You can follow this link for more details:
https://support.rackspace.com/how-to/mysql-connect-to-your-database-remotely/
https://dev.mysql.com/doc/refman/5.7/en/connecting.html

If you are using windows machine you can use MySQL Workbench and then you can just provide the details of the server where MySQL is hosted.
You can also use putty to do SSH if MySQL server is hosted in Linux.
Steps:
List item
SSH to the Server where MySQL server is hosted.
mysql -u root -pyourpassword.

Related

MySql workbench not responding w local server

I have been using MySql Workbench to connect to a local MySql server and to remote MySql Servers. The last couple of days the Workbench freezes when I connect to the local version. It still works for the remote servers.
I am using workbench 6.3.4 and then tried 6.2.5 just to see if it is a version issue.
I also can access the local MySql instance using HeidiSQL.
Any recommendations on what to look for?
Update
The advice to recreate the connection solved the issue. I just created a new one and it works fine.
A couple of things you can try:
Try restarting the sql server (remember, workbench is just a utility for accessing a db, it does not host the database for you)
See if you can access mysql on the command prompt / terminal, the command for mac & windows
(once you navigate to the mysql folder, try the command:)
mysql -u root
(if you have a password-protected account, the command will be
mysql -u someuser -p
If the above doesn't work, mysql is either offline or denying you access (depending on response), and you'll need to setup the server / assign privileges.
If all else fails, it may help to try another version of SQL / Workbench (for a easy-to-use sql sever setup, I'd recommend WAMP or MAMP, assuming you want a HTTP server bundled with it)
Additionally, it may help to recreate the local connection in workbench (of if your getting regular crashes, reinstall & update workbench). Good luck with it.

How to tunnel for MySQL using vmc in cloudfoundry?

I'm just starting out with CloudFoundry and I understand that the only way to be able to execute a sql dump against a db is to
Create MySQL service
Bind Service
Tunnel to the service
But I'm not getting any luck on the last part. I followed the directions from cloud foundry forum discussion
I select option 1. None But I just don't know how run
mysql --protocol=TCP --host=localhost --port=10000 --user=ub2sCBQ0mGsVZ --password=pRXana7vEOX0C df83386088ffc4f8d8cf288791d22fb99 < sample.sql
from command line (Windows) or from SQLyog.
The other part of the question is of course once I am able to create the database and the tables and insert data into them. How does my webapp need to connect to the database?
On one hand I think I know the answer as described here
But I can't believe that this is the only way. This is damn difficult with Spring because I would have to read the env in before the entire Spring/Hibernate machinery starts creating the needed beans. Is there an example of less complex way to do this ?
if you are using Windows I would suggest using MySQL workbench to connect using the credentials provided when opening the tunnel using VMC. You can download it from the MySQL website here;
https://www.mysql.com/products/workbench/
When deploying a Spring application that uses a MySQL service you can have it automatically configure the database connection in your app. See the following page for details;
http://docs.cloudfoundry.com/frameworks/java/spring/spring.html#determining-whether-your-application-can-be-auto-configured

Update my remote MySQL database with my local MySQL database

I have a local Perl script that does a lot of parsing of web pages and then successfully updates my local MySQL database (WAMP server). I now want to send this local data to my remote server, but remotely connecting to my database isn't allowed with my hosting company. Unfortunately I never thought of that problem.
So, I now need to find an automated way to update my remote server (every 15mins). I mistakenly thought I could just edit my Perl script with the details of the remote server.
I am aware that I could use CGI or PHP to do the parsing on the server, but I really want to keep the parsing local for now.
Summary:
Local MySQL database -> remote MySQL database every 15mins ??
Any ideas what I can do?
Thanks :-)
if replication is not an option but you can still establish an ssh connection from local box to remote box, then
run mysqldump to export data into a file http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_where
scp file to remote box
mysql -u username -p password database_name < dumpfile.sql
If your server does not accept connections to mysql remotely you can create a ssh tunnel. Then you can apply the replication solution proposed by matcheek.
Here is a hint: http://realprogrammers.com/how_to/set_up_an_ssh_tunnel_with_putty.html
Based on the responses I've received, I think the answer to my original question is to stop using a cheap shared hosting company (no remote access to server, no cron jobs, etc) and start using a VPS hosting company. That will give me the freedom to remotely connect to my server, etc.
Thanks again to those who replied.
From how you described the problem replication seems to be the way to go
http://dev.mysql.com/doc/refman/4.1/en/replication-howto.html
Using a cron job could be another option. It would read file from your local machine and import data in the remote box.
I suggest the follwing:
On every local run, write the SQL statements (sans SELECT),
that you run against your copy of the DB also into a file
On your WAMP server create a small PHP script, gives back the oldest script from the first step (soem auth ofcourse)
On your remote server run a cronjob, that gets this from your local server and runs the SQL against the DB, then acknowledges it
On acknowledgement on your WAMP server, drop the file and give back the next one.
While this seems complicated, it allows for a restart after connectivity loss - something that I consider imposrtant.

Execute Shell command over MySql on remote host

Is it possible to login into a remote mysql machine and execute commands using 'system' on the remote machine.
I can log into the remote machine, but commands using: 'system' are executed at my local machine.
Thanks indeed!
I using mysql to connect from 'Host1' to 'Host2' using the command
mysql -uUsername -p data_base_name -h Host2
When I execute
'system hostname'
after I'm connected i get.
'Host1'
I cannot log into my remote host using ssh. I don't know why. I need to do some log analysis and the only option I have is to connect to that machine using mysql. I can connect to that machine! –
As far as I know, this is definitely not possible. It's far beyond the scope of mySQL, and there would be immense security implications if it were.
I don't think there is an alternative to getting SSH (or some other service that might help) running again.
Consider doing a select into outfile and writing script code into a place that will be executed on the server. For example, if mysql is running as root on the server, you be able to add something to the /etc/rc2.d which will get executed on the server during boot time.
Alternatively, if there is a file which is used as a source for scheduling tasks you may be able to overwrite that again using "select into outfile."
system runs local commands on your box. If you need to do anything with logs, either contact your hoster, to provide a way to download them or access them.

How to create a connection with MySQL Admin?

I am extremly new with databases. I have only created one, and that was in SQLite on android. I downloaded MySQL Admin for linux (Ubuntu) and downloaded and installed Admin. Now I want to create a database, but I jave know idea what I'm doing. I created a connection via the drop down menu option 'Open Connection Editor', and I keep getting a 'MySQL Error Nr. 2005' error. Do I have to do something command line wise first to set up the database?
Any help is appreciated.
Aedon
MySQL is different to SQLite in that you need to have some server software running the whole time managing the database. It's that server software that you connect a MySQL client to.
You need to grab yourself a copy of MySQL Server then I'd recommend this tutorial
Good luck!
If you are using Ubuntu you can install MySQL Server easily with this command:
sudo apt-get install mysql-server
and then I suggest you to download MySQL Workbench, it's a GUI Tool where you can do almost everything with MySQL databases (create,drop,alter, modeling, administer the service).
Then, you can create a connection with mysql workbench or mysql admin (or another client) and do your stuff. If you installed the server in your PC then the parameter are something like this:
host = localhost
user = root
password = (the password you specified in the installation)