CentOS 5下源码安装Mysql 5.5

首先要说明一点的是CentOS上安装mysql极其简单,只需要一条命令yum install mysql就帮我们搞定了。但是如果你想深入学习和了解的话,就必须开始进行折腾,通过源代码的方式进行安装,可以帮助我们更深入的学习整个过程。从中掌握更多的细节。在安装之前,最好看一下官方文档。因为官方文档比较权威,对各个细节都描述的很好。

Installing MySQL from Source 

在mysql 5.5这个版本中,采取了cmake来进行编译。而在过去的mysql 5.1版本里面是直接configure。那么首先来介绍一下整个安装需要的步骤。

  • 编译前对系统环境准备
  • 使用CMake进行编译
  • 安装完成之后对mysql进行配置
  • 设置开机启动

1.编译前对系统环境准备

首先要安装就得去官网上下载介质,首先我们去下载。然后选择5.5.30,Source Code。

然后拖到最下面选择Generic Linux (Architecture Independent), Compressed TAR Archive进行下载。下载完成之后解压即可。然后因为是需要cmake来编译的,我们还要去安装cmake,因为我是centos的系统,所以我只需要配置一个源就能通过yum进行安装了。所以我选择了使用163的镜像源来安装cmake。

[root@mysql ~]# mv /etc/yum.repos.d/CentOS-Base.repo  /etc/yum.repos.d/CentOS-Base.repo.bak
[root@mysql ~]# wget http://mirrors.163.com/.help/CentOS5-Base-163.repo
--2013-03-06 16:39:31--  http://mirrors.163.com/.help/CentOS5-Base-163.repo
Resolving mirrors.163.com... 123.58.173.106, 123.58.173.89
Connecting to mirrors.163.com|123.58.173.106|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2341 (2.3K) [application/octet-stream]
Saving to: `CentOS5-Base-163.repo'
100%[===============================================================================================================================================================>] 2,341       --.-K/s   in 0.03s   
2013-03-06 16:39:31 (76.6 KB/s) - `CentOS5-Base-163.repo' saved [2341/2341]

[root@mysql ~]# mv CentOS5-Base-163.repo /etc/yum.repos.d/
[root@mysql ~]# yum makecache
Loaded plugins: fastestmirror, security
Loading mirror speeds from cached hostfile
addons                                                   | 1.9 kB     00:00     
addons/filelists_db                                      |  568 B     00:00     
addons/other_db                                          |  546 B     00:00     
addons/primary_db                                        | 1.1 kB     00:00     
base                                                     | 1.1 kB     00:00     
base/filelists                                                                                                                                                                     | 3.6 MB     00:02     
base/other                                                                                                                                                                         |  13 MB     00:07     
extras                                                                                                                                                                             | 2.1 kB     00:00     
extras/filelists_db                                                                                                                                                                | 227 kB     00:00     
extras/other_db                                                                                                                                                                    | 399 kB     00:00     
updates                                                                                                                                                                            | 1.9 kB     00:00     
updates/filelists_db                                                                                                                                                               | 698 kB     00:00     
updates/other_db                                                                                                                                                                   | 2.3 MB     00:01     
base                                                                                                                                                                                                    3641/3641
base                                                                                                                                                                                                    3641/3641
Metadata Cache Created

[root@mysql ~]# yum install cmake
Loaded plugins: fastestmirror, security
Loading mirror speeds from cached hostfile
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package cmake.x86_64 0:2.6.4-5.el5.4 set to be updated
--> Processing Dependency: libxmlrpc.so.3()(64bit) for package: cmake
--> Processing Dependency: libxmlrpc_util.so.3()(64bit) for package: cmake
--> Processing Dependency: libxmlrpc_client.so.3()(64bit) for package: cmake
--> Running transaction check
---> Package xmlrpc-c.x86_64 0:1.16.24-1206.1840.4.el5 set to be updated
---> Package xmlrpc-c-client.x86_64 0:1.16.24-1206.1840.4.el5 set to be updated
--> Finished Dependency Resolution

Dependencies Resolved

=====================================================================================================================================================================================================
 Package                                              Arch                                        Version                                              Repository                              Size
=====================================================================================================================================================================================================
Installing:
 cmake                                                x86_64                                      2.6.4-5.el5.4                                        base                                    6.5 M
Installing for dependencies:
 xmlrpc-c                                             x86_64                                      1.16.24-1206.1840.4.el5                              base                                    170 k
 xmlrpc-c-client                                      x86_64                                      1.16.24-1206.1840.4.el5                              base                                    26  k

Transaction Summary
=====================================================================================================================================================================================================
Install       3 Package(s)
Upgrade       0 Package(s)

Total download size: 6.7 M
Is this ok [y/N]: y
Downloading Packages:
(1/3): xmlrpc-c-client-1.16.24-1206.1840.4.el5.x86_64.rpm                                                                                                                      |  26 kB     00:00     
(2/3): xmlrpc-c-1.16.24-1206.1840.4.el5.x86_64.rpm                                                                                                                             | 170 kB     00:00     
(3/3): cmake-2.6.4-5.el5.4.x86_64.rpm                                                                                                                                          | 6.5 MB     00:03     
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                                          1.4 MB/s | 6.7 MB     00:04     
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing     : xmlrpc-c                                                                                                                                                                                   1/3 
  Installing     : xmlrpc-c-client                                                                                                                                                                            2/3 
  Installing     : cmake                                                                                                                                                                                      3/3 

Installed:
  cmake.x86_64 0:2.6.4-5.el5.4                                                                                                                                                                                    

Dependency Installed:
  xmlrpc-c.x86_64 0:1.16.24-1206.1840.4.el5                                                            xmlrpc-c-client.x86_64 0:1.16.24-1206.1840.4.el5                                                           

Complete!

[root@mysql ~]# cmake -version
cmake version 2.6-patch 4

在安装完cmake之后,我们还需要建立mysql组和用户及mysql安装目录。这里我建的安装目录是/usr/local/mysql,数据存放目录为/usr/local/mysql/data

[root@mysql ~]# groupadd mysql
[root@mysql ~]# useradd -g mysql mysql
[root@mysql ~]# mkdir -p /usr/local/mysql/data

2.使用CMake进行编译
用CMake进行编译之前,建议阅读一下Autotools to CMake Transition Guide。这篇文档详细的介绍了各种编译可以设置的选项,并且和以前的configure模式做了对比,你可以根据你的需求编译出你所需要功能的mysql。然后进行安装。这个也就是我们折腾的“意义”所在。这也是mysql灵活性的一种展示吧。

[root@mysql tmp]#tar -xvf mysql-5.5.30.tar
[root@mysql tmp]# cd mysql-5.5.30
[root@mysql mysql-5.5.30]#cmake . \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DMYSQL_UNIX_ADDR=/tmp/mysqld.sock \
-DWITH_DEBUG=0 \
-DWITH_INNOBASE_STORAGE_ENGINE=1

在执行编译的过程中遇到了个问题,错误如下。google了一下,是以为没安装ncurses-devel包的缘故,这个好说。直接yum就行了

-- Could NOT find Curses  (missing:  CURSES_LIBRARY CURSES_INCLUDE_PATH)
CMake Error at cmake/readline.cmake:83 (MESSAGE):
  Curses library not found.  Please install appropriate package,

      remove CMakeCache.txt and rerun cmake.On Debian/Ubuntu, package name is libncurses5-dev, on Redhat and derivates it is ncurses-devel.
Call Stack (most recent call first):
  cmake/readline.cmake:127 (FIND_CURSES)
  cmake/readline.cmake:217 (MYSQL_USE_BUNDLED_LIBEDIT)
  CMakeLists.txt:269 (MYSQL_CHECK_READLINE)


-- Configuring incomplete, errors occurred!
[root@mysql ~]# yum install ncurses-devel
Loaded plugins: fastestmirror, security
Loading mirror speeds from cached hostfile
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package ncurses-devel.i386 0:5.5-24.20060715 set to be updated
---> Package ncurses-devel.x86_64 0:5.5-24.20060715 set to be updated
--> Finished Dependency Resolution

Dependencies Resolved

===================================================================================================================================================================================================
 Package                                               Arch                                           Version                                                  Repository                                    Size
===================================================================================================================================================================================================
Installing:
 ncurses-devel                                         i386                                           5.5-24.20060715                                          base                     1.6 M
 ncurses-devel                                         x86_64                                         5.5-24.20060715                                          base                     1.7 M

Transaction Summary
===================================================================================================================================================================================================
Install       2 Package(s)
Upgrade       0 Package(s)

Total download size: 3.3 M
Is this ok [y/N]: y
Downloading Packages:
(1/2): ncurses-devel-5.5-24.20060715.i386.rpm                                                                                                                                  | 1.6 MB     00:05     
(2/2): ncurses-devel-5.5-24.20060715.x86_64.rpm                                                                                                                                | 1.7 MB     00:04     
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                                 254 kB/s | 3.3 MB     00:13     
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing     : ncurses-devel                                                                                                                                                                              1/2 
  Installing     : ncurses-devel                                                                                                                                                                              2/2 

Installed:
  ncurses-devel.i386 0:5.5-24.20060715                                                                   ncurses-devel.x86_64 0:5.5-24.20060715                                                                  
Complete!

再一次尝试运行CMake,在运行之前需要rm -rf CMakeCache.txt这个文件。结果又一次报错,悲剧啊。又google了一下,发现是gcc-c++没安装。因为mysql是c和c++写的。所以这里我们需要安装c++

[root@mysql ~]# rm -rf  CMakeCache.txt
CMake Error at /usr/share/cmake/Modules/CMakeCXXInformation.cmake:17 (GET_FILENAME_COMPONENT):
  get_filename_component called with incorrect number of arguments
Call Stack (most recent call first):
  CMakeLists.txt:3 (PROJECT)
  
[root@mysql mysql-5.5.30]# rpm -qa | grep gcc-c++
[root@mysql mysql-5.5.30]# yum install gcc-c++
Loaded plugins: fastestmirror, security
Loading mirror speeds from cached hostfile
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package gcc-c++.x86_64 0:4.1.2-54.el5 set to be updated
--> Processing Dependency: libstdc++-devel = 4.1.2-54.el5 for package: gcc-c++
--> Running transaction check
---> Package libstdc++-devel.x86_64 0:4.1.2-54.el5 set to be updated
--> Finished Dependency Resolution

Dependencies Resolved

=================================================================================================================================================================================================
 Package                                                 Arch                                           Version                                                Repository                    Size
=================================================================================================================================================================================================
Installing:
 gcc-c++                                                 x86_64                                         4.1.2-54.el5                                           base                         3.8 M
Installing for dependencies:
 libstdc++-devel                                         x86_64                                         4.1.2-54.el5                                           base                         2.8 M

Transaction Summary
==================================================================================================================================================================================================
Install       2 Package(s)
Upgrade       0 Package(s)

Total download size: 6.6 M
Is this ok [y/N]: y
Downloading Packages:
(1/2): libstdc++-devel-4.1.2-54.el5.x86_64.rpm                                                                                                                                  | 2.8 MB     00:10     
(2/2): gcc-c++-4.1.2-54.el5.x86_64.rpm                                                                                                                                          | 3.8 MB     00:11     
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                                  264 kB/s | 6.6 MB     00:25     
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing     : libstdc++-devel                                                                                                                                                                            1/2 
  Installing     : gcc-c++                                                                                                                                                                                    2/2 

Installed:
  gcc-c++.x86_64 0:4.1.2-54.el5                                                                                                                                                                                   

Dependency Installed:
  libstdc++-devel.x86_64 0:4.1.2-54.el5                                                                                                                                                                           

Complete!

接下来就很顺利啦。然后我们就可以make和安装了。

[root@mysql mysql-5.5.30]# make
[root@mysql mysql-5.5.30]# make install

3.安装完成之后对mysql进行配置
安装完成之后,我们就可以初始化数据库了,这个过程也有一些参数可以设置,然后主要是调用mysql_install_db这个脚本去执行一些sql。

[root@mysql mysql-5.5.30]# chown -R mysql:mysql /usr/local/mysql/
[root@mysql local]# /usr/local/mysql/scripts/mysql_install_db \
> --user=mysql \
> --basedir=/usr/local/mysql \
> --datadir=/usr/local/mysql/data \
> --no-defaults
Installing MySQL system tables...
OK
Filling help tables...
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

/usr/local/mysql/bin/mysqladmin -u root password 'new-password'
/usr/local/mysql/bin/mysqladmin -u root -h mysql password 'new-password'

Alternatively you can run:
/usr/local/mysql/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:
cd /usr/local/mysql ; /usr/local/mysql/bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl
cd /usr/local/mysql/mysql-test ; perl mysql-test-run.pl

Please report any problems with the /usr/local/mysql/scripts/mysqlbug script!

做完这一个步骤后,我们需要配置mysql的配置文件my.cnf,配置了这个文件之后,我们就不需要在mysql启动的时候输入一大堆参数了。我这里主要是加了几个目录。如果要详细了解这块,请参考官方文档。配置完成之后,我们就可以使用mysqld_safe进行后台启动了。只要看到3306端口被占用,既证明了配置成功。可以正常启动mysql。

[root@mysql mysql]# cp /usr/local/mysql/support-files/my-medium.cnf  /etc/my.cnf 
[root@mysql mysql]# vi /etc/my.cnf 
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
user=mysql

[root@mysql bin]# ./mysqld_safe --user=mysql &
[1] 17705
[root@mysql bin]# 130306 21:57:19 mysqld_safe Logging to '/usr/local/mysql/data/mysql.err'.
130306 21:57:19 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data

[root@mysql ~]# tail -200f /usr/local/mysql/data/mysql.err
130306 21:57:19 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data
130306 21:57:19 InnoDB: The InnoDB memory heap is disabled
130306 21:57:19 InnoDB: Mutexes and rw_locks use GCC atomic builtins
130306 21:57:19 InnoDB: Compressed tables use zlib 1.2.3
130306 21:57:19 InnoDB: Initializing buffer pool, size = 128.0M
130306 21:57:19 InnoDB: Completed initialization of buffer pool
130306 21:57:19 InnoDB: highest supported file format is Barracuda.
InnoDB: Log scan progressed past the checkpoint lsn 48941
130306 21:57:19  InnoDB: Database was not shut down normally!
InnoDB: Starting crash recovery.
InnoDB: Reading tablespace information from the .ibd files...
InnoDB: Restoring possible half-written data pages from the doublewrite
InnoDB: buffer...
InnoDB: Doing recovery: scanned up to log sequence number 1595675
130306 21:57:19  InnoDB: Starting an apply batch of log records to the database...
InnoDB: Progress in percents: 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 
InnoDB: Apply batch completed
130306 21:57:19  InnoDB: Waiting for the background threads to start
130306 21:57:20 InnoDB: 5.5.30 started; log sequence number 1595675
130306 21:57:20 [Note] Server hostname (bind-address): '0.0.0.0'; port: 3306
130306 21:57:20 [Note]   - '0.0.0.0' resolves to '0.0.0.0';
130306 21:57:20 [Note] Server socket created on IP: '0.0.0.0'.
130306 21:57:20 [Note] Event Scheduler: Loaded 0 events
130306 21:57:20 [Note] /usr/local/mysql/bin/mysqld: ready for connections.
Version: '5.5.30-log'  socket: '/tmp/mysqld.sock'  port: 3306  Source distribution

[root@mysql ~]# ps -ef | grep 3306
mysql    17995 17705  0 21:57 pts/1    00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/usr/local/mysql/data/mysql.err --pid-file=/usr/local/mysql/data/mysql.pid --socket=/tmp/mysqld.sock --port=3306
root     18017 12588  0 21:58 pts/2    00:00:00 grep 3306

4.设置开机启动
最后一个步骤是设置开机启动,这个也比较简单。首先把mysql的服务添加到系统服务当中,然后给它所有用户的可执行权限,然后把这个系统服务添加到chkconfig下面,并设置启动级别为3和5。
[root@mysql bin]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@mysql bin]# chmod a+x /etc/init.d/mysqld
[root@mysql bin]# chkconfig –add mysqld
[root@mysql bin]# chkconfig –level 35 mysqld on
Mysql的源码安装就完成了!其实不算很折腾,当时折腾fedora的时候,可是没日没夜折腾啊,特别是在fedora上安装wine QQ,比这麻烦多了!后来又折腾Office,折腾的次数多了也就习惯了。

分享到: 更多

Post a Comment

Your email is never published nor shared. Required fields are marked *