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?
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
Related
I have a mysql server 8.0.21 running on ubuntu 20.04, on this later i also have a VB win7 32bi guest.
I'm trying to connect to the Mysql server from guest using c api (specifically 6.1.11 connector as newer MySQL 8 installations no more support win7)
I'm running the following basic program:
int main(){
MYSQL *conn;
conn = mysql_init (NULL);
if(mysql_real_connect ( conn, "169.254.128.100", "myuser","mypass", "my_schema", 3306, NULL,0 )==NULL)
{
fprintf(stderr, "%s\n", mysql_error(conn));
}
mysql_close ( conn );
return 0;
}
I get the error: "SSL Connection Error: Unknown error number"
The thing is i can connect fine using the ODBC DSN
I also checked the server status & it requires no ssl ("not in use")
thanks
#include <stdio.h>
#include <stdlib.h>
#include <mysql.h>
static char *host = "localhost";
static char *user = "user";
static char *pass = "password";
static char *db = "mysql";
static char *socket = NULL;
unsigned int port = 3306;
unsigned int flags = 0;
/*
* brew install mysql-client
* export PATH=/usr/local/opt/mysql-client/bin:$PATH
* brew install mysql-connector-c++ (optional)
* gcc -o main $(mysql_config --cflags) main.c $(mysql_config --libs) -L/usr/local/opt/openssl/lib
*
* -L/usr/local/opt/openssl/lib is required if you get ld: library not found for -lssl error
*/
int main(int argc, char *argv[]) {
MYSQL *con = mysql_init(NULL);
if (!mysql_real_connect(con, host, user, pass, db, port, socket, flags)) {
fprintf(stderr, "Error %s (%d)", mysql_error(con), mysql_errno(con));
exit(1);
}
printf("Connected\n");
return EXIT_SUCCESS;
}
You need to do this:
int sslmode = 1;
mysql_options(&p->mysqlConn, MYSQL_OPT_SSL_MODE, &sslmode);
I want to manage a database in C language. So I am using C API for mysql.
I am unable to find a function in C API for loading the a .sql file directly to mysql.
I have gone through documentation but didn't find it.
I also tried using SOURCE file.sql by giving it as a query but I am getting a weird error
source error.You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SOURCE /home/iota/Academics/4-2/NP/a/a4/cars.sql' at line 1
Related code is below:
void connectdb(){
con = mysql_init(NULL);
if (mysql_real_connect(con, "localhost", "root", "psswd", NULL, 0, NULL, 0) == NULL){
fprintf(stderr, "qqddd%s\n", mysql_error(con));
mysql_close(con);
exit(1);
}
mysql_query(con, "drop database if exists testdb");
mysql_query(con, "create database testdb");
if(mysql_query(con, "use testdb")){
fprintf(stderr, "qq%s\n", mysql_error(con));
mysql_close(con);
exit(1);
}
if(mysql_query(con, "SOURCE /home/iota/Academics/4-2/NP/a/a4/cars.sql")){ /*error here*/
fprintf(stderr, "source error.%s\n", mysql_error(con));
mysql_close(con);
exit(1);
}
}
Could you please suggest any way to do this ?
Thank you.
This is the code I am trying to run. It compiles fine, and worked great until yesterday.
#include <my_global.h>
#include <mysql.h>
int main(int argc, char **argv)
{
MYSQL *conn;
MYSQL_RES *result;
MYSQL_ROW row;
int num_fields;
int i;
conn = mysql_init(NULL);
mysql_real_connect(conn, "hostname", "username", "password", "database_name", 0, NULL, 0);
mysql_query(conn, "SELECT * FROM tabletest");
result = mysql_store_result(conn);
num_fields = mysql_num_fields(result);
while ((row = mysql_fetch_row(result)))
{
for(i = 0; i < num_fields; i++)
{
printf("%s ", row[i] ? row[i] : "NULL");
}
printf("\n");
}
mysql_free_result(result);
mysql_close(conn);
}
Note that the parameters for mysql_real_connect() are generic here for privacy, but like I said, it worked yesterday. When I try to run the code after compiling successfully, I get:
Segmentation fault (core dumped)
Paddy pointed out the key problem, I didn't error check. After error checking, I found out that I wasn't being granted access to my remote server and was getting
Error 1045: Access denied for user 'username'#'ip' (using password: YES)
I then realized that the IP address of my computer changed after turning it on. I never knew this happened. So I had to go back into cPanel, and add my 'new' IP address to the remote access list for MySQL, and it works. Now the issue is to find out how my IP address can stay static.
The moral of the story is to always handle errors.
I am connecting C to mysql and then creating a database .
My code is:
#include <mysql.h>
#include <my_global.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);}
if (mysql_real_connect(conn, "localhost", "zetcode", "passwd", NULL, 0, NULL, 0) == NULL)
{printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
exit(1);}
if (mysql_query(conn, "create database testdb")) {
printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
exit(1);
}
mysql_close(conn);}
But i don't have the headers mysql.h and my_global.h
How can i get them??Thanks
In ubuntu/debian
$sudo apt-get install libmysqlclient libmysqlclient-dev
in centos/fedora/RHEL
$yum install mysql-devel
Once you have installed the Client....you should be able to just link them? Or at least add the include/libs to your compiler?
http://www.mysql.com/downloads/
You have to install the library:
http://dev.mysql.com/downloads/connector/c/
Insufficient. You need all the files for development. You need the header and proper libraries or you won't get anywhere. So if you are one some linux check out something like libmysql or search on the proposed download pages.
I'm working on a project in C with MySQL
I'm having a problem connection to the MySQL server. First have one time a connection, this works.
But now in an other section an also need to retrieve info from the db
There I use the same code to connect.
conn = mysql_init(NULL);
//check if there is a connection
if (conn == NULL)
{
printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
exit(1);
}
if (mysql_real_connect(conn, "localhost", "test", "test", "test", 0, NULL, 0) == NULL)
{
printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
}
But when I use this a second time, I get an error
Unhandled exception at 0x009818c9 in
simple.exe: 0xC0000005: Access
violation reading location 0x00000000.
it crashes on the if(conn == NULL)
Anyone got an idea why this don't work?
That seems to be windows's version of a segmentation fault (I've never compiled anything on windows), so, check your pointers and make sure they aren't null, and if they are to be global, make sure they are on the heap.