Run MySQL Script on remote Server - mysql

I have a MySQL server installed in a remote Ubuntu VM that i access using a VPN and i have some sql scripts that i want to run on that server, to create new Stored procedures and queries.
The the details of the virtual machine are:
IP: 192.168.58.61
Hostname: les12a.fi.fr
I am using putty and i am writting directly the commands, but i would like to run directly a script that is on my Windows machine.I have tried using Source, and path but this works fine when MySQL is on my machine. What is the correct syntax to do that on remote server?

just do:
shell> mysql -u user -p -h HOSTNAME DBNAME < yourscript.sql
HOSTNAME: put the hostname if could be resolved or the ip of the remote server
enjoy

SSH to your server, and then run:
mysql -u USERNAME -p DATABASE_NAME < scripts.sql
Obviously, your scripts should be stored in scripts.sql, which should be in your working (current) folder.

Related

Can't connect to MySQL server running inside custom docker

I'm running this on a Ubuntu terminal on my Windows laptop. I'm using docker desktop and have enabled integration with Ubuntu like so:
I've also tried this with docker installed through the Ubuntu terminal (instead of Docker desktop on Windows), but experienced the same issue.
I have a MySQL database in a docker image, and an api docker image, and am running both:
I can access MySQL by running the following command: mysql -uroot -ppassword -h0.0.0.0 -P3306. The api is referencing is connected to this container with the following connection string located in appsettings.json: "Server=0.0.0.0; Port=3306; Uid=root; Pwd=#G3minar31; Database=cybersecuritydatabase". This is also shown in the image below. However, when I try to make a request to the endpoint from the backendapi container (http://127.0.0.1:5001/company/allcompanies), I receive a Can't connect to server error.
How can I resolve this?
UPDATE: When I run mysql -uroot -ppassword -h0.0.0.0 -P3306 in my Windows terminal I receive the following error: ERROR 2003 (HY000): Can't connect to MySQL server on '0.0.0.0:3306' (10049), but if I run mysql -uroot -ppassword -P3306 without specifying the host, MySQL starts successfully.
0.0.0.0 or 127.0.0.1 in config won't work it will point to the app container
either you:
change db address in appsettings.json to your machine local address e.g: 192.168 ..
create a docker network and connect the 2 containers, change db address in appsettings.json to mysql container name instead of an ip address
Run mysql and backendapi with docker-compose, change db address in appsettings.json to mysql service name instead of an ip address, I recommend this

Mysql Server does not require password to access and edit databases

Currently running Ubuntu 18.04.2 and have a freshly installed MySQL server 5.7.26-0ubuntu0.18.04.1 running on my server. My problem is that when I connect to mysql through mysql -u root -p the password I set up through Secure Setup works but then if I type in any random string then that works also. Which leads me to think that it did not work. Additionally, I can login to the mysql server through mysql and mysql -u root.

port fowarding for mysql server ubuntu remote connection

I have a ubuntu 16.04 on which I run 3 servers from virtualbox (a webserver, a home file server and a mysql server).
In this mysql server I naturally have an internal ip (192.168....) and an external one.
However when I try to connect remotely to this mysql server from mysql shell, workbench or .NET connector (eg. mysql -u root -p -h 172.241.442.12) using the external IP of this virtual machine I always get ERROR: Not connected.
My question is whether to connect I have to enable port forwarding in my router (3306), just as I would do enabling 80/8080 in a webserver to make it accessible from the internet.
I never did it before and haven't find any clear tutorial either. My main purpose is to connect to this db in a vb.net application.
Thanks!
Yes, if you intend to access it in this manner you will need to forward a port. You will also need to grant access to MySQL for a user that is allowed remote access.
GRANT ALL ON somedb.* TO `user`#`123.123.123.123` IDENTIFIED BY 'somePassword'
The IP address can be a hostname, or % to match everything.
That said, unless you really need a permanant external connection to MySQL you should not expose it like this, the better option would be to tunnel the port through SSH.
ssh -N user#dbserver -L12345:localhost:3306 &
mysql -u root -h localhost -P 12345

How can access a database in MySQL server on my laptop from a cloud instance (preferably from within R)?

I would like couple things clarified.
(I have mysql client installed on my remote VM running on Ubuntu 16.04, goal is to access the database from within R but I have been trying so far with mysql as such: mysql -u root -p -h fe80::883f:XXXX:XXX:XXXXX -P 3306 where for "XXX" I tried every ip address I get in windows command line when the command ipconfig is issued.
To narrow down my troubleshooting I want couple pointers:
Do I need to give the IP address for my remote(cloud) instance in the mysql config file (thus find my.cnf or its equivalent in windows in dir where mysql server is installed and bind the address for the remote/cloud VM I am trying to access the db on my laptop from).
2 SSL, is this something that needs to be enabled?
Additionally, from Bash on Ubuntu on Windows (WSL) I am able to log into mysql server with `mysql -u root -p'xxx' -h ' i.e. the ip address from windows command line ipconfig output called "Link-local IPv6 Address". After going over several posts and using my imagination(which gets me in trouble with troubleshooting), I feel like in some config file in MySQL server dir is have to "bind the address" for the remote VM (which I access via putty from my laptop), am thinking along the right path?
As for the privileges for the root user in mysql server I believe I have given root every privilege (show below):
enter image description here
If you're going to use the RJDBC package, you have to stick with JDBC. ODBC might be "one letter off", but it's not the same thing.
You need a few things to be in place:
The database on your Windows machine has to be running, visible from the Linux machine, and have a JDBC driver JAR available that will let you connect to it. If you tell me your database is Microsoft Access, you're done. There are no free JDBC drivers for Access.
You have to have the JDBC driver JAR for your database on the Linux machine, in the CLASSPATH that R will use to find it.
You need a JDK installed on the Linux machine. I recommend that you install nothing older than version 8.
Once this is in place it should be easy. I've done it.
I solved my issue and can access MySQL sever on my laptop from remote ression(that I access with putty as follows:
in putty first used 3307 as source, checked the remote radio buttons, set destination to localhost:3306 and at Bash prompt in VM:
mysql -u root -p -h '127.0.0.1' -P 3307
Or from Linux (I use WSL) Bash prompt:
# access the remote VM from Windows Linux subsystem(WSL)
ssh -R 3307:localhost:3306 blsingh#149.165.169.21
## access MySQL server on my laptop from Bash prompt on in WSL
mysql -u root -p -h'127.0.0.1' -P 3307
From here its not difficult to figure out how to access it in R; we have a remote port forward and we use the same parameters in R.
☼

How to access mysql of my Webhost from Command Promt

Is it possible to connect mysql of my webhost from cmd on my pc, in the same way as I am connecting to mysql installed on my localhost using XAMPP.
You'll need to download and install the Windows MySQL client
http://www.mysql.com/downloads/installer/
After installation, make sure the mysql executable is in your cmd PATH.
You need to specify the hostname or IP address of the server with the -h parameter.
mysql -u username -p -h myhost.example.com
Yes its possible to connect to MySQL on web host. MySQL runs on port number 3306. You need to check if the web host has this port open. if not you can request your hosting provider to get it opened. Once its done you can connect to MySQL as you do on localhost May be you need to add privileges to user account to connect from remote machine(your localhost IP).
Other option would be to use PhpMyAdmin.