安装方式分为rpm和源码编译安装两种,本文是采用mysql源码编译方式,编译器使用Cmake。软件需要mysql-5.5.29.tar.gz和cmake-2.8.10.2.tar.gz,请自行下载。
下载地址:
http://mysql.mirror.kangaroot.net/Downloads/
http://www.cmake.org/files/v2.8/cmake-2.8.10.2.tar.gz
其中mysql使用最新的稳定版本,即最新试用版的上一个版本,且非rc或者alpha的版本,Cmake直接用的最新版。
1.上传mysql-5.5.29.tar.gz和cmake-2.8.10.2.tar.gz到/usr/local文件夹下。
2.CentOS安装g++和ncurses-devel
[[email protected] local]# yum install gcc-c++
[[email protected] local]# yum install ncurses-devel
3.cmake的安装
[[email protected]]# tar -zxv -f cmake-2.8.10.2.tar.gz // 解压压缩包
[[email protected] local]# cd cmake-2.8.10.2
[[email protected] cmake-2.8.10.2]# ./configure
[[email protected] cmake-2.8.10.2]# make
[[email protected] cmake-2.8.10.2]# make install
4.将cmake永久加入系统环境变量
用vi在文件/etc/profile文件中增加变量,使其永久有效,
[[email protected] local]# vi /etc/profile
在文件末尾追加以下两行代码:
PATH=/usr/local/cmake-2.8.10.2/bin:$PATH
export PATH
执行以下代码使刚才的修改生效:
[[email protected] local]# source /etc/profile
用 export 命令查看PATH值
[[email protected] local]# echo $PATH
5.创建mysql的安装目录及数据库存放目录
[[email protected]]# mkdir -p /usr/local/mysql //安装mysql
[[email protected]]# mkdir -p /usr/local/mysql/data //存放数据库
6.创建mysql用户及用户组
[[email protected]] groupadd mysql
[[email protected]] useradd -r -g mysql mysql
7.编译安装mysql
[[email protected] local]# tar -zxv -f mysql-5.5.29.tar.gz //解压
[[email protected] local]# cd mysql-5.5.29
[[email protected] mysql-5.5.29]#
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
[[email protected] mysql-5.5.29]# make
[[email protected] mysql-5.5.29]# make install
8.检验是否安装成功
[[email protected] mysql-5.5.29]# cd /usr/local/mysql/
[[email protected] mysql]# ls
bin COPYING data docs include INSTALL-BINARY lib man mysql-test README scripts share sql-bench support-files
有bin等以上文件的话,恭喜你已经成功安装了mysql。
配置mysql
9.设置mysql目录权限
[[email protected] mysql]# cd /usr/local/mysql //把当前目录中所有文件的所有者设为root,所属组为mysql
[[email protected] mysql]# chown -R root:mysql .
[[email protected] mysql]# chown -R mysql:mysql data
10.将mysql的启动服务添加到系统服务中
[[email protected] mysql]# cp support-files/my-medium.cnf /etc/my.cnf
cp:是否覆盖"/etc/my.cnf"? y
11.创建系统数据库的表
[[email protected] mysql]# cd /usr/local/mysql
[[email protected] mysql]# scripts/mysql_install_db --user=mysql
12.设置环境变量
[[email protected] ~]# vi /root/.bash_profile
在修改PATH=$PATH:$HOME/bin为:
PATH=$PATH:$HOME/bin:/usr/local/mysql/bin:/usr/local/mysql/lib
[[email protected] ~]# source /root/.bash_profile //使刚才的修改生效
13.手动启动mysql
[[email protected] ~]# cd /usr/local/mysql
[[email protected] mysql]# ./bin/mysqld_safe --user=mysql & //启动MySQL,但不能停止
mysqladmin -u root -p shutdown //此时root还没密码,所以为空值,提示输入密码时,直接回车即可。
14.将mysql的启动服务添加到系统服务中
[[email protected] mysql]# cp support-files/mysql.server /etc/init.d/mysql
15.启动mysql
[[email protected] mysql]# service mysql start
Starting MySQL... ERROR! The server quit without updating PID file (/usr/local/mysql/data/localhost.localdomain.pid).
启动失败:
我这里是权限问题,先改变权限
[[email protected] mysql]# chown -R mysql:mysql /usr/local/mysql
接着启动服务器
[[email protected] mysql]# /etc/init.d/mysql start
16.修改MySQL的root用户的密码以及打开远程连接
[[email protected] mysql]# mysql -u root mysql
mysql> use mysql;
mysql> desc user;
mysql> GRANT ALL PRIVILEGES ON *.* TO [email protected]"%" IDENTIFIED BY "root"; //为root添加远程连接的能力
mysql> update user set Password = password(‘123456‘) where User=‘root‘; //设置root用户密码
mysql> select Host,User,Password from user where User=‘root‘;
mysql> flush privileges;
mysql> exit
17.重新登录
[[email protected] mysql]# mysql -u root -p
Enter password:123456
若还不能进行远程连接,关闭防火墙
[[email protected]]# /etc/rc.d/init.d/iptables stop