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

 

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

 

 

 

MySQL : Project(MySQL 연동) 2 – 환경 구성

 

1.  MySQL 설치

작업의 대부분은 Ubuntu – 10.04 버전에서 진행하였다.

Ubuntu Linux의 경우 MySQL 은 기본적으로 설치가 되어있으나, 다른 Linux 배포판의 경우, 설치가 안되어 있거나 따로 설치를 해야할 경우가 있다.

이럴 경우, 대부분의 최신의 배포판들은 다음의 명령어를 사용하여 간단하게 설치를 진행할 수 있다.

apt-get 을 이용할 경우

# apt-get install mysql

yum 을 이용할 경우

# yum install mysql

이번 프로젝트에서 사용한 MySQL의 버전은 5.1.41 버전이다.

MySQL 버전 확인하기

# mysql –version

 

2. MySQL Options

ubuntu 환경에서 apt-get 으로 MySQL을 설치 했을 경우, 기본적으로 설정되는 database directory는 다음과 같다.

/var/lib/mysql

만약 위의 디렉토리에 존재하지 않는다면, /etc/mysql/my.cnf 파일을 통해서 database directory 의 위치를 확인할 수 있다.

아래는 필자의 my.cnf 파일의 내용이다.

#
# The MySQL database server configuration file.
#
# You can copy this to one of:
# – “/etc/mysql/my.cnf” to set global options,
# – “~/.my.cnf” to set user-specific options.
#
# One can use all long options that the program supports.
# Run program with –help to get a list of available options and with
# –print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

# This will be passed to all mysql clients
# It has been reported that passwords should be enclosed with ticks/quotes
# escpecially if they contain “#” chars…
# Remember to edit /etc/mysql/debian.cnf when changing the socket location.
[client]
port        = 3306
socket        = /var/run/mysqld/mysqld.sock

# Here is entries for some specific programs
# The following values assume you have at least 32M ram

# This was formally known as [safe_mysqld]. Both versions are currently parsed.
[mysqld_safe]
socket        = /var/run/mysqld/mysqld.sock
nice        = 0

[mysqld]
#
# * Basic Settings
#

#
# * IMPORTANT
#   If you make changes to these settings and your system uses apparmor, you may
#   also need to also adjust /etc/apparmor.d/usr.sbin.mysqld.
#

user        = mysql
socket        = /var/run/mysqld/mysqld.sock
port        = 3306
basedir        = /usr
datadir        = /var/lib/mysql
tmpdir        = /tmp
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address        = 127.0.0.1
#
# * Fine Tuning
#
key_buffer        = 16M
max_allowed_packet    = 16M
thread_stack        = 192K
thread_cache_size       = 8
# This replaces the startup script and checks MyISAM tables if needed
# the first time they are touched
myisam-recover         = BACKUP
#max_connections        = 100
#table_cache            = 64
#thread_concurrency     = 10
#
# * Query Cache Configuration
#
query_cache_limit    = 1M
query_cache_size        = 16M
#
# * Logging and Replication
#
# Both location gets rotated by the cronjob.
# Be aware that this log type is a performance killer.
# As of 5.1 you can enable the log at runtime!
#general_log_file        = /var/log/mysql/mysql.log
#general_log             = 1

log_error                = /var/log/mysql/error.log

# Here you can see queries with especially long duration
#log_slow_queries    = /var/log/mysql/mysql-slow.log
#long_query_time = 2
#log-queries-not-using-indexes
#
# The following can be used as easy to replay backup logs or for replication.
# note: if you are setting up a replication slave, see README.Debian about
#       other settings you may need to change.
#server-id        = 1
#log_bin            = /var/log/mysql/mysql-bin.log
expire_logs_days    = 10
max_binlog_size         = 100M
#binlog_do_db        = include_database_name
#binlog_ignore_db    = include_database_name
#
# * InnoDB
#
# InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/.
# Read the manual for more InnoDB related options. There are many!
#
# * Security Features
#
# Read the manual, too, if you want chroot!
# chroot = /var/lib/mysql/
#
# For generating SSL certificates I recommend the OpenSSL GUI “tinyca”.
#
# ssl-ca=/etc/mysql/cacert.pem
# ssl-cert=/etc/mysql/server-cert.pem
# ssl-key=/etc/mysql/server-key.pem

[mysqldump]
quick
quote-names
max_allowed_packet    = 16M

[mysql]
#no-auto-rehash    # faster start of mysql but no tab completition

[isamchk]
key_buffer        = 16M

#
# * IMPORTANT: Additional settings that can override those from this file!
#   The files must end with ‘.cnf’, otherwise they’ll be ignored.
#
!includedir /etc/mysql/conf.d/

각각의 옵션들의 정확한 의미와 사용법을 알고 싶다면 아래의 링크를 참조한다.

http://dev.mysql.com/doc/refman/5.1/en/program-options.html