Connecting to Remote MySQL DB in docker container using PDO - mysql

I am a bit of a newbie and new to the php coding world and have a small task I am trying to find an answer to, but I cannot seem to find the relevant question asked anywhere before.
Basically I have a server where I create reports from mySQL DBs located on other remote machines.
So far these other servers have had a basic mysql server running on them and I can easily connect to them with PDO like this:
$DBcon = new PDO("mysql:host={$DB_host};dbname={$DB_name}",$DB_user,$DB_pass);
$DBcon->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
But I now have a server running mySQL inside of a docker container on this remote server.
So my question is, is it possible to connect to this mySQL db as well using PDO?
I am able to connect to the DB remotely with mySQL workbench using Standard TCP/IP over SSH (as I also do with the other servers), but the one thing I have to specify differently here compared to the other servers not using docker is the 'MySQL server host relative to the SSH server'. Usually just having 127.0.0.1 here is fine, but with mySQL inside a docker container I first have to find this relative address with
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' database-name
you then get something like 172.21.0.5 and add this in that field
workbench connection
How are you able to represent this small change in the PDO statement? Is it still possible to connect in this way?

actually, no. You can't connect inside code as mysql workbench over ssh. But you can use ssh jumb for this connection as below link.
https://www.tecmint.com/access-linux-server-using-a-jump-host/
You should configure jump command to a mysql servers with port. this jump command provides your connection via bastion you mentioned instance for ssh to the mysql server.
for example: ssh -N -i pemfile.pem(if you have to connect to jump server) -L fakemysqlserverip:3306:mysqlserverrealip:3306 ec2-user#jumpserverip
fakemysqlserverip may be localhost,127.0.0.1 or different
jumpserverip is your server which is for using over ssh
Regards

Related

port forwarding to connect to a mysql database on a VPS

I have a mysql database runnnig on 127.0.0.1 on a VPS and now I need to connect to this database I saw that for this I must do a port forwarding in order to expose the mysql service to the internet otherwise I won't be able to connect, I saw here that I can do this connecting via SSH to the VPS and then run the command ssh -L 3306:127.0.0.1:3306 MY_SERVER_IP but I am not sure if this could unset or damage the VPS, this VPS is on production and the developers that created this left the project without finishing it so I have fear of doing a bad configuration and crash the productino server, which could be another way of do this? i.e be able to connect to the database Or is this the safer and correct way?

I need connect mysql via ssh

is there a valid extension for connect mysql via ssh on visual code studio?
I can't find nothing!;
I've been tryed with Mysql managment tool but seem there are not options for ssh
I guess you mean that you want to connect to MySQL via an SSH tunnel.
I don't know any specific solution for visual studio code, but if you have an extension to browse your database (and it doesn't support SSH tunneling) on its own, you can just create a ssh tunnel by yourself. Just run:
ssh -L 3306:127.0.0.1:3306 user#remotehost
And keep that session opened for the time you want to use your DB. Point your DB browser at 127.0.0.1:3306 and that's it.
Keep in mind that 127.0.0.1 in the example is the host you're tunneling TO, so if your MySQL server listens on, say, 1.3.3.7, just do -L 3306:1.3.3.7:3306.

How can I connect a Jupyter Notebook to a remote MySQL DB using Peewee?

I am trying Peewee to connect and retrieve data from a MySQL remote database, but I get the following error:
InternalError: (1130, "Host 'x.x.x.x' is not allowed to connect to this MariaDB server")
Could you help me?
"retrieve data from a MySQL remote database"
"Host is not allowed to connect to this MariaDB server"
Seem to point on a simple problem:
You're not allowed to connect on the DB from "outside".
By default, MySql / MariaDB are only listening on the "inside" of the server, from MariaDb doc :
MariaDB packages bind MariaDB to 127.0.0.1 (the loopback IP address) by default as a security measure using the bind-address configuration directive.
This mean apart for an application that run on the same machine (accessing 127.0.0.1 or localhost), you'll not be able to connect.
Solutions:
SSH tunnelling
This is probably the safest way to allow a connexion on a remote DB.
SSH is a protocol that allow you to connect to a server. It's mainly used on unix server to manage them, but can do a lot more.
How to use it in your case?
if you can connect with SSH to your DB server, then running this simple command on your notebook the will do the trick:
ssh -L 3306:localhost:3306 user#x.x.x.x
Lets explain a bit: first, your run SSH, then, you tell him to enable a port forwarding from your 3306 port to the localhost:3306 port of the server you connect through user#IP.
With this command running, every query from your local machine:3306 will by send to your MariaDB:3306 server, allowing you to use it as if you where on the server.
Allowing a remote-access user
This one is way more dangerous than the previous one. You'll need to take your time and think about every outcome it mean.
As already said, you're not allowed to connect from outside, ssh let you be "inside", but if you know what you do, you can just remove the security.
The point is:
to make an account that'll be able to login from a remote IP,
allow MariaDB to listen on external requests,
and at least, secure other account to disable remote connection.
[I'm not putting the how-to now, if you really need it, I'll update this answer]

Remote access to local MySQL

I want to connect to MySQL. Unfortunately target server is accessible only from localhost. It is able to work around for example by uploading some PHP script on the server? I mean a single file rather than the entire PHPMyAdmin or something similar.
can you please elaborate more about your issue?
If you want to connect to mysql server remotely then you need to grant user in mysql users table with your local system IP range.
You could look at using ssh to tunnel the connection through your local host .. Will need to know a bit about your network layout
http://www.howtogeek.com/howto/ubuntu/access-your-mysql-server-remotely-over-ssh/
ssh -L 3306:localhost:3306 geek#webserver.com
you can use ssh tunneling for that here is a link
ssh -L 3306:localhost:3306 root#remote-mysql
The syntax is
ssh -L <localport>hostname<remoteport> <username>#<servername>.
We’re using localhost as the hostname because we are directly accessing the remote mysql server through ssh. You could also use this technique to port-forward through one ssh server to another server.

Accessing Database created in XAMPP while working in Laravel 5 homestead

I'm starting a large project in Laravel 5. In the past I used XAMPP for all local work. Now we're going to work in Homestead. I got Homestead set up and running and I booted up a Laravel 5 project in it. works fine.
Because of various reasons we do NOT want to use Laravel migrations but are using MYSQL workbench in combo with XAMPP-PHPMyAdmin for work on the mysql database. My question is: how can I access this mysql database from within the Homestead VM?
I tried all kinds of settings in the app/Config/database.php. but none work.
I looked around for related questions here, but none seem to answer the base of my question: is it even possible to access a remote database (or one on my host machine) from within homestead?
I read somewhere that I can create migrations from an SQL dump to duplicate the DB in homestead using a package (https://github.com/Xethron/migrations-generator). using this as a workaround, I could connect my Laravel app to my DB I have set up in XAMPP, generate the migrations, then connect back to the Homestead DB and finally run the migrations. I think this could work but I would prefer direct connection. any suggestions on how to connect to a database outside the Homestead VM would be welcome.
<1>Open .env (can be found in root of laravel)
<2>Change DB_HOST to your LAN IP address(something like 198.168.0.11)
DB_HOST=LAN_IP
<3>Now go to phpmyadmin and in privileges tab
Add new user and fill login info
Select host as any_host(or fill host with your PC name)
I am also relatively new to Laravel and Homestead too but have had some luck with it.
First I'm not sure it's such a great idea not to use the MySQL instance inside Homestead but if you have your reasons I'll take you at your word. That said from your "Homestead" directory you can "vagrant ssh" into your Homestead VM.
Here you can mysql -u homestead -psecret dbname into your database and configure it at will just as you would your XAMPP instance. If you are using Laravel models as long as they match the database you'll be fine.
To your point of connecting to another database from within the VM have you tried to connect to that outside database from the command line? As long as your XAMPP MySQL instance is configured to receive remote connections and you have a user properly configured you should be able to test from the command line from within the Vagrant VM. See below where 192.168.1.196 is the IP address of the box/VM containing the MySQL instance to which you want to connect. I'm not running XAMPP but I was able to make a connection from within Vagrant Homestead out of the VM to another MySQL instance running on my local machine by providing my local network address. "127.0.0.1" won't work obviously because the Vagrant VM will perceive that as itself.
mysql -u user -ppassword dbname -h 192.168.1.196
Hope this helps.