본문으로 바로가기

MySQL Connector for MinGW

category Devlogs 2024. 3. 18. 15:09

이 글은 2014년 4월 10일에 작성된 글입니다. 참고로 올려봅니다.

 

요즘은 아래를 쓰면 되지 않을까..

https://github.com/mysql/mysql-connector-cpp


우선 아래의 링크에서 mysql-connector-c-6.0.2.mingw-port.tgz 파일을 받는다.

https://sourceforge.net/projects/windiana.u/files/mysql/port-mingw/

다음 압축을 풀고.. 다음과 같이 컴파일한다.

% cmake -G "MSYS Makefiles" -DCMAKE_INSTALL_PREFIX=/c/Temp/mysql-connector-c-6.0.2-mingw32
% make
% make install

 

다음의 코드로 접속 테스트 실시.

#include <stdio.h>
#include <stdlib.h>
#include "mysql.h"
 
static void change_user(MYSQL *sock,const char *user, const char *password,
            const char *db,my_bool warning)
{
  if (mysql_change_user(sock,user,password,db) != warning)
  {
    fprintf(stderr,"Couldn't change user to: user: '%s', password: '%s', db: '%s':  Error: %s\n",
        user, password ? password : "", db ? db : "",
        mysql_error(sock));
  }
}
 
int main(int argc, char **argv)
{
  MYSQL *sock;
 
  if (!(sock=mysql_init(0)))
  {
    fprintf(stderr,"Couldn't initialize mysql struct\n");
    exit(1);
  }
  mysql_options(sock,MYSQL_READ_DEFAULT_GROUP,"connect");
  if (!mysql_real_connect(sock,"1.2.3.4","root","PASSWORD",NULL,0,NULL,0))
  {
    fprintf(stderr,"Couldn't connect to engine!\n%s\n",mysql_error(sock));
    perror("");
    exit(1);
  }
  sock->reconnect= 1;
 
  if (mysql_select_db(sock,"test"))
  {
    fprintf(stderr,"Couldn't select database test: Error: %s\n",
        mysql_error(sock));
  }
 
  mysql_close(sock);
  exit(0);
  return 0;
}
% gcc -o connect_test.exe connect_test.c -I/c/Temp/mysql-connector-c-6.0.2-mingw32/include -L/c/Temp/mysql-connector-c-6.0.2-mingw32/lib -lmysqlclient -lws2_32

 

컴파일시 아래의 아래와 같은 에러가 나면..

error: unknown type name 'SOCKET'

 

mysql_com.h 파일에 다음을 추가한다.

#ifndef my_socket_defined
#if  defined(__WIN32__) && !defined(__WIN__)
#define my_socket SOCKET
#else
typedef int my_socket;
#endif /* __WIN__ */
#endif /* my_socket_defined */

 

다음과 같은 에러가 나면서 접속이 안되면..

$ ./connect_test.exe
No error
Couldn't connect to engine!
Host 'xx.xx.xx.xx' is not allowed to connect to this MySQL server

 

DB 접근 권한 문제로 localhost 로는 접근이 가능하지만 외부에서 다른 IP로 접근했을시 뜨는 오류 이므로 권한을 부여해서 해결한다.
참고로 root 의 host 값들은 localhost, 127.0.0.1 등으로 기본 등록되어 있지만,

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
 
Database changed
mysql> select host, user, password from user;
+-------------+-------+-------------------------------------------+
| host        | user  | password                                  |
+-------------+-------+-------------------------------------------+
| localhost   | root  | *xxxx |
| 127.0.0.1   | root  |                                           |
| localhost   |       |                                           |
| localhost   | mysql | *xxxxx |
+-------------+-------+-------------------------------------------+
7 rows in set (0.00 sec)

 

외부접속을 나타내는 값이 없다. 특정 아이피로 지정할 수도 있지만 여기선 % 기호로 어디서든 접속 가능하게 만든다.

mysql> use mysql;
mysql> grant all privileges on *.* to 'root'@'%' identified by 'root의 패스워드';
mysql> flush privileges;
mysql> select host, user, password from user;
+-------------+-------+-------------------------------------------+
| host        | user  | password                                  |
+-------------+-------+-------------------------------------------+
| localhost   | root  | *xxxx |
| 127.0.0.1   | root  |                                           |
| localhost   |       |                                           |
| localhost   | mysql | *xxxx |
| %           | root  | *xxxx |
+-------------+-------+-------------------------------------------+
8 rows in set (0.00 sec)
 
mysql> quit;


참고 싸이트: http://forums.mysql.com/read.php?117,425191,425191#msg-425191


Korea Tcl/Tk Community
블로그 이미지 ihmin 님의 블로그
VISITOR 오늘 / 전체