ofxMySQL problem to connect on MAC using MAMP

Hi all!
I want to try this addon: https://github.com/elliotwoods/ofxMySQL
I try to connect to my localhost (on my Mac) and i received these errors:

OF: OF_LOG_ERROR: ofxMySQL: Connection failed to database 'italcornici' on host localhost  
OF: OF_LOG_ERROR: ofxMySQL: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)  

I look around for documentation of “mysql_real_connect” but i don’t resolve.
Maybe someone have the same problems?

It’s been a while since I used mysql, but this looks like it could be either how the DB is configured to receive connections or it might how the connection is actually being routed to the DB. I suppose it could also just not be running as well.

You might just try calling:

/etc/init.d/mysql start

if it’s not running.

Uhmmm… i activated my mysql before, using MAMP interface and i can see my db by browser. So, it’s not the same? Or you think for some reason i need to start from terminal?

I also try to set the port, but nothing.

Well, I don’t really know much about MAMP, I suppose it might be configured to only accept connections from a certain port. I think one problem might be that ofxMySQL is expecting the mysql install to be in the standard location, i…e /etc/init.d/mysql start while MAMP installs it at /Applications/MAMP/Library/bin/mysql

I’d recommend taking a look through the code for ofxMySQL and see if there’s paths to the executables that you can change.

After one hour with my friend very good to write code by console, we found the problem… and it was so easy! :slight_smile:
First, we need to uncheck where MAMP say “Allow local access only”. It’s easy to find, it’s under “MySQL Menu”.
After, if you work on your host, you can’t set the host on “localhost”, but you can only use “”.
And stop… all works! Ok… now it’s so easy, but my friend try to check many thing before find the solution.
Anyway, i think it could be better to modify connect method.
Now is:

void connect(string hostname, string username, string password, string dbname="")  

but i think is better

void connect(string hostname, string username, string password, string dbname="", int port)  

because this time i have to change my port. If i don’t change the port, the addon set number 0 (default port), but in my case doesn’t work.

I also add this method, because often i need to get all field of my table.

bool ofxMySQL::getStrings(vector<vector<string> > &results, string tableName, string whereCondition)  
	string querystring = "SELECT *";  
	querystring += " FROM " + tableName + (whereCondition.length()>0 ? "WHERE " + whereCondition : "");  
	//if query failed, quit  
	//query function will report errors  
	if (!query(querystring))  
		return false;  
	//setup vectors  
	vector<string> resultsRow;  
	//fill in vector  
	MYSQL_ROW row;  
	while (row = mysql_fetch_row(_result))  
		//populate values  
		for (int iField = 0; iField<nFields; iField++)  
			resultsRow[iField] = row[iField];  
		//insert row into results  
	return true;	  

For me now work. Maybe i need to register to github and write there to the author.

Nice. The project lives on github at https://github.com/elliotwoods/ofxMySQL so if you have changes that should be made to it, fork, change, and then issue a pull request. Future OF-ers will thank you :slight_smile:

Trying to use the ofxMysql addon. cant seem to get it to work.

I am using ofx v007 and your latest code from gitHub

I drag and drop the code folder into the addons group folder in Xcode. In my testApp.h I include ofxMySQL.h then in my .cpp try to connect.


#include "ofxMySQL.h"  
class testApp : public ofBaseApp{  
        ofxMySQL db;  


void testApp::logClicked()   
    db.ofxMySQL::connect("", "root", "passwd", "test");  
    vector<ofxMySQLField> vals("val", "1");  
    db.ofxMySQL::insert("test_a", vals);  
    //store data in mysql or db  
    printf("LOG: store in mysql\n");   

Got it to work:


void testApp::logClicked()    
    //connect to the database  
    db.ofxMySQL::connect("", "root", "administrator", "test");  
    //allocate a vector to hold values  
    vector<ofxMySQLField> vals;  
    //insert value for click event for col1  
    ofxMySQLField f1("val", "'value 1'");  
    //insert value for click event for col2  
    ofxMySQLField f2("val2", "'value 2'");  
    //do insert of the data  
    db.ofxMySQL::insert("test_a", vals);  

Dear OF people,
ofxMySQL has some minor challenges for the connection. Here is the brief explanation for the solution for Mac Users

  1. ofxMySQL.h needs the following change
    void connect(string hostname, string username, string password, string dbname, int port);
    in port is required, otherwise no success

2) same function in cpp needs a change with port settings, I have it be 8889 since it is the default setting for my MAMP

void ofxMySQL::connect(string hostname, string username, string password, string dbname="", int port=8889)
if (isConnected)

isConnected = mysql_real_connect(_db,hostname.c_str(),username.c_str(),password.c_str(),dbname.c_str(),port,NULL,0);

if (!isConnected)
ofLog(OF_LOG_ERROR, “ofxMySQL: Connection failed to database '” + dbname + "’ on host " + hostname);
} else
ofLog(OF_LOG_VERBOSE, “ofxMySQL: Successfully connected to database '” + dbname + "’ on host " + hostname);

now it should connect.

here is the query calls for those who want to gather entries from the tables.

db.ofxMySQL::connect(“”, “root”, “root”, “FilikaTwitter”,8889);

string querystring = “SELECT * FROM tweets”;

cout << db.getStrings(id_str, “tweets”, “id_str”) << endl;
cout << db.getStrings(screen_name, “tweets”, “screen_name”) << endl;
cout << db.getStrings(tweet_text, “tweets”, “tweet_text”) << endl;
cout << db.getStrings(created_at, “tweets”, “created_at”) << endl;

However yet there is some sort of problem that I was not able to solve. Being a non-English speaking person, I need to gather data from my mySQL server with UTF8 encoding. But with that I was not able to move ahead. Entries are clear with all proper characters in mySQL or with a Php call, but as soon as I access them with oF, they are no longer proper. I will be grateful for those who would suggest me a solution.
Thanks in advance