mysql_query() return 1

 

mysql 라이브러리를 이용한 MySQL C 프로그램에서 이상한 로그를 발견했다.

 

mysql_query()  함수 결과를 수행한 결과값이 1이 나왔던 것.
mysql_query() 에 관한 명세는 다음 링크에서 찾을 수 있었다.

http://dev.mysql.com/doc/refman/5.0/en/mysql-query.html

 

이 중, Return Value와 관계있는 항목을 아래에 나타내었다.

Return Values

Zero if the statement was successful. Nonzero if an error occurred.

정상일 경우 0, 실패일 경우 0이 아닌 다른 값이 나온다고 나와있다.
에러가 발생했을 경우의 나타나지는 결과값들을 살펴보았다.

Errors

mysql_query() 함수의 리턴값이 1인 경우에 대한 내용은 찾을 수 없었다.
결국 mysql_error() 와 mysql_errno() 함수를 이용해 에러 메시지를 직접 로그에 찍어보고 나서야 그 원인을 알 수 있었다.

1054  – Unknown column

undefined reference to `SSL_CTX_free’

 

사내에서 개발중인 제품을 테스트하기 위해 컴파일을 하던 도중, 아래의 오류를 발견했다. mysqlclient 라이브러리를 함께 사용하는 부분이 있었는데, Linking 을 하던 도중 에러가 발생한 것이다.

/usr/lib/mysql/libmysqlclient.a(client.o): In function `mysql_close_free_options’:
(.text+0xd7d): undefined reference to `SSL_CTX_free’
/usr/lib/mysql/libmysqlclient.a(client.o): In function `mysql_get_ssl_cipher’:
(.text+0x1081): undefined reference to `SSL_get_current_cipher’
/usr/lib/mysql/libmysqlclient.a(client.o): In function `mysql_real_connect’:
(.text+0x3a4c): undefined reference to `SSL_get_peer_certificate’
/usr/lib/mysql/libmysqlclient.a(client.o): In function `mysql_real_connect’:
(.text+0x3a60): undefined reference to `X509_get_subject_name’
/usr/lib/mysql/libmysqlclient.a(client.o): In function `mysql_real_connect’:
(.text+0x3a74): undefined reference to `X509_NAME_oneline’
/usr/lib/mysql/libmysqlclient.a(client.o): In function `mysql_real_connect’:
(.text+0x3a7c): undefined reference to `X509_free’
/usr/lib/mysql/libmysqlclient.a(client.o): In function `mysql_get_ssl_cipher’:
(.text+0x108a): undefined reference to `SSL_CIPHER_get_name’
/usr/lib/mysql/libmysqlclient.a(viossl.o): In function `report_errors’:
(.text+0x45): undefined reference to `ERR_get_error_line_data’
/usr/lib/mysql/libmysqlclient.a(viossl.o): In function `ssl_do’:
(.text+0x8c): undefined reference to `SSL_new’
/usr/lib/mysql/libmysqlclient.a(viossl.o): In function `ssl_do’:
(.text+0x9a): undefined reference to `SSL_clear’
/usr/lib/mysql/libmysqlclient.a(viossl.o): In function `ssl_do’:
(.text+0xa2): undefined reference to `SSL_get_session’
/usr/lib/mysql/libmysqlclient.a(viossl.o): In function `ssl_do’:
(.text+0xae): undefined reference to `SSL_SESSION_set_timeout’
/usr/lib/mysql/libmysqlclient.a(viossl.o): In function `ssl_do’:
(.text+0xbc): undefined reference to `SSL_set_fd’
/usr/lib/mysql/libmysqlclient.a(viossl.o): In function `ssl_do’:
(.text+0xd1): undefined reference to `SSL_get_fd’
/usr/lib/mysql/libmysqlclient.a(viossl.o): In function `ssl_do’:
(.text+0x14b): undefined reference to `SSL_free’
/usr/lib/mysql/libmysqlclient.a(viossl.o): In function `sslconnect’:
(.text+0x16f): undefined reference to `SSL_connect’
/usr/lib/mysql/libmysqlclient.a(viossl.o): In function `sslaccept’:
(.text+0x18f): undefined reference to `SSL_accept’
/usr/lib/mysql/libmysqlclient.a(viossl.o): In function `vio_ssl_close’:
(.text+0x1c5): undefined reference to `SSL_set_quiet_shutdown’
/usr/lib/mysql/libmysqlclient.a(viossl.o): In function `vio_ssl_close’:
(.text+0x1cd): undefined reference to `SSL_shutdown’
/usr/lib/mysql/libmysqlclient.a(viossl.o): In function `vio_ssl_delete’:
(.text+0x212): undefined reference to `SSL_free’
/usr/lib/mysql/libmysqlclient.a(viossl.o): In function `vio_ssl_write’:
(.text+0x261): undefined reference to `SSL_write’
/usr/lib/mysql/libmysqlclient.a(viossl.o): In function `vio_ssl_read’:
(.text+0x281): undefined reference to `SSL_read’
/usr/lib/mysql/libmysqlclient.a(viosslfactories.o): In function `report_errors’:
(.text+0x25): undefined reference to `ERR_get_error_line_data’
/usr/lib/mysql/libmysqlclient.a(viosslfactories.o): In function `new_VioSSLFd’:
(.text+0x99): undefined reference to `SSL_CTX_new’
/usr/lib/mysql/libmysqlclient.a(viosslfactories.o): In function `new_VioSSLFd’:
(.text+0xbc): undefined reference to `SSL_CTX_set_cipher_list’
/usr/lib/mysql/libmysqlclient.a(viosslfactories.o): In function `new_VioSSLFd’:
(.text+0xd9): undefined reference to `SSL_CTX_load_verify_locations’
/usr/lib/mysql/libmysqlclient.a(viosslfactories.o): In function `new_VioSSLFd’:
(.text+0xfb): undefined reference to `SSL_CTX_use_certificate_file’
/usr/lib/mysql/libmysqlclient.a(viosslfactories.o): In function `new_VioSSLFd’:
(.text+0x125): undefined reference to `SSL_CTX_use_PrivateKey_file’
/usr/lib/mysql/libmysqlclient.a(viosslfactories.o): In function `new_VioSSLFd’:
(.text+0x135): undefined reference to `SSL_CTX_check_private_key’
/usr/lib/mysql/libmysqlclient.a(viosslfactories.o): In function `new_VioSSLFd’:
(.text+0x142): undefined reference to `DH_new’
/usr/lib/mysql/libmysqlclient.a(viosslfactories.o): In function `new_VioSSLFd’:
(.text+0x166): undefined reference to `BN_bin2bn’
/usr/lib/mysql/libmysqlclient.a(viosslfactories.o): In function `new_VioSSLFd’:
(.text+0x185): undefined reference to `BN_bin2bn’
/usr/lib/mysql/libmysqlclient.a(viosslfactories.o): In function `new_VioSSLFd’:
(.text+0x1bb): undefined reference to `SSL_CTX_ctrl’
/usr/lib/mysql/libmysqlclient.a(viosslfactories.o): In function `new_VioSSLFd’:
(.text+0x1c3): undefined reference to `DH_free’
/usr/lib/mysql/libmysqlclient.a(viosslfactories.o): In function `new_VioSSLFd’:
(.text+0x1da): undefined reference to `SSL_load_error_strings’
/usr/lib/mysql/libmysqlclient.a(viosslfactories.o): In function `new_VioSSLFd’:
(.text+0x1eb): undefined reference to `SSL_library_init’
/usr/lib/mysql/libmysqlclient.a(viosslfactories.o): In function `new_VioSSLFd’:
(.text+0x1f0): undefined reference to `OPENSSL_add_all_algorithms_noconf’
/usr/lib/mysql/libmysqlclient.a(viosslfactories.o): In function `new_VioSSLFd’:
(.text+0x206): undefined reference to `SSL_CTX_set_default_verify_paths’
/usr/lib/mysql/libmysqlclient.a(viosslfactories.o): In function `new_VioSSLFd’:
(.text+0x21d): undefined reference to `SSL_CTX_free’
/usr/lib/mysql/libmysqlclient.a(viosslfactories.o): In function `new_VioSSLFd’:
(.text+0x2bd): undefined reference to `DH_free’
/usr/lib/mysql/libmysqlclient.a(viosslfactories.o): In function `new_VioSSLFd’:
(.text+0x2d6): undefined reference to `SSL_CTX_free’
/usr/lib/mysql/libmysqlclient.a(viosslfactories.o): In function `new_VioSSLAcceptorFd’:
(.text+0x328): undefined reference to `TLSv1_server_method’
/usr/lib/mysql/libmysqlclient.a(viosslfactories.o): In function `new_VioSSLAcceptorFd’:
(.text+0x36f): undefined reference to `SSL_CTX_ctrl’
/usr/lib/mysql/libmysqlclient.a(viosslfactories.o): In function `new_VioSSLAcceptorFd’:
(.text+0x389): undefined reference to `SSL_CTX_set_verify’
/usr/lib/mysql/libmysqlclient.a(viosslfactories.o): In function `new_VioSSLAcceptorFd’:
(.text+0x39f): undefined reference to `SSL_CTX_set_session_id_context’
/usr/lib/mysql/libmysqlclient.a(viosslfactories.o): In function `new_VioSSLConnectorFd’:
(.text+0x3c6): undefined reference to `TLSv1_client_method’
/usr/lib/mysql/libmysqlclient.a(viosslfactories.o): In function `new_VioSSLConnectorFd’:
(.text+0x405): undefined reference to `SSL_CTX_set_verify’
/usr/lib/mysql/libmysqlclient.a(my_compress.o): In function `my_uncompress’:
(.text+0x60): undefined reference to `uncompress’
/usr/lib/mysql/libmysqlclient.a(my_compress.o): In function `my_compress_alloc’:
(.text+0x102): undefined reference to `compress’
collect2: ld returned 1 exit status
make: *** [nxstart] 오류 1

위의 오류는 ssl 관련 라이브러리를 찾지 못해서 발생되는 오류였다. 보통 SSL 라이브러리의 경우 MySQL 이 설치될때 같이 설치가 되는 것 같았는데 이번엔 조금 경우가 특이했다.

OS는 64비트 였지만 제품은 32 비트로 컴파일 해야 했던 것. 처음에는 약간 헤매었지만 결국 해결할 수 있었다.

OS:

Linux localhost.localdomain 2.6.18-238.el5 #1 SMP Thu Jan 13 15:51:15 EST 2011 x86_64 x86_64 x86_64 GNU/Linux

SSL 라이브러리 설치:

# yum install openssl-devel.i386

SSL 라이브러리 추가:

-lssl

 

How to use gdb in Multi Threaded Network Program

 

Stack Overflow 에서 상당히 유용한 스레드를 발견하여 이곳에 싣는다.

내용은 멀티 스레드 환경에서 네트워크 프로그램을 디버그 할 때 어떻게 gdb를 사용하는가? 이다.

 

Enable core dump saving in your environment. Run command ulimit -c unlimited and rerun your program. When it crashes, load generated core dump in gdb and look backtraces of crash. In case of multi threaded program it is convenient to obtain backtraces from all threads by one command at once: (gdb) thread apply all bt.

 

결론적으로 다음의 한 줄로 요약될 수 있다.

(gdb) thread apply all bt

원글의 주소는 아래와 같다.

http://stackoverflow.com/questions/6813629/how-do-i-use-gdb-for-multi-threaded-networking-program

 

 

 

Ubuntu Virtual Box Start Error

 

Ubuntu 11.04 Kernel Version

Linux localhost.localdomain 2.6.18-194.32.1.el5 #1 SMP Wed Jan 5 17:53:09 EST 2011 i686 i686 i386 GNU/Linux

환경에서 Virtual Box 를 구동하려고 하니 다음과 같은 에러메시지가 나타났다.

“Failed to start the virtual machine WindowsXP.
Failed to open/create the internal network ‘HostInterfaceNetworking-eth0’ (you might need to modprobe vboxnetflt to make it accessible) (VERR_SUPDRV_COMPONENT_NOT_FOUND).
Please install the virtualbox-ose-dkms package and execute ‘modprobe vboxdrv’ as root (VERR_SUPDRV_COMPONENT_NOT_FOUND).”

이유인즉, 두 개의 모듈 vboxnetflt 와 vboxdrv 이 적재 되지 않아서 발생하는 문제였다.

vboxdrv 의 경우,

$ sudo modprobe vboxdrv

의 명령어로 로딩시킬 수 있었지만, vboxnetflt 모듈의 경우에는 다음의 에러 메시지를 내며 로딩되지 않았다.

jonathan@jonathan-laptop:~$ sudo modprobe vboxnetflt
[sudo] password for jonathan:
FATAL: Error inserting vboxnetflt (/lib/modules/2.6.32-33-generic-pae/updates/dkms/vboxnetflt.ko): Invalid module format

한참을 고민하다가 해결책을 찾았는데, 너무도 간단했다.
다음의 명령어로 모듈을 업로드 할 수 있다.

$ modprobe /lib/modules/2.6.32-33-generic-pae/updates/dkms/vboxnetflt.ko

또한, 매 재부팅시 자동으로 모듈 업로드를 설정하기 위해서, /etc/modules 의 파일을 수정해야 한다.

</etc/modules>

# /etc/modules: kernel modules to load at boot time.
#
# This file contains the names of kernel modules that should be loaded
# at boot time, one per line. Lines beginning with “#” are ignored.

lp
vboxdrv
/lib/modules/2.6.32-33-generic-pae/updates/dkms/vboxnetflt.ko

 

 

 

MySQL : Project(MySQL 연동) 3 – 기초

1. MySQL 시작하기

MySQL 구동 하기.

$ /etc/init.d/mysql start

 

MySQL 접속하기

$ mysql -uroot -p1234567890

-u 다음에는 접속하고자 하는 ID, -p 다음에는 접속하고자 하는 계정의 패스워드를 입력한다.

 

MySQL에서 데이터 베이스 생성하기

mysql> create database test;

생성한 데이터 베이스 확인하기

mysql> show databases;

 

MySQL에서 데이터 베이스 선택하기

mysql> use test;

use 다음에 사용하고자 하는 데이터 베이스를 입력한다.

 

선택한 데이터베이스에서 테이블 만들기

mysql> CREATE TABLE student (
-> name varchar(10),
-> korean int,
-> math int,
-> english int
-> );

student 라는 이름의 테이블을 생성하는 쿼리이다.  테이블 생성 후, Data Directory 를 확인하면, 데이터 베이스 디렉토리 속에 student라는 이름을 가진 파일들이 생성된 것을 확인할 수 있다.

root@jonathan-laptop:/var/lib/mysql/test# pwd
/var/lib/mysql/test
root@jonathan-laptop:/var/lib/mysql/test# ls -slh|grep student
rw-rw—- 1 mysql mysql    0 2011-06-30 10:59 student.MYD
rw-rw—- 1 mysql mysql 1.0K 2011-06-30 10:59 student.MYI
rw-rw—- 1 mysql mysql 8.5K 2011-06-30 10:59 student.frm

 

생성한 테이블 확인하기

mysql> show tables;
+—————-+
| Tables_in_test |
+—————-+
| address        |
| student        |
| test           |
+—————-+

 

테이블 정보 확인하기

mysql> explain student;
+———+————-+——+—–+———+——-+
| Field   | Type        | Null | Key | Default | Extra |
+———+————-+——+—–+———+——-+
| name    | varchar(10) | YES  |     | NULL    |       |
| korean  | int(11)     | YES  |     | NULL    |       |
| math    | int(11)     | YES  |     | NULL    |       |
| english | int(11)     | YES  |     | NULL    |       |
+———+————-+——+—–+———+——-+
4 rows in set (0.00 sec)

 

2. 사용자 설정

 

2-1. 사용자 암호 바꾸기

MySQL에서 사용자의 암호를 변경하는 방법은 세가지가 있다.

– UPDATE 문을 이용
– SET PASSWORD 이용
– mysqladmin 이용

2-1-1 UPDATE 문 이용하기

jonathan@jonathan-laptop:/var/lib$ mysql -uroot -p mysql

mysql> update user set password=password(‘1234’) where user = ‘root’;
Query OK, 3 rows affected (0.09 sec)
Rows matched: 3  Changed: 3  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.03 sec)

-p 다음에 mysql은 mysql이란 데이터 베이스를 사용하라는 것을 의미한다. mysql 프롬프트에서 use db_name을 이용하여 사용하고자 하는 데이터베이스를 변경할 수도 있다. UPDATE 문을 이용하여 암호를 변경할 경우에는 꼭 flush privileges; 를 실행해야 한다.

이유는 MySQL은 속도를 조금이나마 빠르게 하기 위해 mysql 데이터베이스에 있는 사용자 권한에 관련딘 내용을 처음 MySQL 시작시에 메모리로 읽어 들이는데(메모리가 하드 디스크에 비해서 속도가 빠르므로 이렇게 해서 사용자 인증 부분에 속도를 향상시킬 수 있는 것이다), UPDATE문을 사용하여 암호가 바뀌었을 때는 flush privileges; 를 이용하여 사용자 관련 테이블에 변경된 내용이 있으니 다시 읽으라고 서버에 알려야 한다.

2-1-2 SET PASSWORD 이용하기

mysql> SET PASSWORD for root=password(‘1234’);

이 방법은 flush privileges; 를 할 필요가 없음. 그러나 MySQL 5.1 버전에서는 제대로 되지 않는것을 확인했음.

2-1-3 mysqladmin 이용하기

$ mysqladmin -u root password  new-password

 

2-2 새로운 사용자 등록하기

MySQL에서 새로운 사용자를 추가하는 방법은 GRANT를 이용하는 방법과 직접 mysql의 user 테이블에 INSERT 하는 방법 두가지가 있다.

2-2-1 GRANT문 이용

mysql> grant all privileges on *.* to pchero@localhost identified by ‘1234’ with grant option;
Query OK, 0 rows affected (0.04 sec)

localhost의 pchero에게 (to pchero@localhost) 1234라는 암호로(identified by ‘1234’) MySQL의 모든 데이터베이스에 있는 모든 테이블에(on *.*) 모든 권한 (all privileges)을 부여하는 것을 의미한다.

위의 쿼리문은 모든 데이터베이스를 사용할 수 있도록 하는 쿼리인데, 이를 특정 데이터베이스에 한정하기 위해서는 다음의 쿼리문을 사용하도록 한다.

mysql> grant all privileges on test.* to pchero@localhost identified by ‘1234’;

위의 쿼리문은 localhost의 pchero에게 test 데이터베이스의 모든 테이블에 모든 권한을 부여하는 것을 나타낸다. 특정 데이터베이스에 특정 권한만을 부여하기 위해서는 다음의 쿼리문을 수행한다.

mysql> grant select, insert on test.* to pchero@localhost identified by ‘1234’;

이 외에도 특정 테이블이나 특정 컬럼에만 권한을 주는 방법도 있다.

2-2-2 INSERT문 이용하기

insert into user values(‘localhost’, ‘pchero’, password(‘1234’), ‘N’, ‘N’, ‘N’,’N’,’N’,’N’,’N’,’N’,’N’,’N’,’N’,’N’,’N’,’N’,’N’,’N’,’N’,’N’,’N’,’N’,’N’,’N’,’N’,’N’,’N’,’N’,’N’,’N’,’N’,’N’,’N’,’N’,’N’,’N’,’N’,’N’);