Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6904501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:04:09+00:00 2026-05-27T08:04:09+00:00

I can use mysql from terminal to query data and I try to connect

  • 0

I can use mysql from terminal to query data and I try to connect to mysql server with C but here is some problem i don’t know to fix but seems like version conflict.

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

int main(int argc, char **argv)
{
  MYSQL *conn;
  conn = mysql_init(NULL);
  if (conn == NULL) {
      printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
      exit(1);
}

// upper code works

// following don't work...
if (mysql_real_connect(conn, "localhost", "username", "password", NULL, 0, NULL, 0) == NULL)
{
  printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
  exit(1);
}
etc...

When I start this code message “Error 2002: Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’ (2)” appears.
Problem is that I don’t have mysql.sock anywhere in computer but have /var/run/mysqld/mysqld.sock.
In linker options I added /var/lib/libmysql.so and program compiles OK.

Is here any simple way to get this to work?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:04:10+00:00Added an answer on May 27, 2026 at 8:04 am

    why not connecting directly to the socket?

    mysql_real_connect(conn, "localhost", "username", "password",
                       NULL, 0, "/var/run/mysqld/mysqld.sock", 0)
    

    I have no environment to test it right now, but I think it should work

    To answer question in your comment:

    I think its’ because of your configuration, mysql’s default configuration is to store socket at /tmp/mysql.sock, and it didn’t find it there, you just specified your location of the socket, this is not a portable solution, it depends on purpose of your application

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

From here: http://dev.mysql.com/doc/refman/5.0/en/order-by-optimization.html In some cases, MySQL can use an index to satisfy an
Can I use mysql database from my personal web server instead of heroku's database?
In MySQL you can use the syntax DELETE t1,t2 FROM table1 AS t1 INNER
How can we re-use the deleted id from any MySQL-DB table? If I want
I have a problem regarding the datediff MYSQL function, I can use it and
Can MySql 5.0 views use tables that are located on another server? What is
How can I use MySQL 3.x and MySQL 5.x simultaneously from my Java application
When I extract data from a MySQL database, some of the output have special
I just set up a new LAMP server. I can connect to mysql through
I'm trying to connect to remote MySQL server with SSL from PHP using mysql_connect:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.