centos 6.5 编译安装 mysql 5.5.40

在开始之前我们需要准备一些库文件。

1
[root@imarkofu ~]# yum install zlib-devel openssl-devel gcc-c++ gcc bison cmake ncurses-devel -y

一定要安装这些包, LZ之前没有安装就开始编译MySQL,踩了好多坑, 后来上网一查才发现需要这些文件和管理工具才行。因此一定要安装。

还有如果大家编译过程中出错, 请一定要执行:

1
[root@imarkofu ~]# rm -f CMakeCache.txt

之后再继续安装。
安装好之后我们来添加MySQL数据库需要的账户和目录:

1
2
3
4
5
[root@imarkofu ~]# groupadd -r mysql
[root@imarkofu ~]# useradd -g mysql -r -s /sbin/nologin mysql
[root@imarkofu ~]# id mysql #确认用户
[root@imarkofu ~]# mkdir /var/data/mysql -p #存放数据库文件
[root@imarkofu ~]# chown mysql:mysql /var/data -R

下载MySQL源码包:mysql-5.5.40.tar.gz
下载后上传到服务器

1
2
[root@imarkofu ~]# tar -xvf mysql-5.5.40.tar.gz -C /usr/local/src/
[root@imarkofu ~]# cd /usr/local/src/mysql-5.5.40

cmake编译MySQL的一些常用选项:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
------------------------------------------------------------
cmake mysql 编译安装:
./configure -> cmake .
./configure --help -> cmake -LH -> ccmake .
------------------------------------------------------------
指定安装文件的安装路径常用选项
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql
-DMYSQL_DATADIR=/data/mysql
-DSYSCONFDIR=/etc
------------------------------------------------------------
默认编译的存储引擎包括: csv, myisma, myisammrg, heap. 若要安装其他存储引擎,可以使用类似如下的编译选项
-DWITH_INNOBASE_STORAGE_ENGINE=1
-DWITH_ARCHIVE_STORAGE_ENGINE=1
-DWITH_BLACKHOLE_STORAGE_ENGINE=1
------------------------------------------------------------
若要明确指出不编译某存储引擎, 可以使用类似如下的选项:
-DWITHOUT_<ENGINE>_STORAGE_ENGINE=1
例如:
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1
-DWITHOUT_FEDERATED_STORAGE_ENGINE=1
------------------------------------------------------------
若要编译进其他功能, 如SSl等, 可使用类似如下选项来实现编译时使用某库文件或者, 或者不使用某库文件.
-DWITH_READLINE=1
-DWITH_SSL=system
-DWITH_ZLIB=system
-DWITH_LIBWRAP=0
------------------------------------------------------------
其他常用选项:
-DMYSQL_TCP_PORT=3306
-MYDQL_UNIX_ADDR=/tmp/mysql.sock
-DENABLED_LOCAL_INFILE=1
-DEXTRA_CHARSETS=all
-DDEFAULT_CHARSET=utf8
-DDEFAULT_COLLATION=utf8_general_ci
-DWITH_DEBUG=0
-DENABLE_PROFILING=1
------------------------------------------------------------

开始编译

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@imarkofu mysql-5.5.40]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql-5.5.40 \
-DMYSQL_DATADIR=/var/data/mysql \ #数据库目录
-DSYSCONFDIR=/etc \ #配置文件目录
-DWITH_INNOBASE_STORAGE_ENGINE=1 \ #启用InnoDB存储引擎
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \ #启用ARCHIVE存储引擎
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \ #启用黑洞存储引擎
-DWITH_READLINE=1 \
-DWITH_SSL=system \ #启用SSL协议
-DWITH_ZLIB=system \ #启用Zlib压缩
-DWITH_LIBWRAP=0 \
-DDEFAULT_CHARSET=utf8 \ #设置字符集utf8
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DDEFAULT_COLLATION=utf8_general_ci

如果出现以下信息,那么恭喜你第一阶段的配置顺利完成了
过程中如果出现错误也不要害怕, 因为你长经验的时刻就要来了, 通常是因为缺少了什么依赖的包而出现的问题, 你可以把你的错误信息铁道Google或者百度中去搜索。

1
2
3
4
5
6
7
8
9
10
11
12
13
....(此处省略一万字)
-- Performing Test HAVE_IB_GCC_ATOMIC_BUILTINS
-- Performing Test HAVE_IB_GCC_ATOMIC_BUILTINS - Success
-- Performing Test HAVE_IB_ATOMIC_PTHREAD_T_GCC
-- Performing Test HAVE_IB_ATOMIC_PTHREAD_T_GCC - Success
-- Check size of pthread_t
-- Check size of pthread_t - done
-- Performing Test HAVE_PEERCRED
-- Performing Test HAVE_PEERCRED - Success
-- Library mysqlclient depends on OSLIBS -lpthread;z;m;rt;/usr/lib64/libssl.so;/usr/lib64/libcrypto.so;dl
-- Configuring done
-- Generating done
-- Build files have been written to: /usr/local/mysql-5.5.40

继续

1
2
3
4
5
6
7
8
9
10
[root@imarkofu mysql-5.5.40]# make
....(此处省略一万字)
Scanning dependencies of target udf_example
[100%] Building C object sql/CMakeFiles/udf_example.dir/udf_example.c.o
Linking C shared module udf_example.so
[100%] Built target udf_example
Scanning dependencies of target my_safe_process
[100%] Building CXX object mysql-test/lib/My/SafeProcess/CMakeFiles/my_safe_process.dir/safe_process.cc.o
Linking CXX executable my_safe_process
[100%] Built target my_safe_process

如果没有报错,继续

1
2
3
4
5
6
7
8
9
10
[root@imarkofu mysql-5.5.40]# make install
....(此处省略一万字)
-- Installing: /usr/local/mysql-5.5.40/man/man1/mysql_upgrade.1
-- Installing: /usr/local/mysql-5.5.40/man/man1/mysqlslap.1
-- Installing: /usr/local/mysql-5.5.40/man/man1/replace.1
-- Installing: /usr/local/mysql-5.5.40/man/man1/mysql.1
-- Installing: /usr/local/mysql-5.5.40/man/man1/mysql_setpermission.1
-- Installing: /usr/local/mysql-5.5.40/man/man1/mysql_secure_installation.1
-- Installing: /usr/local/mysql-5.5.40/man/man1/mysqlimport.1
-- Installing: /usr/local/mysql-5.5.40/man/man8/mysqld.8

全部安装ok之后我们来配置下文档的权限

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[root@imarkofu mysql-5.5.40]# chown :mysql /usr/local/mysql-5.5.40 -R
[root@imarkofu mysql-5.5.40]# cd /usr/local/mysql-5.5.40/
[root@imarkofu mysql-5.5.40]# chmod +x scripts/mysql_install_db
[root@imarkofu mysql-5.5.40]# scripts/mysql_install_db --user=mysql --datadir=/var/data/mysql/ #执行初始化脚本
....(此处省略一万字)
./bin/mysqladmin -u root password 'new-password'
./bin/mysqladmin -u root -h imarkofu password 'new-password'

Alternatively you can run:
./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 . ; ./bin/mysqld_safe &

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

Please report any problems at http://bugs.mysql.com/

如果你获得的信息和上面一样的话, 恭喜你, 你又离成功近了一步

1
2
[root@imarkofu mysql-5.5.40]# ls /var/data/mysql/ #看一下是否生成了文件
mysql performance_schema test

如果是这样那么继续
创建服务脚本

1
2
3
[root@imarkofu mysql-5.5.40]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld 
[root@imarkofu mysql-5.5.40]# chmod +x /etc/rc.d/init.d/mysqld #添加mysqld执行权限
[root@imarkofu mysql-5.5.40]# chkconfig --add mysqld #添加开机启动

创建配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[root@imarkofu mysql-5.5.40]# mv /etc/my.cnf{,.bak}
[root@imarkofu mysql-5.5.40]# cp support-files/my-large.cnf /etc/my.cnf
[root@imarkofu mysql-5.5.40]# vim /etc/my.cnf

#找到如下部分
[mysqld]
port = 3306
socket = /tmp/mysql.sock
skip-external-locking
key_buffer_size = 256M
max_allowed_packet = 1M
table_open_cache = 256
sort_buffer_size = 1M
read_buffer_size = 1M
read_rnd_buffer_size = 4M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size= 16M
# Try number of CPU's*2 for thread_concurrency
thread_concurrency = 8
datadir= /var/data/mysql #添加次行内容, 就是数据库文件存放路径
:wq
#保存退出

[root@imarkofu mysql-5.5.40]# vim /etc/profile.d/mysql.sh
export PATH=/usr/local/mysql-5.5.40/bin:$PATH #添加此行

好了,尝试着启动MySQL服务

1
2
3
4
5
6
[root@imarkofu mysql-5.5.40]# service mysqld start
Starting MySQL.. [ OK ]
[root@imarkofu mysql-5.5.40]#
[root@imarkofu mysql-5.5.40]# ss -tnl| grep :3306
LISTEN 0 50 *:3306 *:*
#发现正常监听 3306端口

距离胜利越来越近了
然后配置MySQL管理员密码进行初始化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
[root@imarkofu mysql-5.5.40]# mysql #使用客户端登录
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.40-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>
#查看一下默认存在的用户
mysql> select user,password,host from mysql.user;
+------+----------+-----------+
| user | password | host |
+------+----------+-----------+
| root | | localhost |
| root | | imarkofu |
| root | | 127.0.0.1 |
| root | | ::1 |
| | | localhost |
| | | imarkofu |
+------+----------+-----------+
6 rows in set (0.00 sec)
mysql>

#删除默认存在的账户, 要求用户只能从本地服务器访问数据库
mysql> delete from mysql.user where host!='localhost';
Query OK, 4 rows affected (0.00 sec)

#修改root管理员账号密码
mysql> update mysql.user set password=PASSWORD('p@ssw0rd') where user='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

#更新数据库
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

#看一下修改后的结果
mysql> select user,password,host from mysql.user;
+------+-------------------------------------------+-----------+
| user | password | host |
+------+-------------------------------------------+-----------+
| root | *D7E39C3AF517EC9EF7086223B036E0B4F22821F8 | localhost |
| | | localhost |
+------+-------------------------------------------+-----------+
2 rows in set (0.00 sec)
mysql> \q
Bye
[root@imarkofu mysql-5.5.40]#

然后就可以使用新密码登陆了

1
2
3
4
5
6
7
8
9
10
11
[root@imarkofu mysql-5.5.40]# mysql -u root -h localhost -p
Enter password: #输入刚才设置的密码
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.5.40-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>

好了MySQL到此 就安装成功了
对了,忘记了导入开发的库文件了, ok 很快就好

1
2
3
4
5
6
7
8
9
[root@imarkofu mysql-5.5.40]# ln -sv /usr/local/mysql/include/ /usr/include/mysql #软连接到系统库中
[root@imarkofu mysql-5.5.40]# vim /etc/ld.so.conf.d/mysql.conf #编辑这个文件, 并且添加如下路径
/usr/local/mysql-5.5.40/lib
[root@imarkofu mysql-5.5.40]# ldconfig #重新加载库文件
[root@imarkofu mysql-5.5.40]# ldconfig -p | grep mysql #让我们来检查下是不是加载好了
libmysqlclient_r.so.16 (libc6,x86-64) => /usr/lib64/mysql/libmysqlclient_r.so.16
libmysqlclient.so.18 (libc6,x86-64) => /usr/local/mysql-5.5.40/lib/libmysqlclient.so.18
libmysqlclient.so.16 (libc6,x86-64) => /usr/lib64/mysql/libmysqlclient.so.16
libmysqlclient.so (libc6,x86-64) => /usr/local/mysql-5.5.40/lib/libmysqlclient.so

至此,MySQL5.5.40 编译安装完成