Jun 30, 2008

cygwin 上使用 Mysql

如果你是shell指令愛好者,非常喜歡用shell下指令玩Rails的話,除了Mac, Linux之外,最好的選擇就是在Win32平台上安裝Cygwin

安裝Cygwin有什麼好處呢?這可以讓你在Win32底下使用一些常見的Linux工具程式,例如: joe, nano, vim, emacs, mc, gcc, make等,其他像top, ls, man, ps, mv等常見的指令也都相容。

Windows/CygWin
If you are a user of Cygwin (the Redhat distribution) you already know, it contains a nicely running perl 5.6.1, installation of additional modules usually works as a charm via the standard procedure of
- perl makefile.PL
- make
- make test
- make install
The Windows binary distribution of MySQL runs smoothly under Cygwin. You can start/stop the server and use all Windows clients without problem. But to install DBD::mysql you have to take a little special action.
Don't attempt to build DBD::mysql against either the MySQL Windows or Linux/Unix BINARY distributions: neither will work!
You MUST compile the MySQL clients yourself under Cygwin, to get a 'libmysqlclient.a' compiled under Cygwin. Really! You'll only need that library and the header files, you don't need any other client parts. Continue to use the Windows binaries. And don't attempt (currently) to build the MySQL Server part, it is unnecessary, as MySQL AB does an excellent job to deliver optimized binaries for the mainstream operating systems, and it is told, that the server compiled under Cygwin is unstable.
Install MySQL (if you havn't already)
- download the MySQL Windows Binaries from http://www.mysql.com/downloads/index.html
- unzip mysql--win.zip into some temporary location
- start the setup.exe there and follow the instructions
- start the server
- alternatively download, install and start the server on a remote server, on what supported OS ever Build MySQL clients under Cygwin:
- download the MySQL LINUX source from http://www.mysql.com/downloads/index.html
- unpack mysql-XX.tar.gz into some tmp location
- cd into the unpacked dir mysql-
./configure --prefix=/usr/local/mysql --enable-server
#./configure --prefix=/usr/local/mysql --without-server
This prepares the Makefile with the installed Cygwin features. It takes some time, but should finish without error. The 'prefix', as given, installs the whole Cygwin/MySQL thingy into a location not normally in your PATH, so that you continue to use already installed Windows binaries. The --without-server parameter tells configure to only build the clients.
- make :This builds all MySQL client parts ... be patient. It should finish finally without any error.
- make install :This installs the compiled client files under /usr/local/mysql/. Remember, you don't need anything except the library under /usr/local/mysql/lib and the headers under /usr/local/mysql/include! Essentially you are now done with this part. If you want, you may try your compiled binaries shortly; for that, do:
- cd /usr/local/mysql/bin
mysql.exe -h 127.0.0.1 -u root -p

The host (-h) parameter 127.0.0.1 targets the local host, but forces the mysql client to use a TCP/IP connection. The default would be a pipe/socket connection (even if you say '-h localhost') and this doesn't work between Cygwin and Windows (as far as I know).
If you have your MySQL server running on some other box, then please substitute '127.0.0.1' with the name or IP-number of that box.
At the 'mysql>' prompt do a quick check:
mysql> use mysql
mysql> show tables;
mysql> select * from db;
mysql> exit
You are now ready to build DBD::mysql!
Build DBD::mysql:
- download DBD-mysql-X.tar.gz from CPAN
- unpack DBD-mysql-X.tar.gz
- cd into unpacked dir DBD-mysql- you probably did that already, if you are reading this!
- cp /usr/local/mysql/bin/mysql_config .
This copies the executable script mentioned in the DBD::mysql docs from your just built Cywin/MySQL client directory; it knows about your Cygwin installation, especially about the right libraries to link with.
- perl Makefile.PL --testhost=127.0.0.1 --testuser=root --testpassword=xxxx
The --testhost=127.0.0.1 parameter again forces a TCP/IP connection to the MySQL server on the local host instead of a pipe/socket connection for the 'make test' phase.
- make :This should run without error
- make test :with DBD-mysql-2.1022 or earlier you will see several errors in dbdadmin.t, mysql.t and mysql2.t; with later versions you should not get errors (except possibly one, indicating, that some tables could not be dropped. I'm hunting for a solution to that problem, but have none yet).
- make install :This installs DBD::mysql into the Perl hierarchy.
Notes:
This was tested with MySQL version 3.23.54a and DBD::mysql version 2.1022. I patched the above mentioned test scripts and sent the patches to the author of DBD::mysql Jochen Wiedman.


--- If you try to install MySQL with wrong version(Must be source) , you will get return message like this.
./bin/my_print_defaults: ./bin/my_print_defaults: cannot execute binary file
Neither host 'brian_chang' nor 'localhost' could be looked up with
./bin/resolveip
Please configure the 'hostname' command to return a correct
hostname.
If you want to solve this at a later stage, restart this script
with the --force option

0 意見: