Why am I getting a segmentation fault (core dumped)? - mysql

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.

Related

mysql 6.1.11 client to Mysql 8 connection

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);

Build Error in developing C and MYSQL application

I am writing some code in C which connects to MYSQL server. I am using Netbeans and new to this. I configured it as directed and installed MYSQL Connector C. I also installed CYGWIN GCC, G++, GDB, MAKE from cygwin site. I created a c project and in the properties-> build-> c compiler->Include directories, set the path of mysql connector (C:\Program Files\MySQL\Connector C 6.0.2\include). Now i write some code to interect with MYSQL server, on build some error occurs.
#include <stdio.h>
#include <stdlib.h>
#include <mysql.h>
int main(int argc, char** argv) {
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "localhost";
char *user = "root";
char *password = "aaaa"; /* set me first */
char *database = "mysql";
conn = mysql_init(NULL);
/* Connect to database */
if (!mysql_real_connect(conn, server,
user, password, database, 0, NULL, 0)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
/* send SQL query */
if (mysql_query(conn, "show tables")) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
res = mysql_use_result(conn);
/* output table name */
printf("MySQL Tables in mysql database:\n");
while ((row = mysql_fetch_row(res)) != NULL)
printf("%s \n", row[0]);
/* close connection */
mysql_free_result(res);
mysql_close(conn);
return (EXIT_SUCCESS);
}
After build following error occurs:
build/Debug/Cygwin-Windows/main.o: In function `main':
/cygdrive/c/Documents and Settings/AEM/My Documents/NetBeansProjects/CppApplication_2/main.c:26: undefined reference to `_mysql_init'
/cygdrive/c/Documents and Settings/AEM/My Documents/NetBeansProjects/CppApplication_2/main.c:28: undefined reference to `_mysql_real_connect'
/cygdrive/c/Documents and Settings/AEM/My Documents/NetBeansProjects/CppApplication_2/main.c:30: undefined reference to `_mysql_error'
/cygdrive/c/Documents and Settings/AEM/My Documents/NetBeansProjects/CppApplication_2/main.c:34: undefined reference to `_mysql_query'
/cygdrive/c/Documents and Settings/AEM/My Documents/NetBeansProjects/CppApplication_2/main.c:35: undefined reference to `_mysql_error'
/cygdrive/c/Documents and Settings/AEM/My Documents/NetBeansProjects/CppApplication_2/main.c:38: undefined reference to `_mysql_use_result'
/cygdrive/c/Documents and Settings/AEM/My Documents/NetBeansProjects/CppApplication_2/main.c:41: undefined reference to `_mysql_fetch_row'
/cygdrive/c/Documents and Settings/AEM/My Documents/NetBeansProjects/CppApplication_2/main.c:44: undefined reference to `_mysql_free_result'
/cygdrive/c/Documents and Settings/AEM/My Documents/NetBeansProjects/CppApplication_2/main.c:45: undefined reference to `_mysql_close'
make[2]: Leaving directory `/cygdrive/c/Documents and Settings/AEM/My Documents/NetBeansProjects/CppApplication_2'
make[1]: Leaving directory `/cygdrive/c/Documents and Settings/AEM/My Documents/NetBeansProjects/CppApplication_2'
collect2: ld returned 1 exit status
make[2]: *** [dist/Debug/Cygwin-Windows/cppapplication_2.exe] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 1s)
I googled a lot but could not find anything to solve this issue. Need Help.
add -lmysql to link with the mysql library (valid for gcc). Or, if you use another Compiler, tell the Compiler
where the Libs are (add the Path)
to link the Library to the executable. This is something different, then adding the Path.

MySql from C on Ubuntu

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

mysql_errno always returns 0

I am trying to connect to the server with a wrong user or password and as expected it fails but the mysql_errno always returns 0. is there something wrong with my mysql installation?.
EDIT: I am using a c program to connect.
int main(int argc, char *argv[]) {
MYSQL my_connection;
mysql_init(&my_connection);
if (mysql_real_connect(
&my_connection,
"localhost",
"rick",
"I do not know",
"foo", 0, NULL, 0)) {
printf("Connection success\n");
mysql_close(&my_connection);
} else {
fprintf(stderr, "Connection failed\n");
if (mysql_errno(&my_connection)) {
fprintf(stderr, "Connection error %d: %s\n",
mysql_errno(&my_connection), mysql_error(&my_connection));
}
}
return EXIT_SUCCESS;
}
IN the book the output is supposed to say error 1045:
In my case it is 0 and nothing but connection failed is printed.
mysql_errno() only reports errors from a valid connection.
The error number comes from the actual database. How can it retrieve these without a connection in the first place?
You'll notice in the API example, they don't bother with the error number for a failed connection
MYSQL mysql;
mysql_init(&mysql);
mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"your_prog_name");
if (!mysql_real_connect(&mysql,"host","user","passwd","database",0,NULL,0))
{
fprintf(stderr, "Failed to connect to database: Error: %s\n",
mysql_error(&mysql));
}

Error detection in C client program connecting MySQL

My C client program connects with MySQL server. I can connect, but the problem arises when I try to implement error detection codes. If I do the following (here I have intentionally put incorrect user name):
#include "mysql.h"
int main()
{
MYSQL* conn_ptr_db;
conn_ptr_db = mysql_init(NULL);
if(!conn_ptr_db)
{
perror("Error connecting to MySQL! ");
exit(0);
}
if(mysql_real_connect(conn_ptr_db,"localhost","rooti","root","mysql",0,NULL,0))
{
printf("Hurrah! we have connected to MySQL! ");
mysql_close(conn_ptr_db);
}
else
{
printf("Connection failed to MySQL! \n");
printf("Error code: %d %s %s\n",mysql_errno(conn_ptr_db),mysql_sqlstate(conn_ptr_db),mysql_error(conn_ptr_db));
}
//mysql_close(conn_ptr_db);
return 0;
}
Now the output to this program:
./db_access
Connection failed to MySQL!
Error code: 1045 28000 Access denied for user 'rooti'#'localhost' (using password: YES)
This is the correct output.
But if I do the following:
#include "mysql.h"
int main()
{
MYSQL* conn_ptr_db;
conn_ptr_db = mysql_init(NULL);
if(!conn_ptr_db)
{
perror("Error connecting to MySQL! ");
exit(0);
}
//CHANGES -------------
conn_ptr_db = mysql_real_connect(conn_ptr_db,"localhost","rooti","root","mysql",0,NULL,0);
if(conn_ptr_db)
{
printf("Hurrah! we have connected to MySQL! ");
mysql_close(conn_ptr_db);
}
else
{
printf("Connection failed to MySQL! \n");
printf("Error code: %d %s %s\n",mysql_errno(conn_ptr_db),mysql_sqlstate(conn_ptr_db),mysql_error(conn_ptr_db));
}
//mysql_close(conn_ptr_db);
return 0;
}
I get the following output:
./db_access
Connection failed to MySQL!
Error code: 0 08001
Here, the mysql_errno() and mysql_error() are not working. Why?
After the connection has failed conn_ptr_db is set to NULL. You then try and use this value as a paramter to a call to msql_errono(), in effect msql_errno(NULL). Hence the error code your getting.
Because in the second example you assign the result to conn_ptr_db.
You should not have done so.
From the documentation:
A MYSQL* connection handle if the
connection was successful, NULL if the
connection was unsuccessful. For a
successful connection, the return
value is the same as the value of the
first parameter.
In the case of an unsuccessful connection (which you have), you've replaced your conn_ptr_db object with NULL then attempt to do stuff with it (mysql_error).
The documentation for mysql_error doesn't explicitly say that a NULL parameter is disallowed, but it's clear that you won't get the error relating to the original, actual connection context that you overwrote.