R Shiny can't connect to MySQL database hosted on GCP - mysql

I can access the database if I run the app from my local machine without problems, but after deployment on shinyapps.io, I get an error ("The application failed to start."). I suppose that I need to change to host IP from localhost to something else, but the IP address of my database instance does not work either.
pool<- dbPool(
drv = RMySQL::MySQL(),
dbname = 'XXXXXX',
username = 'XXXXXX',
password = 'XXXXXX',
host = '127.0.0.1',
port = 3307
)
For the local connection, I use Cloud SQL Proxy.
Any ideas how the app could connect to the database from the Shiny Server?
How can I account for both the local machine's connection and the cloud-to-cloud connection in my code?
This question is similar but without Cloud hosting:
Error to connect to database (Mysql) when publising shiny app

To connect to your Cloud SQL instance using public ip, the IP address of your Shiny server where your app is deployed must configured as an authorized network
You may check this link on how to configure authorized networks and connect your Cloud SQL instance using public ip address.
Note: If you're going to deploy your application, you may need to modify the host IP and use your sql instance public ip address in your code.

Related

GCP using VM to connect to Cloud SQL via Cloud SQL Auth Proxy Private IP

I am having problems connecting a GCP Compute Engine VM to a Cloud SQL instance configured with private IP (no public) via Cloud SQL Auth Proxy.
After executing
$ ./cloud_sql_proxy -instances=INSTANCE_CONN_NAME=tcp:3306
I see the following output:
2021/10/11 12:29:24 Rlimits for file descriptors set to {Current = 8500, Max = 1048576}
2021/10/11 12:29:28 Listening on 127.0.0.1:3306 for *CLOUD-SQL-CONNECTION-NAME*
2021/10/11 12:29:28 Ready for new connections
2021/10/11 12:29:28 Generated RSA key in 101.010024ms
I haven't seen the final statement on the above on any of the tutorials I have read on this subject.
I CTRL-C out of the above, and I am shown:
^C2021/10/11 12:29:39 Received TERM signal. Waiting up to 0s before terminating.
I then run the below command to connect to my Cloud SQL instance:
mysql -u root -p --host 127.0.0.1
which returns:
Enter password: (password entered)
that returns once the password entered:
ERROR 2005 (HY000): Unknown MySQL server host 'CLOUD-SQL-PROXY-IP-ADDRESS:3306' (22)
Here are some additional information:
My VM (Ubuntu) is in the same region as my Cloud SQL instance, and in the same VPC network.
My CLoud SQL Admin API is enabled
The Cloud SQL instance is configured with only Private IP (Not public)
I have installed the MySQL client in my VM
The scope of the Cloud Access API's on my VM includes Cloud SQL (full access to all API's)
Both myself and the VM's service account has Cloud SQL Admin permissions
From docs:
The Cloud SQL Auth proxy does not provide a new connectivity path; it relies on existing IP connectivity. To connect to a Cloud SQL instance using private IP, the Cloud SQL Auth proxy must be on a resource with access to the same VPC network as the instance.
Which means you won't be able to access the instance unless you are in the same VPC so either you need a vm, a tunnel or a public IP.

How do I connect to my MySQL database from another network

import mysql.connector
db = mysql.connector.connect(
host = "",
user = "username",
passwd = "password",
database = "theDatabase")
c = db.cursor()
c.execute("SELECT * FROM Test2")
print(c.fetchall())
Hi, using this piece of code I can connect to my mysql database that is another computer on the same network. But if I wanted to connect to the database from a computer that is not on the same network, how would I do that? Thanks for all/any responses.
If host is empty "" it will connect tot 127.0.0.1 (localhost). This is fine if your database is on the same server as your webpage or application (which is almost never the case).
Referring to this URL, valid parameters for the host are an IP address. Add the ip of the server where your database runs. Maybe you need to add some firewall rules to allow the connection.
Above is my best guess but i am sure there are some people that can help you better.
The mysql server have to be publicly available from outside on port 3306. If you want to connect from outside it depends on your network. Normally you have to NAT the port to the internal machine that it's available from outside.
https://stevessmarthomeguide.com/understanding-port-forwarding/
If it's available on the IP on port 3306 you can connect to it. It's the same like connecting from inside. Connect directly to the ip.
You can check if the connection works with telnet for example.
telnet 1.1.1.1 3306
It is possible that you have to change your mysql user that the user accessible from * and not from localhost or your network only. That depends how you have configured your user.
How to grant remote access permissions to mysql server for user?

Unable to connect R to mysql which is hosted in Bitnami server in AWS

I am very new to AWS and Wordpress and am unable to connect to Bitnami MYSQL database server hosted in AWS form R shiny.
I want to access the Mysql database which the wordpress is using to store the data. The wordpress is was installed using Bitnami and hosted in AWS.
The AWS details are as below :
When I SSH to the ip, I get the BITNAMI message with a private IP login:
The PHP Myadmin console shows the following details of the wordpress mysql:
I have created a firewall rule for the port 3306 from the AWS console. Both inbound and outbound have the same setup and accepts traffic from all sources : 0.0.0.0
Now I have created one user from the above mysql console and granted it all the access :
CREATE USER 'r_user'#'localhost' IDENTIFIED BY '1234';
GRANT ALL PRIVILEGES ON * . * TO 'r_user'#'localhost';
Retrieve the data
My R server is hosted in Azure with pubic IP :52.187.238.94
The R code:
library(RMySQL)
> mydb = dbConnect(MySQL(), user='r_user', password='1234',
> dbname='bitnami_wordpress', host='34.221.144.129', port =3306);
I am getting the following error message :
> mydb = dbConnect(MySQL(), user='r_user', password='r_gom', dbname='bitnami_wordpress', host='34.221.144.129', port =3306);
Error in .local(drv, ...) :
Failed to connect to database: Error: Can't connect to MySQL server on '34.221.144.129' (0)
I looked at few post but not able sure if they address the same question :
Can't connect to phpmyadmin in Bitnami instance hosted by AWS
Can anyone please help me to connect the Mysql server to R shiny ?
Check if NACL is allowing port 3306 for incoming rules. And outgoing rule is also setup accordingly.
Check security group rules.

Remotely connect to an aws-windows instance based MySQL Database

I have launched an AWS EC2 Windows_Server-2016 instance.
Since this server is windows based,I can successfully connect to
it using remote desktop connection by entering the username,
password and a .ppk file.
I have installed MySQL server on the instance and created a user
that is allowed to login with any ip i.e by providing the '%' sign.
Here's what my AWS instance security group looks like :
I have tried connecting to the MySQL server using SQLYOG using 2
different scenarios :
Tried connecting by using the aws public ipv4 address as a host field and
using mysql username & password
Tried connecting by creating an SSH connection i.e providing server
username/password and .ppk file in SSH connection panel & then
providing mysql host/username and localhost in mysql connection
panel
None of the methods used allows me to connect.I try same procedure with a linux based server and can connect successfully. Please let me know where am I going wrong while using a windows based MySQL hosting.
MySQL has an extra level of IP protection for remote clients. You have to GRANT access to the remote ip(your house of office) to be able to connect. As root, you have to do :
GRANT ALL ON <db>.* TO '<username>'#'<remote ip>' IDENTIFIED BY '<remote_password>';
Where is the DB you are trying to connect to etc.

Remote Connect to Google Cloud SQL

Hi (Sorry for my English)
I am a new user to Google Cloud SQL
I create a "Instance" and I have upload my database.
I request an Ip and also I have add on "Authorized Networks" all of my VPS ip's
also I add my current IP from my ISP.
I try to connect from my VPS with
mysql_connect("Requested IP", "root", "PASSWORD")
but nothing happen and I receive this:
Can't connect to MySQL server on 'XXX.XXX.XXX.XXX' (4)
Also I try to connect from my PC with mysql client like
mysql --host=Requested IP --user=root --password=PASSWORD
and I receive this:
ERROR 2005 (HY000): Unknown MySQL server host
What wrong I have done ?
Thank you
You should add your public IP as an Authorized Network in Cloud SQL.
Go to your instance configuration screen -> Access Control tab and on Authorized Networks add your public IP address.