How to securely connect to a database in Next.js app (using Prisma and MySQL) deployed to Vercel (serverless) - mysql

My MySQL database on DigitalOcean is locked down to only be accessible via local connection.
I'd strongly prefer to keep it secure like that (i.e. not to open it up).
I'm used to using SSH tunnels to connect to it (such as ssh -L 3306:localhost:3306 -i ~/.ssh/id_rsa myusername#myhost).
Now that I'm deploying a Next.js app (with Prisma) to Vercel, I'm trying to figure out the appropriate DATABASE_URL environment variable.
How can I set the DATABASE_URL environment variable (or whatever other relevant setting) such that my Next.js backend is able to securely connect to my remote database that is only open to local connections?
I couldn't tell from the docs.

Related

How to connect mysql when deploying node app on heroku

I have already deployed my node app with heroku which is working fine with my local phpmyadmin database but how can i use it without starting mysql on local machine.
OR how to connect my node app deployed in heroku with phpmyadmin(mysql2)
You shouldn't use local MySQL database in the production app which you have deployed in a hosting provider in this case Heroku.
You could create a remote MySQL database resource from providers like db4free.net.
Update your code to use database credentials of the remote database and start using it.
Note Heroku also provides a MySQL service. You could set up your database there too if you need.

Connect localhost Mysql from Docker .net core

I have working WebAPI in .net core in Docker. I want to deploy this API on AWS EC2 instance with local mysql database working with other web app.
How can I reach this Mysql from inside docker?
Locally I can do it by using my private ip addres in
=> optionsBuilder.UseMySql(#"Server=$my_local_ip;database=db_name;uid=user;pwd=pass;");
Ho to determine which $my_local_ip should I use in order to connect to DB?
But while using the private ip on EC2 I got error while sending request that it can't connect to any MYSQL host.
For MySQL server you generally have to specify the port to connect to, as each instance on the same server uses a different port.
Use --network=host flag while starting the Web API container and use the localhost in you connection string as the host name. This will enable the Docker container to access the host network. Hence you can access MySQL Database from the container.
Note: This is an insecure for running containerized workloads. Click Here to learn more

phpstorm debugging mamp but not my remote server

I have tried many ways to debug my remote server but I am unable to do so. My ftp and sftp and remote db is configures to my phpstorm 9 but I cannot debug my remote server it is connecting to my mamp server and debugging ,y local files but not connecting to server username and password . Basically it fails at mysql_connect but works for mamp.How can I make it deubug with server.Everything else is synced with server but I cannot debug. I really appreciate any help.
Edit: Should I install x-debug on my server(cpanel) also ?
php.ini
[xdebug]
zend_extension="/usr/local/opt/php55-xdebug/xdebug.so"
xdebug.remote_enable=1
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.remote_host=127.0.0.1
xdebug.remote_port=9000
xdebug.idekey=PHPSTORM
xdebug.remote_connect_back=1
Maybe I didn't understand your question/problem. These are the ways you can debug your code
Debug the local code that uses the local database
This is the easiest setup and it probably already works on your system. You have all the files on the local computer and also you have an instance of MySQL running on it. The code connects to localhost:3306, the xdebug extension is installed and it can connect to PhpStorm, everybody is happy.
Debug the local code that uses the remote database
You can have all the PHP files on localhost and use the local mamp stack to debug it; you control the environment, xdebug works and happily collaborates with PhpStorm. You want the code to be able to use the remote (live) database.
In this case you need a way to access the database. Either you create a MySQL user that allows you to connect from the IP address of the local computer (a firewall along the way might prevent this), or start a ssh session that creates a tunnel from the local port 3306 (or any other open port you choose) to port 3306 of the database server (assuming the host where you ssh is allowed to connect to it). You can do this by running
ssh user#remote_host -L 3306:database_host:3306
(replace user, remote_host and database_host with your actual values)
If you have a MySQL server installed and running on localhost then the local port 3306 is not open and ssh cannot use it as the source port of the tunnel. Use another port instead (let's say you use 13306):
ssh user#remote_host -L 13306:database_host:3306
Modify the local configuration files of your application to use localhost as database server and 13306 as database port.
Debug the remote code
If you want to debug the live code (it uses the live database) then you have to upload the code on the web server (the live environment) and make it work there (be able to connect to the database etc).
In order to be able to debug it you need to have the xdebug PHP extension installed on the server and properly configured in the server's php.ini configuration file.
The debugger (the remote xdebug extension) needs to connect to your local computer where PhpStorm is listening on port 9000. This is either impossible or making it happen requires changing configuration here and there in several places (that might be out of your control); we better forget about it.
We can use the ssh tunnel trick: start a ssh connection to the server that creates a tunnel from local port 9000 to the servers port 9000:
ssh user#remote_host -L 9000:localhost:9000
Test if it works
PhpStorm provides a tool that uploads a script on the web server then tries to access it to check if the xdebug extension is properly configured. Depending on the version of PhpStorm you use, you can find it either in the menu (Run -> Web Server Debug Validation, on PhpStorm 9) or somewhere in the Settings (PHP -> Servers or around, on older versions).

No environment variables in OpenShift

I have created a new OpenShift account for a new application I'm developing.
I have added a MongoDB cartridge for the database, and a Tomcat cartridge for the Java web application.
I now need to connect to the database from my Java web app, but I miss two authentication details:
$OPENSHIFT_MONGODB_DB_HOST
$OPENSHIFT_MONGODB_DB_PORT
As far as I know, I have to type rhc env list -a the_name_of_my_app in the console, but my application seems to have no environment variables set.
What can I do?
Apparently, the default enironment variables are visible only via ssh.
In order to see them, you have to type rhc ssh <appid-as-seen-on-openshift-console> followeb by env.
you can see environment variables by doing ssh to openshift. Also you can use openshift port forwarding feature to setup a connection locally to your database.
Openshift blog link for port forwarding

Can I deploy mysql in the openshift and access from the outter

I create a diy Appliation and add a mysql cartridge and bind it to the port 8080.Is it possible to access outside the network through domain? I have a little idea about the routing
system.
I am not very sure what you want here. But you can use putty in your local machine to connect to Openshift mysql.
Thank you
You can use port forwarding using the 'rhc' command line or using the OpenShift Tooling in JBoss Tools. In both case, you'll have a set of local sockets to connect to, and it will forward commands to the remote 3306 port on your OpenShift gear, so you can run MySQL/SQL commands on your database.