一.准备工作
1.yum安装各个依赖包
[root@WebServer ~]# yum -y install gcc gcc-devel gcc-c++ gcc-c++-devel libaio-devel boost boost-devel autoconf* automake* zlib* libxml* ncurses-devel ncurses libgcrypt* libtool* cmake openssl openssl-devel bison bison-devel unzip
2.新建mysql组和用户
[root@WebServer ~]# groupadd mysql [root@WebServer ~]# useradd -g mysql -s /sbin/nologin mysql
3.目录准备
[root@WebServer ~]# mkdir -p /webserver/mysql/etc [root@WebServer ~]# mkdir -p /webserver/mysql/data [root@WebServer ~]# mkdir -p /webserver/mysql/tmp [root@WebServer ~]# chown -R mysql.mysql /webserver/mysql
4.解压MySQL并进入安装目录
[root@WebServer ~]# tar zxvf mysql-5.6.16.tar.gz [root@WebServer ~]# cd mysql-5.6.16
二.编译并安装
1.编译MySQL
[root@WebServer mysql-5.6.16]# cmake -DCMAKE_INSTALL_PREFIX=/webserver/mysql \ -DSYSCONFDIR=/webserver/mysql/etc \ -DMYSQL_DATADIR=/webserver/mysql/data \ -DMYSQL_TCP_PORT=3306 \ -DMYSQL_UNIX_ADDR=/webserver/mysql/tmp/mysqld.sock \ -DMYSQL_USER=mysql \ -DEXTRA_CHARSETS=all \ -DWITH_READLINE=1 \ -DWITH_SSL=system \ -DWITH_EMBEDDED_SERVER=1 \ -DENABLED_LOCAL_INFILE=1 \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DENABLE_DOWNLOADS=1 \ -DDEFAULT_CHARSET=utf8 \ -DDEFAULT_COLLATION=utf8_general_ci
2.如果出现以下错误
则需要
[root@WebServer mysql-5.6.16]# cd source_downloads/ [root@WebServer source_downloads]# unzip gmock-1.6.0.zip [root@WebServer source_downloads]# cd gmock-1.6.0 [root@WebServer gmock-1.6.0]# ./configure [root@WebServer gmock-1.6.0]# make [root@WebServer gmock-1.6.0]# cd ../..
然后删除mysql-5.6.16目录下的CMakeCache.txt重新使用cmake进行编译安装
2.安装
[root@WebServer mysql-5.6.16]# make && make install
编译过程会比较漫长,编译时间取决于机器的性能
三.MySQL配置
1.客户端库文件的链接(未执行此步骤的话,在PHP编译时会提示找不到MySQL文件)
[root@WebServer mysql-5.6.16]# ln -s /webserver/mysql/lib/libmysqlclient.so.18 /usr/lib64/libmysqlclient.so.18
ps:我使用的是64位CentOS,如果使用的是32位请使用ln -s /webserver/mysql/lib/libmysqlclient.so.18 /usr/lib/libmysqlclient.so.18
2.MySQL启动文件的配置
[root@WebServer mysql-5.6.16]# cp support-files/mysql.server /etc/init.d/mysqld [root@WebServer mysql-5.6.16]# chmod +x /etc/init.d/mysqld [root@WebServer mysql-5.6.16]# vim /etc/init.d/mysqld basedir=/webserver/mysql datadir=/webserver/mysql/data //修改以上这2行的路径即可
3.设置MySQL配置文件
[root@WebServer mysql-5.6.16]# cp support-files/my-default.cnf /webserver/mysql/etc/my.cnf [root@WebServer mysql-5.6.16]# rm -rf /etc/my.cnf [root@WebServer mysql-5.6.16]# ln -s /webserver/mysql/etc/my.cnf /etc/my.cnf
4.初始化数据库
[root@WebServer mysql-5.6.16]# cd /webserver/mysql [root@WebServer mysql]# ./scripts/mysql_install_db --user=mysql --basedir=/webserver/mysql --datadir=/webserver/mysql/data
5.链接MySQL命令(这一步可以让我们方便很多,可以不用写出命令的路径,也可以写入/etc/profile的PATH中)
[root@WebServer ~]# cd /usr/sbin [root@WebServer sbin]# ln -s /webserver/mysql/bin/* .
6.启动MySQL并设置为开机启动
[root@WebServer sbin]# service mysqld start ;chkconfig mysqld on Starting MySQL.. SUCCESS!
7.设置MySQL root用户的密码
[root@WebServer ~]# mysqladmin -u root password 123456
这里将MySQL的root密码设置为123456,强烈不建议将数据库的密码设的简单
8.删除用户密码为空的用户
[root@WebServer ~]# mysql -u root -p Enter password: mysql> use mysql; mysql> select Host,User,Password from user; +-----------+------+-------------------------------------------+ | Host | User | Password | +-----------+------+-------------------------------------------+ | localhost | root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | | webserver | root | | | 127.0.0.1 | root | | | ::1 | root | | | localhost | | | | webserver | | | +-----------+------+-------------------------------------------+ 6 rows in set (0.00 sec) mysql> delete from user where password=""; Query OK, 5 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> select Host,User,Password from user; +-----------+------+-------------------------------------------+ | Host | User | Password | +-----------+------+-------------------------------------------+ | localhost | root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | +-----------+------+-------------------------------------------+ 1 row in set (0.00 sec) mysql> exit;