Trouble tunneling my local Wordpress install to the mysql database on appfog - mysql

I've set up a wordpress install on appfog (using rackspace), and cloned the install to my local machine for development. I know the install works (using MAMP) because I created a local mysql database and changed wp-config.php to point to it. However, I want to develop without having to change wp-config.php every time I commit. After doing some research, it seems like the Appfog service Caldecott lets me tunnel into the mysql database on the server, using af tunnel. Unfortunately, I'm having issues with getting it working. Even if I change my MAMP mysql port to something like 8889, and tunnel mysql through port 3306, it looks like it's connected but I still get "Error establishing a database connection" when loading my localhost Wordpress. When I quit the mysql monitor (using ctrl+x, ctrl+c), I get a message stating "Error: 'mysql' execution failed; is it in your $PATH?'. Originally, no, it wasn't, but I've fixed my PATH variable on my local machine so that when I go to Terminal and just type mysql, it loads up.
So I guess my question is 2 parts:
1.)Am I going with the right approach for Wordpress development on my local machine
and
2.)If so, why is the tunnel not working?

One way to deal with this is to mimic the VCAP_SERVICES environment variable on your local system with your local database settings. This would allow you to use the same custom AppFog wp-config.php file which looks at VCAP_SERVICES to get its db creds.
export VCAP_SERVICES='{"mysql-5.1": [{"credentials": {
"hostname": "localhost",
"port": 3306,
"password": "root",
"name": "LOCAL_DATABASE_NAME",
"user": "root"}}]}'
EDIT: You will need to restart the Apache server in MAMP after setting this env var. (Thanks Dex)
This will eliminate the need to point your development code at your production database.

simply test for whether vcap_services are available. if they are, use one config. if they arent, use a different config.
here is an example of my local + appfog development website configuration file.
if(getenv("VCAP_SERVICES")){
//if in webserver
$services_json = json_decode(getenv("VCAP_SERVICES"),true);
$mysql_config = $services_json["mysql-5.1"][0]["credentials"];
$username = $mysql_config["username"];
$password = $mysql_config["password"];
$hostname = $mysql_config["hostname"];
$port = $mysql_config["port"];
$db = $mysql_config["name"];
define("DB_SERVER", "$hostname");
define("DB_USER", "$username");//enter your database username
define("DB_PASS", "$password");//databse password
define("DB_NAME", "$db");//database name
} else {
//if in local development
define("DB_SERVER", "localhost");
define("DB_USER", "website");//enter your database username
define("DB_PASS", "dfgdfgdf");//databse password
define("DB_NAME", "fgdf_web");//database name
}
also, you can use .afignore same way you'd use .gitignore to ignore some files from the af update feature. u can update once with appropriate config, then add afignore, then it will never get updated again.

Here is a quick and very dirty script to automate the process based on Tim Santeford's answer. Be sure to change the LOCAL_DATABASE_NAME
#!/bin/bash
export VCAP_SERVICES='{"mysql-5.1": [{"credentials": {"hostname": "localhost", "port": 8889, "password": "root", "name": "LOCAL_DATABASE_NAME", "user": "root"}}]}'
/Applications/MAMP/Library/bin/httpd -k stop
sleep 3
/Applications/MAMP/Library/bin/httpd -k start

Related

How to setup and connect my database to NodeJS application on digital ocean

I have managed to create a droplet on Digital-Ocean and managed to clone my Node JS app onto it. Locally , the app connects to MySQL database and I wanted to the same on the live version. Ignorantly, I attempted to create a Managed database cluster which I did and added 1 user account and created 1 database. Right now I do not know how I can import the exported database.sql file into the database since I am only used to phpMyAdmin.
How can I get this to work and connect to my NodeJS app?
You were using phpmyadmin as an interactive Mysql client program. It's easy to use but hard to set up because it's a web app.
Try another MySql client program. The command-line client, memorably named mysql, is a good choice.
Get a shell on your droplet, then say
sudo apt install -y mysql-client
mysql -u username -p -h databasehostname -D database
mysql> source database.sql
mysql> quit
You'll be prompted for your database password.
That should import your database.
The mysql command line program is very useful and worth some of your time to learn to use.
First, make sure your database cluster is not open to the outside world by adding a DB firewall using DigitalOcean databases. You can allow connections from your own droplet's private IP address, and your own public address (or VPN or however way you're set up). Once you've done that, you should be able to import your SQL file locally (or from the DO Droplet, as long as you have the mysql client installed):
mysql -h [host-provided-do] -P [port-provided-do] -u [username-provided-do] -p [db-name-provided-do] < my-file.sql
The most important thing is to make sure your managed database is not open to the outside world, and that you make sure it only allows incoming connections from known IP addresses.
In your NodeJS app, you can set the driver to connect to the private subnet that DO provides.

Using mysqli_connect() for Wordpress connection to Cloud SQL on Google App Engine

I am trying to run Wordpress on Google App Engine standard environment. I have configured a Cloud SQL for MySQL Second Generation instance and can access it using Cloud SQL Proxy with this command:
cloud_sql_proxy -instances=my_project_id:us-central1:my_project=tcp:3306
The wp-config.php file:
if (isset($_SERVER['GAE_ENV'])) {
define('DB_HOST', ':/cloudsql/my_project_id:us-central1:my_project');
} else {
define('DB_HOST', '127.0.0.1');
}
Finally, I connect to the database using this:
$dbConn = mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD) or die (mysqli_error($dbConn));
mysqli_select_db($dbConn, DB_NAME) or die(mysqli_error($dbConn));
This setup works perfectly from the local development environment, which is Cloud Shell. The website runs and I am able to query the database and insert records etc. My problem arises when I deploy to my_project_id.appspot.com using google app deploy. The website runs, but when I try to query the database I receive this error:
Warning: mysqli_connect(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /srv/wp-content/themes/mytheme/system/db.php on line 14
Line 14 is $dbConn = mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD)
so I am guessing that mysqli must not like the format of the specified DB_HOST which is :/cloudsql/my_project_id:us-central1:my_project.
In this Community Tutorial there is sample code which uses a unix socket and PDO to connect to the database. I don't know if I should be adding these lines to the app.yaml file and someone using this different connection string.
env_variables:
MYSQL_DSN: mysql:unix_socket=/cloudsql/my_project_id:us-central1:my_project;dbname=my_dbname
MYSQL_USER: username
MYSQL_PASSWORD: password
My apologies for the lengthy question, but I wanted to provide as much information as possible. Anyone have any ideas what I am doing wrong? Thanks.
It looks like you might be passing in the information incorrectly into mysqli_connect. If you take a look a the documentation for it, it actually takes in 6 parameters: host, username, passwd, dbname, port, socket.
Under host, you can read the following:
Passing the NULL value or the string "localhost" to this parameter, the local host is assumed. When possible, pipes will be used instead of the TCP/IP protocol.
Under socket, it clarifies it should be the socket path:
Specifies the socket or named pipe that should be used.
So you need to call mysqli_connect like this:
mysqli_connect (null, "user", "password", "database", 3306, "/cloudsql/<INSTANCE_CONNECTION_NAME>")
From Cloud Shell, your environment is all setup properly in order to directly connect to Cloud SQL. From AppEngine there's a few other steps necessary in order to connect.
Check out this documentation:
https://cloud.google.com/sql/docs/mysql/connect-app-engine
It should get you up and running.
The answer by #Kurtisvg is absolutely correct in terms of this being the proper format for connecting to Cloud SQL using mysqli_connect:
mysqli_connect (null, DB_USER, DB_PASSWORD, DB_NAME, 3306, "/cloudsql/<INSTANCE_CONNECTION_NAME>")
The original question, however, also mentioned that I was trying to get the connection working for a Wordpress installation. These are the two areas with additional information relevant to this tutorial on how to run Wordpress on Google App Engine standard.
1. Specifying correct mysqli_connect() parameters
Testing the app in the local development environment only required mysqli_connect to use these four parameters: DB_HOST, DB_USER, DB_PASSWORD, DB_NAME. Once the app is deployed to Google App Engine, the mysqli_connect has to use all six parameters: DB_HOST, DB_USER, DB_PASSWORD, DB_NAME, DB_PORT, DB_SOCK. This is the code in db.php that provides the correct parameters, depending on the environment:
if (isset($_SERVER['GAE_ENV'])) {
$dbConn = mysqli_connect (null, DB_USER, DB_PASSWORD, DB_NAME, DB_PORT, DB_SOCK);
} else { // local environment
$dbConn = mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
}
2. Specify correct Wordpress configuration in wp-config.php
What I was finding was that in order Wordpress to function correctly, it was necessary to not only define and use the socket DB_SOCK in mysqli_connect(), but I also had to define a DB_HOST for purposes of the Wordpress installation. This was the configuration that worked in my wp-config.php file:
define('DB_USER', 'user');
define('DB_PASSWORD', 'password');
define('DB_NAME', 'database');
define('DB_PORT', 3306);
// Check for Google App Engine Environment
if (isset($_SERVER['GAE_ENV'])) {
$onGae = true;
define('DB_HOST', ':/cloudsql/<INSTANCE_CONNECTION_NAME>');
define('DB_SOCK', '/cloudsql/<INSTANCE_CONNECTION_NAME>');
} else {
$onGae = false;
define('DB_HOST', '127.0.0.1');
}
In the above code, the variable for DB_HOST requires a full-colon : at the beginning of the socket. This DB_HOST variable is not used as one of the connection parameters of mysqli_connect when in the GAE environment. This variable does seem to be used elsewhere in Wordpress (such as setup-config.php), which is why it needs to be defined. The variable for DB_SOCK does not require the full-colon : in order to work in the GAE environment. This socket needs to be the last (6th) parameter of mysqli_connect, with the first parameter specified as null, which forces the connection to use the socket.
It took a while to get this sorted, but eventually got it working using these settings above. I wonder if anyone else had such a complicated experience as I did getting Wordpress on Google App Engine standard environment to connect to a Cloud SQL for MySQL Second Generation instance. I hope these comments help someone.
You are better off using wpdb class. Wpdb is a WordPress database access abstraction class. Check out this documentation

replacing remote package with local directory

I'm playing around with thisenter link description here Go CRUD api app. The READ.me advises that mysql needs to be configured in config/dbconn.go. In the source code, mysql is configured to use port 3306. I cloned the source code and changed the config to use port 8889, which MAMP mysql requires me to use for my own scripts (the only mysql I have on my system is through MAMP).
dbUserName := "root"
dbPass := "root"
dbIp := "127.0.0.1"
dbPortNo := 8889
When I ran cloned version of the app and tried to visit one of the routes, I got this error message
Create tables failed dial tcp 127.0.0.1:3306: connection refused
exit status 1
So I'm assuming that it's trying to connect on the port through 3306, and when I look in the util.go file of the project, I see that it's importing the config from the remote repo, rather (I'm assuming) than using the config file in the local repo that I changed to use the mysql settings for MAMP
util.go
import (
"github.com/mantishK/gonotevanilla/config"
)
My question is, if the problem is as I think it is, how do I tell util.go to use the database config file at /config/dbconn.go rather than the file that's in the remote repo.
I tried to using
"config"
and
"/config"
and I got the same error

Unable to connect to remote mysql server using unixodbc, libmyodbc

I'm a little green at this, and I hope the issue I'm having is a simple one...edit: new information at bottom
I need to make a connection to a remote mysql (Amazon RDS) database.
After following a few tutorials, I have unixodbc and libmyodbc installed and configured on the client, but when I try to connect via isql, I get the error
[08S01][unixODBC][MySQL][ODBC 5.1 Driver]Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
[ISQL]ERROR: Could not SQLConnect
The most confusing part about this error is that I'm not trying to connect to a local database, but rather to a remote one. I do not have a mysql.sock file on the client...this isn't the issue though is it?
I'm sensing a configuration error but I'm just not sure what it could be.
If I run odbcinst -j then the output is:
DRIVERS............: /etc/unixODBC/odbcinst.ini
SYSTEM DATA SOURCES: /etc/unixODBC/odbc.ini
USER DATA SOURCES..: /root/.odbc.ini
The content of /etc/unixODBC/odbcinst.ini is:
[MySQL]
Description = ODBC for MySQL
Driver = /usr/lib64/libmyodbc5.so
Setup = /usr/lib64/unixODBC/libodbcmyS.so
UsageCount = 5
[MySQL ODBC 515 Driver]
Description = ODBC 5.515 for MySQL
DRIVER = /usr/lib64/libmyodbc5-5.1.5.so
SETUP = /usr/lib64/unixODBC/libodbcmyS.so
UsageCount = 3
Please note that I had to make up this configuration myself, I did a find for libmyodbc* and found these two .so files, thus set up a driver for each of them. A search for libodbcmyS* yields:
/usr/lib64/unixODBC/libodbcmyS.so.1
/usr/lib64/unixODBC/libodbcmyS.so
/usr/lib64/unixODBC/libodbcmyS.so.1.0.0
So, I don't know what else that configuration could be.
The content of /etc/unixODBC/odbc.ini is:
[target_db]
Driver = MySQL
Server = [servername.com]
Port = 3306
Database = [databasename]
Option = 2
User = [username]
Password = [password]
I've tried different options in "Driver", changing it from MySQL, to MySQL ODBC 515 Driver, to the path to the .so file (eg: /usr/lib64/libmyodbc5.so) and all yield the same result.
I'm running:
odbcinst -i -d -f /etc/unixODBC/odbcinst.ini
Followed by:
odbcinst -i -s -l -f /etc/unixODBC/odbc.ini
Followed by:
odbcinst -s -q
Which prints out the name of my connection, ie [target_db]
Then, I try the connect:
isql -v target_db user password
or just
isql -v target_db
and get the error shown above.
Anyone happen to know what I'm doing wrong here? Thanks a bunch-
EDIT:
Wanted to mention that I'm able to connect to the database from this server using the mysql command line tools.
I installed a local mysql database, and I'm able to connect to this using isql. It seems to be ignoring my odbc.ini file entirely, i have to enter a name with the command, ie isql -v test-database, but it still tries to connect to localhost despite my settings.
I feel as though I've tried everything but will keep at it and will post if i find a solution.
You could try to connect using the ip of your server instead of the dns entry on the "Server" line of odbc.ini.. Have you verified the driver is installed with phpinfo()?
Try to set the environment variable ODBCINI with the path of your odbc.ini file.
Keep in mind that the odbc.ini file you point to must be "write-accessible" by the user that is running the program (i.e. the user must have permissions to write in this file).
chmod g+w .odbc.ini did it for me since we run the DB with ORACLE-Start and the crs-User seems to be in charge

centos server running cakephp can't access remote mysql server but can access via pdo connect outside of cake

I asked this on stackexchange also but its starting to look more like a programming issue instead of configuration
centos server running cakephp can't access remote mysql server but can access via pdo connect outside of cake
I can connect using the mysql commandline client using the same credentials and options. however when cake tries to connect to the remote server all I get is
Error: SQLSTATE[HY000] [2003] Can't connect to MySQL server on 'subdomian.domain.com' (13) requires a database connection
I tried running this
GRANT ALL PRIVILEGES ON *.* TO 'myrootuser'#'webserver.domain.com' WITH GRANT OPTION;
This won't help me because I know I can connect via the cli
I can connect remotely from my home computers cake instance with no problems same host same credentials same everything
Yet my centos apache server running cake with all the right mysql packages installed can't connect
I am using the correct username password port everything and I keep getting that error
I have cleared tmp folders cache I used apc to clear cache I even restarted httpd
I have done everything. Nothing works.
I have event tried temporarily shutting off the firewall on both machines... no dice same error
I even tried creating a php file that just uses pdo to connect to the db and running it from the command line and it says Connected to Database !!
Why can't cake do it from cake. It can obviously work from a lone php file
I verified my creds and host over and over again
again I am able to connect using the exact same codebase on my local machine
so there has to be something like a mystery cake or centos apache thing going
I am also running APC Restarting mysqld and httpd doesn't change anything. I am at my wits end with this nonsense.
this is the php script I am testing with
$hostname = "my.dns.name.tohost";
$username = "myrootuser";
$password = "myrootuserpass";
try {
$db = new PDO("mysql:host=$hostname;dbname=myDbSchemaName", $username, $password);
echo "Connected to database";
}
catch(PDOException $e) {
echo $e->getMessage();
}
// console output
Connected to database[root#localhost ~]# service httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
[root#localhost ~]# php connectInt.php
Connected to database[root#localhost ~]#
So I am like cool lets go try to site again.
Error: SQLSTATE[HY000] [2003] Can't connect to MySQL server on 'subdomian.domain.com' (13) requires a database connection
nothing works
I even tried other ways in php I did a normal mysql connect script using the same credentails it can connect it can see all the databases.
<?php
$hostname = "dns.name.com";
$username = "mygoodtestednonrootuser";
$password = "somepasswoerd";
try {
$db = new PDO("mysql:host=$hostname;dbname=int_zzipline", $username, $password);
$link = mysql_connect($hostname, $username, $password);
$db_list = mysql_list_dbs($link);
while ($row = mysql_fetch_object($db_list))
{
echo 'Connected to database';
echo $row->Database . "\n";
}
}
catch(PDOException $e) {
echo $e->getMessage();
}
?>
[root#localhost ~]# php connectInt.php
information_schema
mydbname1
myotherdb
mysql
test
So I am like cool lets go try the site again
Error: SQLSTATE[HY000] [2003] Can't connect to MySQL server on 'subdomian.domain.com' (13) requires a database connection
This is infuriating to say the least.
So cakephp doesn't like to connect to other linux servers in the same subnet without any firewall turned on yet any other php executed with the same credentials works. my local pc running the exact same code base talking to the same server works.
SELINUX was the culprit. As soon as I disabled it and rebooted everything was fine. I was running my web virtual host in /opt to help with obfuscation but it appears the biggest pain in my you know what the whole time with simple stuff like giving apache write access to logs, uploads, everything was because of SELINUX. It even was preventing cake from talking to a database it could talk to with no problems otherwise.
I knew centos was secure as a boss, but now I know why. SELINUX
learn about it, what it does and discover a world where centos no longer wants to be stubborn SELinux is CentOS's his little friend that will cover your ass instead of busting your balls.
With power comes great responsibility. When I get over beating myself up about this I will begin the work of reconfiguring my server to use SELINUX responsibly instead of using it without knowing why I am using it. Oh and of course the solution:
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - SELinux is fully disabled.
#SELINUX=enforcing
SELINUX=disabled
# SELINUXTYPE= type of policy in use. Possible values are:
# targeted - Only targeted network daemons are protected.
# strict - Full SELinux protection.
SELINUXTYPE=targeted
SETLOCALDEFS=0
Oh I almost forgot.... You can find the config file in /ect/selinux
just change yours to look like this and you should be able to do any ol thing you please. but so will the h4x0rz