Bug #87896 Mysql Connector/C on mac os 10.12 produces can't connect to server
Submitted: 28 Sep 2017 11:26 Modified: 5 Oct 2017 11:02
Reporter: Mark Maleson Email Updates:
Status: Not a Bug Impact on me:
None 
Category:Connector / C Severity:S1 (Critical)
Version:6.1.11 OS:MacOS (Mac OS X 10.12 (x86, 64-bit), DMG Archive 6.1.11)
Assigned to: CPU Architecture:Any
Tags: connection error, connector/c, mac

[28 Sep 2017 11:26] Mark Maleson
Description:
mysql_real_connect(con,"host","user","password","database",3306,etc
yields can't connect to server.
mysql -h host -u user -p can connect.
mysqlworkbench can connect

so the problem is not a connection problem.
using xcode to develop.
 

How to repeat:
install connector/c
using xcode
#include <mysql.h>

MYSQL *con = mysql_init(NULL);

  if (con == NULL) 
  {
      NSLog(@"%s\n", mysql_error(con));
      exit(1);
  }

  if (mysql_real_connect(con, "localhost", "root", "root_pswd", 
          NULL, 0, NULL, 0) == NULL) 
  {
      NSLog(@"%s\n", mysql_error(con));
      mysql_close(con);
      exit(1);
 }  

change localhost to ANY host
use appropriate userid and password
use database or not after password
use port 3306 or not after database

any of these combinations produces the same result
[1 Oct 2017 21:55] MySQL Verification Team
Thank you for the bug report.

miguel:test miguel$cat bug87896.c

#include <my_global.h>
#include <m_string.h>
#include "mysql.h"

#define DB_HOST			"localhost"
#define DB_USER			"root"
#define DB_PASSW		"miguel"
#define DB_NAME			"test"
#define DB_PORT			3306
#define DB_UNIX_SOCKET	/tmp/mysql.sock

int main( void )
{
  MYSQL mysql;

  mysql_init(&mysql);

  if (!mysql_real_connect(&mysql,NULL,DB_USER,DB_PASSW,NULL,DB_PORT,NULL,0))
  {
    printf("Error: %s\n",mysql_error(&mysql));
    return 0;
  }
  else
	printf("Connected to the server: %s\n",mysql_get_server_info(&mysql));

  mysql_close(&mysql);
}

miguel:test miguel$ gcc -c `mysql_config --cflags` bug87896.c

miguel:test miguel$ gcc -o bug87896 bug87896.o `mysql_config --libs`

miguel:test miguel$ ./bug87896
Connected to the server: 5.7.19

miguel:test miguel$ ls /usr/local/mysql-connector-c-6.1.11-macos10.12-x86_64/
COPYING	README	bin	docs	include	lib
[1 Oct 2017 22:34] Mark Maleson
I believe that there is a bug. I notice you don't use host and don't use database name in your solution. If you change the first null to host it doesn't connect.
[3 Oct 2017 8:35] MySQL Verification Team
Imho The value of host may be either a host name or an IP address. If host is NULL or the string "localhost", a connection to the local host is assumed
Could you please post the results of below?

mysql_config
mysql_config --version

Thanks,
Umesh
[3 Oct 2017 11:35] Mark Maleson
I do not want to connect to the local host, so using null is not an option.
Could you please try to connect to a foreign host?  Or even use "localhost" or "127.0.0.1"? None of these work!
[3 Oct 2017 14:07] MySQL Verification Team
Could you please provide output of below commands along with exact build/compile command used in your environment(clang/gcc whatever it is)?

mysql_config
mysql_config --version

Thanks,
Umesh
[3 Oct 2017 20:14] MySQL Verification Team
The small client below connects with a remote Linux CentOS 7 server running a source distribution MySQL Server, the remote host is called tikal:

miguel:test miguel$ cat bug87896.c
#include <my_global.h>
#include <m_string.h>
#include "mysql.h"

#define DB_HOST			"tikal"
#define DB_USER			"miguel"
#define DB_PASSW		"miguel"
#define DB_NAME			"test"
#define DB_PORT			3357
#define DB_UNIX_SOCKET	NULL

int main( void )
{
  MYSQL mysql;

  mysql_init(&mysql);

  if (!mysql_real_connect(&mysql,DB_HOST,DB_USER,DB_PASSW,NULL,DB_PORT,NULL,0))
  {
    printf("Error: %s\n",mysql_error(&mysql));
    return 0;
  }
  else
	printf("Connected to the server: %s\n",mysql_get_server_info(&mysql));

  mysql_close(&mysql);
}
miguel:test miguel$ gcc bug87896.c -o bug87896 -I /usr/local/mysql-connector-c-6.1.11-macos10.12-x86_64/include/ -L /usr/local/mysql-connector-c-6.1.11-macos10.12-x86_64/lib/  -lmysqlclient
miguel:test miguel$ ./bug87896
Connected to the server: 5.7.21
miguel:test miguel$
[4 Oct 2017 13:56] Mark Maleson
I can see where this works using the gcc command line.
I have trying to compile using Xcode and am still having the same issues.
Do you have any suggestions?
If I tell xcode to include the c.6.11 libmysqlclient.18.dylib I get 'image not found' when I try to run it.
If I tell xcode to use the 5.7.19 libmysqlclient.20.dylib I get 'unable to connect to host' when I try to run it.
[4 Oct 2017 14:09] MySQL Verification Team
"If I tell xcode to include the c.6.11 libmysqlclient.18.dylib I get 'image not found' when I try to run it."

I assume the above happens because it is dynamic library, so to work just copy libmysqlclient.18.dylib to the /usr/local/mysql/lib directory.
[4 Oct 2017 16:42] Mark Maleson
Thanks for the suggestion. After copying it I now get the same thing as the 5.7 error:
2017-10-04 12:40:22.079734-0400 MySqlTest[37301:2127164] MySQL client version: 6.1.11
2017-10-04 12:40:22.084991-0400 MySqlTest[37301:2127164] Can't connect to MySQL server on '107.180.20.186' (1)
[4 Oct 2017 17:45] MySQL Verification Team
Check if the remote host has enabled firewall and Selinux if Linux and edit the MAC hosts file to avoid resolution name issue.
[4 Oct 2017 21:45] Mark Maleson
Thanks for attempting to help. Still no luck.
I installed Netbeans the Mysql Java connector. No issues. I can connect to the server that fails with Xcode/c.
I can also connect successfully using mysql -h and the Mysqlworkbench, so it's not a connectivity issue.
If you ever hear of someone successfully using the c connector with Xcode, please keep me in mind.
Thanks very much,
Mark
[5 Oct 2017 11:02] Mark Maleson
I found out what was going wrong and wanted to thank you so much for your help on this. There is an Xcode setting that permits connections to outside servers that needed to be set. In General/Capabilities. Thanks again.