闲来无事,准备学习下Mysql的源代码,花了1个小时的时间,终于在VS2008中运行起来Mysql了,有些地方也是知之甚少,理解错了希望大家及时纠正,下面介绍下如何进行配置。
Step 1:
下载Mysql源代码,在http://dev.mysql.com/downloads/mysql/ 网站中,包含两种格式的Mysql,MSI Installer 和 ZIP Archive,MSI Installer是直接安装程序,ZIP Archive中包含两种大小的ZIP,从大小可以看出,较小的是源码。Install和ZIP的具体区别请参考:http://dev.mysql.com/doc/refman/5.1/en/windows-choosing-package.html。
Step 2:
别告诉我你没有VS,呃,好吧,你是外星人。
Step 3:
下载CMAKE,安装后,在CMD中测试是否安装成功。至于是干啥的,后面再讲吧。下载地址http://www.cmake.org
Step4:
下载bision,因为Mysql的词法和语法分析是使用的lex和yacc,下载地址:http://gnuwin32.sourceforge.net/packages/bison.htm
Step 5:
将下载下来的ZIP文件解压,我解压到了D:\open_src\mysql 中,CMD进入mysql目录下,执行脚本文件:
D: \> cd open_src
D: \ open_src > cd mysql
D: \ open_src \ mysql > wscript win \ configure . js WITH_INNOBASE_STORAGE_ENGINE WITH_PARTITION_STORAGE_ENGINE MYSQL_SERVER_SUFFIX = -pro
脚本后面带有三个参数,具体的参数根据自己的需要进行设置,这里选择了INNODB的存储引擎,
貌似这个很流行,提供的可选参数如下:
WITH_PARTITION_STORAGE_ENGINE
WITH_ARCHIVE_STORAGE_ENGINE
WITH_BLACKHOLE_STORAGE_ENGINE
WITH_EXAMPLE_STORAGE_ENGINE
WITH_FEDERATED_STORAGE_ENGINE
__NT__ Enable named pipe support
MYSQL_SERVER_SUFFIX =< suffix > Server suffix , default none
COMPILATION_COMMENT =< comment > Server comment , default " Source distribution "
MYSQL_TCP_PORT =< port > Server port , default 3306
CYBOZU Default character set is UTF8
EMBED_MANIFESTS Embed custom manifests into final exes , otherwise VS
default will be used . ( Note - This option should only be
used by MySQL AB .)
WITH_EMBEDDED_SERVER Configure solution to produce libmysqld . dll
and the static mysqlserver . lib
打开configure.js脚本文件,如下所示:
{
var fso = new ActiveXObject ( " Scripting.FileSystemObject " );
var args = WScript . Arguments
// read in the Unix configure . in file
var configureInTS = fso . OpenTextFile ( " configure.in " , ForReading );
var configureIn = configureInTS . ReadAll ();
configureInTS . Close ();
var default_comment = " Source distribution " ;
var default_port = GetValue ( configureIn , " MYSQL_TCP_PORT_DEFAULT " );
var actual_port = 0 ;
/**<p>输出文件mysql/win/configure.data ,此文件是运行js脚本产生的一些参数</p>*/
var configfile = fso . CreateTextFile ( " win\\configure.data " , true );
for ( i = 0 ; i < args . Count (); i ++)
{
var parts = args . Item ( i ). split ( ' = ' );
switch ( parts[ 0 ] )
{
case " CYBOZU " :
case " EMBED_MANIFESTS " :
case " EXTRA_DEBUG " :
case " WITH_EMBEDDED_SERVER " :
case " WITHOUT_MARIA_TEMP_TABLES " :
configfile . WriteLine ( " SET ( " + args . Item ( i ) + " TRUE) " );
break ;
case " MYSQL_SERVER_SUFFIX " :
case " MYSQLD_EXE_SUFFIX " :
configfile . WriteLine ( " SET ( " + parts[ 0 ] + " \" "
+ parts[ 1 ] + " \") " );
break ;
case " COMPILATION_COMMENT " :
default_comment = parts[ 1 ] ;
break ;
case " MYSQL_TCP_PORT " :
actual_port = parts[ 1 ] ;
break ;
}
}
由此可见configure.js是利用我们传入的参数和configure.in文件,生成一个configure.data文件,其内容如下所示:
SET ( COMPILATION_COMMENT " Source distribution " )
SET ( PROTOCOL_VERSION " 10 " )
SET ( DOT_FRM_VERSION " 6 " )
SET ( MYSQL_TCP_PORT_DEFAULT " 0 " )
SET ( MYSQL_TCP_PORT " 3306 " )
SET ( MYSQL_UNIX_ADDR " /tmp/mysql.sock " )
SET ( VERSION " 5.1.48 " )
SET ( MYSQL_BASE_VERSION " 5.1 " )
SET ( MYSQL_VERSION_ID " 50148 " )
SET ( WITH_INNOBASE_STORAGE_ENGINE TRUE )
SET ( WITH_PARTITION_STORAGE_ENGINE TRUE )
Step 6:
执行win目录下的build-vs9.bat批处理文件,打开这个文件,只有一条语句:
cmake -G "Visual Studio 9 2008"
cmake是一个跨平台的自动化构建系统,即根据平台和编译器生成不同的make文件(UNIX下的叫法)。
Generators
The following generators are available on this platform:
Borland Makefiles = Generates Borland makefiles .
MSYS Makefiles = Generates MSYS makefiles .
MinGW Makefiles = Generates a make file for use with
mingw32-make .
NMake Makefiles = Generates NMake makefiles .
NMake Makefiles JOM = Generates JOM makefiles .
Unix Makefiles = Generates standard UNIX makefiles .
Visual Studio 10 = Generates Visual Studio 10 project files .
Visual Studio 10 Win64 = Generates Visual Studio 10 Win64 project
files .
Visual Studio 6 = Generates Visual Studio 6 project files .
Visual Studio 7 = Generates Visual Studio . NET 2002 project
files .
Visual Studio 7 . NET 2003 = Generates Visual Studio . NET 2003 project
files .
Visual Studio 8 2005 = Generates Visual Studio . NET 2005 project
files .
Visual Studio 8 2005 Win64 = Generates Visual Studio . NET 2005 Win64
project files .
Visual Studio 9 2008 = Generates Visual Studio 9 2008 project files .
Visual Studio 9 2008 Win64 = Generates Visual Studio 9 2008 Win64 project
build-vs9.bat中CMAKE的参数是Visual Studio 9 2008,即Generates Visual Studio 9 2008 project files。Cmake执行时,会使用step 5中生成的configure.data文件,具体咋使用的我也小白了,咱就不研究了。最后会在mysql根目录下生成一个VS的解决方案文件MySql.sln。
Step 7:
设置数据目录,Mysqld启动时,首先去WINDIR中找my.ini配置文件,可以通过如下命令获取WINDIR的路径:
C: \ WINDOWS
如果没有my.ini文件,可以自己创建一个,看到如下内容:
# set basedir to your installation path
basedir = D: / mysql
# set datadir to the location of your data directory
datadir = D: / mysql / win / data
设置了两个参数,mysql的参数路径,我们通过修改这两个参数来指向我们具体的目录。
# set basedir to your installation path
basedir = D: / open_src / mysql
# set datadir to the location of your data directory
datadir = D: / open_src / mysql / win / data
如果不想自己创建的话,可以将win目录下的data目录整体拷贝到sql目录下。
step 8:
将sql目录下的share目录整体拷贝下mysql根目录下,因为SERVER启动时需要在Mysql/share目录下找一个errmsg.txt,用于记录错误信息,当然你也可以不进行整体的目录拷贝,只将errmsg.txt拷贝过去也行,但是确保是这样的路径mysql/share/errmsg.txt.
step 9:
启动mysql服务器,mysql解决方案中,服务器是mysqld项目,客户端是mysql项目,分别编译这两个项目。在编译sql_locale.cc文件时,会出现ERROR,用ULTRA EDIT打开sql_locale.cc文件,另存为UTF-8格式,覆盖原文件即可。使用VS启动服务器mysqld,使用命令行启动客户端mysql:
Enter password:
Welcome to the MySQL monitor . Commands end with ; or \ g .
Your MySQL connection id is 1
Server version: 5.1 . 48 -pro- debug Source distribution
mysql > show databases ;
+ -------------------- +
| Database |
+ -------------------- +
| information_schema |
| mysql |
| test |
+ -------------------- +
3 rows in set ( 0.08 sec )