之前有使用yum安装Mysql,执行过程比较简单。今天尝试使用源码编译的方式来安装Mysql。编译器使用cmake。
选择的Mysql版本为:mysql-5.5.37,cmake的版本是cmake-2.8.10.2
下载地址是:http://mysql.mirror.kangaroot.net/Downloads/ 和 http://www.cmake.org/files/v2.8/cmake-2.8.10.2.tar.gz
环境:
[wh42@e3ddba11 mysql]$ sudo cat /etc/redhat-release
CentOS release 6.3 (Final)
[wh42@e3ddba11 mysql]$ uname -a
Linux e3ddba11 2.6.32-279.el6.x86_64 #1 SMP Fri Jun 22 12:19:21 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
下面是本次的安装步骤:
1.上传mysql-5.5.37.tar.gz和cmake-2.8.10.2.tar.gz文件到e3ddba11的/usr/local文件夹下。
2.在Centos上安装g++和ncurses_devel
[wh42@e3ddba11 local]$ sudo yum install gcc-c++
[wh42@e3ddba11 local]$ sudo yum install ncurses-devel
3.安装make
[wh42@e3ddba11 local]$ sudo yum install make
否则会报错: Cannot find appropriate Makefile processor on this system.
4.安装cmake
[wh42@e3ddba11 local]$ sudo tar -zxvf cmake-2.8.10.2.tar.gz
[wh42@e3ddba11 local]$ cd cmake-2.8.10.2
[wh42@e3ddba11 cmake-2.8.10.2]$ sudo ./configure
[wh42@e3ddba11 cmake-2.8.10.2]$ sudo make
[wh42@e3ddba11 cmake-2.8.10.2]$ sudo make install
5.将cmake加入到系统环境变量中
[wh42@e3ddba11 cmake-2.8.10.2]$ sudo vi /etc/profile
在最后添加两行;
PATH=/usr/local/cmake-2.8.10.2/bin:$PATH
export PATH
之后执行下面的代码使该修改生效。----这里不能直接执行sudo source /etc/profile会报错sudo: source: command not found
[root@e3ddba11 cmake-2.8.10.2]$ source /etc/profile
[wh42@e3ddba11 cmake-2.8.10.2]$ sudo -s
[root@e3ddba11 cmake-2.8.10.2]# source /etc/profile
此时,我们可以使用export来查看当前的PATH值
[wh42@e3ddba11 cmake-2.8.10.2]$ echo $PATH
6.创建Mysql的安装目录以及数据库文件存放的路径
[wh42@e3ddba11 cmake-2.8.10.2]$ sudo mkdir -p /usr/local/mysql
[wh42@e3ddba11 cmake-2.8.10.2]$ sudo mkdir -p /usr/local/mysql/data/
7.创建mysql用户以及对应用户组
[wh42@e3ddba11 cmake-2.8.10.2]$ sudo groupadd mysql
[wh42@e3ddba11 cmake-2.8.10.2]$ sudo useradd -r -g mysql mysql
8.编译安装mysql
[wh42@e3ddba11 cmake-2.8.10.2]$ cd /usr/local/
[wh42@e3ddba11 local]$ sudo tar -zxvf mysql-5.5.37.tar.gz
[wh42@e3ddba11 local]$ cd mysql-5.5.37
[wh42@e3ddba11 mysql-5.5.37]$
sudo cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock
-DDEFAULT_CHARSET=utf8
-DDEFAULT_COLLATION=utf8_general_ci
-DWITH_MYISAM_STORAGE_ENGINE=1
-DWITH_INNOBASE_STORAGE_ENGINE=1
-DWITH_MEMORY_STORAGE_ENGINE=1
-DWITH_READLINE=1
-DENABLED_LOCAL_INFILE=1
-DMYSQL_DATADIR=/usr/local/mysql/data
-DMYSQL_USER=mysql
-DMYSQL_TCP_PORT=3306
[wh42@e3ddba11 mysql-5.5.37]$ sudo make
[wh42@e3ddba11 mysql-5.5.37]$ sudo make install
9.验证mysql是否安装成功
[wh42@e3ddba11 mysql-5.5.37]$ cd /usr/local/mysql
[wh42@e3ddba11 mysql]$ ls
bin COPYING data docs include INSTALL-BINARY lib man mysql.sock mysql-test README scripts share sql-bench support-files
如果看到存在这些文件或者文件夹,那么证明mysql安装成功。
10.设置mysql目录的权限
[wh42@e3ddba11 mysql]$ sudo chown -R mysql:mysql /usr/local/mysql
[wh42@e3ddba11 mysql]$ sudo chown -R mysql:mysql data
11.将mysql的启动服务加入到系统服务中
[wh42@e3ddba11 mysql]$ sudo cp support-files/my-medium.cnf /etc/my.cnf
12.创建系统数据库的表
[wh42@e3ddba11 mysql]$ cd /usr/local/mysql
[wh42@e3ddba11 mysql]$ sudo scripts/mysql_install_db --user=mysql
13.设置环境变量
[wh42@e3ddba11 mysql]$ sudo vi /root/.bash_profile
修改PATH为:
PATH=$PATH:$HOME/bin:/usr/local/mysql/bin:/usr/local/mysql/lib
之后执行下面的代码使该修改生效。----这里不能直接执行sudo source /etc/profile会报错sudo: source: command not found
[wh42@e3ddba11 mysql]$ source /root/.bash_profile
[wh42@e3ddba11 mysql]$ sudo -s
[root@e3ddba11 mysql]# source /root/.bash_profile
同时也将mysql的路径加入到/etc/profile下。
[wh42@e3ddba11 mysql]$ sudo vi /etc/profile
在最后添加两行
PATH=/usr/local/mysql/bin:$PATH
export PATH
[wh42@e3ddba11 mysql]$ source /etc/profile
[wh42@e3ddba11 mysql]$ sudo -s
[root@e3ddba11 mysql]# source /etc/profile
14.启动mysql
[wh42@e3ddba11 mysql]$ cd /usr/local/mysql
[wh42@e3ddba11 mysql]$ sudo ./bin/mysqld_safe --user=mysql &
15.将mysql服务添加到系统服务中
[wh42@e3ddba11 mysql]$ sudo cp support-files/mysql.server /etc/init.d/mysql
16.启动mysql
[wh42@e3ddba11 mysql]$ sudo /etc/init.d/mysql start
Starting MySQL SUCCESS!
17.设置root密码
[wh42@e3ddba11 mysql]$ mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 5
Server version: 5.5.37-log Source distribution
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
mysql> use mysql;
Database changed
mysql> update user set password=PASSWORD('wison') where user='root';
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4 Changed: 4 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye