Connect to external MySQL db on Bluehost from apps script - mysql

I have seen a number of posts relating to this issue, however, have still not found an answer that works for me. I'm trying to connect to an external MySQL database on Bluehost, from a Google Apps Script, using Jdbc.getConnection()
I've tried configuring a table with both MyISAM and InnoDB. In both cases I get the "Failed to connect to the database..." error. In one of the posts, I saw that someone had set their storage engine version to 5.5.25a. I looked for how to do that but couldn't find it in the phpMyAdmin interface that Bluehost provides. They also allow you to write SQL scripts but I couldn't find an SQL syntax example other than "ALTER TABLE [tablename] ENGINE=InnoDB", with no way to specify a version number.
In the code sample below, I don't provide a table name since the getConnection() function is failing anyway. If I can get the connection to work, I'll be good to go.
Here's my apps script code:
function myFunction() {
var address = '69.195.124.100:3306';
var user = 'nathany7_usr2';
var userPwd = 'vom4usr2';
var db = 'nathany7_test2db';
var dbUrl = 'jdbc:mysql://' + address + '/' + db;
try{
// Write one row of data to a table.
var conn = Jdbc.getConnection(dbUrl, user, userPwd);
...
// close database
conn.close();
}catch(e){
return e.message;
}

This is an old question but I just happened upon it now and think I might have the answer. Maybe it will be useful for someone else, if not for OP.
If I understand correctly you are trying to connect to a MySQL database running on your bluehost server from another server. By design, however, Bluehost will block all incoming database connections to it's server (a reasonable security measure methinks)
So you first have to follow the steps here ( https://my.bluehost.com/cgi/help/89 ) to allow the server you're executing the script on to pass the connection call through Bluehost's firewall.

Related

How to MySQL INSERT with prisma in Next.js API server

I'm really a newbie on DB programming and Next.js also.
I have tried the function that received json data should be INSERTed (MySQL) with prisma in Next.js API server.
According to the explanation of the prisma, the code for inserting new record was as follows.
const result = wait prisma.[db_name].create({data:[json_data_name]});
for the PUT data [json-data_name].
For every http PUT case , the db connection count was added and after about 500 times inserting, there broke out the problem of "Too many connections...".
I think that the number of record inserted is not confined to 500 or..,
I think the prisma function prisma.[db_name].create makes new connection.
How to insert 2000~3000 http PUT data into MySQL db with prisma for Next.js API server?
const prisma = new PrismaClient();
the code above was moved out of the API handler, then the problem was finished!.
for every call of API handler , new prisma was created and used.
it means a new connection to db was created.
Thank you.

Previously working Google Sheets App Script is now throwing the "Failed to establish a database connection" error for MySQL DB

I've been using Google Sheets App Script successfully the past 4 months that is connected to a MySQL 5.7 DB hosted remotely on a VPS (this script connected to my DB successfully earlier today as well). All of a sudden this afternoon my requests are returning "Failed to establish a database connection. Check connection string, username and password." The database connection still works just fine remotely on my computer using MySQL Workbench.
Additional details:
The credentials didn't change (I confirmed with a new connection test)
I have Chrome V8 Runtime disabled since that does not work well at all
I double checked the Google Server IPs to whitelist and noticed 1 server that's either new or I missed the first time, either way all provided IPs are currently whitelisted
I'm connecting using this syntax: var conn = Jdbc.getConnection(url, user, pwd);
I saw some previous comments from a month ago that some people were able to add these parameters successfully: var conn = Jdbc.getConnection(url+'?verifyServerCertificate=false&useSSL=true&requireSSL=true', user, pwd);
However I just get this: Error Invalid argument: _serverSslCertificate
Any further steps or tips to get this connected successfully again is appreciated, thanks!
Austin, you just made my day... Same thing, my MySQL connections stopped working all of a sudden yesterday. Spent hours trying to fix it. The '?useSSL=false' worked like charm.
Thank you, thank you, thank you
I found a solution.
Seems a change on Google now requires you to explicitly set SSL to false. (previously if you let it omitted it would default to off)
?useSSL=false
So you need to update your connection string to something like this simple example.
function myFunction() {
var conn = Jdbc.getConnection("jdbc:mysql://35.214.129.151:3306/dbiyrogncria1r?useSSL=false", "uyxfedtljijfy8", "rnqgnyrs2dthb");
Logger.log(conn);
conn.close();
}
Apparently it's just working again.... must have been a service blip. What's strange is it started working again after I decided to just try var conn = Jdbc.getConnection(url+'?useSSL=false', user, pwd); since I saw some other comments mention that. Seems like an awful solution, but it started working again after I did that, but has continued to work even after I reverted back to just var conn = Jdbc.getConnection(url, user, pwd);
Follow-up questions, are there any good alternatives to Google Sheets with App Script like usage? I'm getting really tired of Google's awful support - would rather pay for a product than to hear "you get what you pay for" with awful support.
EDIT: After this started working again yesterday by itself, the issue came back. I have now confirmed today that switching to var conn = Jdbc.getConnection(url+'?useSSL=false', user, pwd); has solved my connection issue, but that is not an ideal fix by any means at all. Google must be messing with some network settings on the Apps Script side and it's causing issues.

Exim forward router

I am trying to create an exim router where the from and the to headers are being fetched from a mysql database.
As I can not find much documentation about using such a forwarder for non-existing domains on the local server I am quite stuck.
I have the following router where only the receiving mail account is being matched at the moment:
virtual_user_fwd:
driver = redirect
verify = no
data = ${lookup mysql{SELECT adress FROM mail_forwards WHERE host = '${quote_mysql:$domain}' AND fwd IS NOT NULL} {${sg{$value}{\\n}{, }}}}
I need the next step: Somewhere/Somehow where I define the mail account where the mails are forwarded to.
Is there anyone who can help me get to the next step?
I have found a new way to create the mysql link
virtual_userforward:
driver = redirect
verify = no
data = ${lookup mysql { SELECT goto FROM mail_forwards WHERE adress='${local_part}#${domain}'}}
domains = *
This seems to work for the virtual test, however when testing an email I get an error 'unroutable adress'. Any ideas?

Keeping a user logged in with nodeJS

I have a login system with my NodeJS using mysql-node.
The problem i have how ever is how to keep the user logged in, if they refresh the page they have to login again, because i do not know how to store the session.
My login system is like this:
socket.on('login', function(data,callBack){
var username = sanitize(data['login']).escape(),
pass = sanitize(data['password']).escape();
var query = connection.query('SELECT uid FROM users WHERE name = ? AND pass = ?', [username,pass],
function(err,results){
if(err){
console.log('Oh No! '+err);
} else if(results.length == 1){
//some how set a session here
} else if(!results.length) {
console.log('No rows found!');
}
});
});
I'm having difficulty understanding how i set up a session for each client that connects. Is this possible with NodeJS ?
Reading that they assign express to var app but if i already have this : var app = http.createServer( ... how can i also assign express to it :S bit confusing
You need to understand the difference between a express' server and a native NodeJS' server, here my link comparaison nodejs server vs express server
So you can do:
var app = express();
var server = http.createServer(app);
This enable you to have still the low level functionnaly with NodeJS.
So, if you don't want to use existing modules or framework, you can build your own session manager:
using cookie
using IP/UA
using socket
The best way would be first to implement it with socket, for example:
server.on('connection', function (socket) {
socket.id = id;
});
or
server.on('request', function (req, res) {
req.connection.id = id; // The socket can also be accessed at request.connection.
});
So, you just need to implement a middleware who check the id.
If you want to prevent from session prediction, session sidejacking, etc. you need to combine cookies, ip, socket, and your ideas to make it more secure for your app.
Once you've done your session manager, you can choose where to store the sessions, in a simple object, in redis, in mongodb, in mysql ... (express use MemoryStore by default, but maybe not now)
I don't have an idea if nodejs has core feature of saving sessions. you need to use a database along with it. using Express will help you to utilized a database to persist user sessions. You better study and use it
http://expressjs.com/
http://blog.modulus.io/nodejs-and-express-sessions
I don't think there is any session mechanism within Nodejs' core. However, they are plenty of libraries that would allow you to do it. The first that comes to mind is Connect's session, which is a middleware for Nodejs. Have a look at this question for more details on how to use it.
Have a look at this tutorial from dailyjs which tries to include Express's session into a notepad webapp. The source code is available here. (Note that Express' session is based on Connect's, and is practically the same).
EDIT: Here is a more complete example for Node authentication, using mongoose. They do however show their schemas, so I assume you can easily do the transition to MySQL.

How do I connect to a Google Cloud SQL database using CodeIgniter?

My CodeIgniter app on Google App Engine is not able to connect to my database on Google Cloud SQL. I tried so many things.
My site loads when I leave database username, password & database name empty but, pages that have database calls show an error. It says that no database was selected.
I noticed that my database was not created and created a new database and a user with all privileges. I entered this details in my app and now, it doesn't even connect to the database server. No pages serve.
When I remove only the username & password fields in database.php, it connects to the database server but, doesn't connect to the database.
I checked the mysql database for users and my user has all privileges. I checked all spellings and it is correct. The app is working locally. HOW I CAN FIX THIS? i just can't get it to connect.
Out of the box CodeIgniter will not connect to a Google Cloud SQL instance, modifications to the CI database driver files are required, this is because CI expects that it’s choices are either to connect to localhost or to a remote tcpip host, the developers never anticipated that anybody would want to connect directly to a socket.
I chose to use the Mysqli driver instead of Mysql for performance reasons and here is how I did it:
Step 1) Edit the codeigniter/system/database/drivers/mysqli/mysqli_driver.php file and replace the db_connect function with the following code:
function db_connect()
{
if(isset($this->socket)){
return mysqli_connect(null, $this->username, null, $this->database, null, $this->socket);
}
elseif ($this->port != ”)
{
return mysqli_connect($this->hostname, $this->username, $this->password, $this->database, $this->port);
}
else
{
return mysqli_connect($this->hostname, $this->username, $this->password, $this->database);
}
}
Step 2) Alter your application’s config/database.php (or wherver you want to declare your database settings) - Depending on your application you may choose to add “database” to the autoload array in the yourapp/config/autoload.php or you may choose to manually call the load->database() function. This assumes your application name is “myappname”. Replace APPENGINE-ID and DATABASE-INSTANCE-ID and YOUR_DATABASE_NAME appropriately.
$db[‘myappname’][‘hostname’] = ‘localhost’;
$db[‘myappname’][‘username’] = ‘root’;
$db[‘myappname’][‘password’] = null;
$db[‘myappname’][‘database’] = ‘YOUR_DATABASE_NAME’;
$db[‘myappname’][‘dbdriver’] = ‘mysqli’;
$db[‘myappname’][‘pconnect’] = FALSE;
$db[‘myappname’][‘dbprefix’] = ‘’;
$db[‘myappname’][‘swap_pre’] = ‘’;
$db[‘myappname’][‘db_debug’] = FALSE;
$db[‘myappname’][‘cache_on’] = FALSE;
$db[‘myappname’][‘autoinit’] = FALSE;
$db[‘myappname’][‘char_set’] = ‘utf8’;
$db[‘myappname’][‘dbcollat’] = ‘utf8_general_ci’;
$db[‘myappname’][‘cachedir’] = ”;
$db[‘myappname’][‘socket’] = ‘/cloudsql/APPENGINE-ID:DATABASE-INSTANCE-ID’;
Viola, your CodeIgniter application should now be able to connect and talk to your Google Cloud MySQL database!
Now if you want to get really fancy and enable the database caching, either make alterations to the CI code to use memcache (fastest) or Google Cloud Storage (more guaranteed persistance) but I won’t cover that in this blog…
Answer courtesy of http://arlogilbert.com/post/67855755252/how-to-connect-a-codeigniter-project-to-google-cloud
Have you authorized your appengine app for access to the Cloud SQL instance? Go to the access control panel on the console for the instance (at https://cloud.google.com/console#/project/{project name}/sql/instances/{instance name}/access-control). Look for authorized app engine applications.
Otherwise, if you're connecting to the instance successfully, you'll have to choose the database from your code or configuration (depending on the app). For example, from the "running wordpress" guide (https://developers.google.com/appengine/articles/wordpress) you have to define DB_NAME. If you're handling the connections in your own code you'll need to use mysql_select_db.
From skimming the codeigniter docs, it looks like you need something like:
$config['database'] = "mydatabase";
I'm not familiar with this framework though, so check the docs yourself (http://ellislab.com/codeigniter/user-guide/database/configuration.html).