MySQL可以用RPM包直接安装,但是不能指定安装等,所有选项皆是默认,安装相对简单,这里就不再介绍。这里介绍MySQL的源码安装。
1.环境
OS: CentOS 6.5 x64
MySQL: 5.6 for Linux (x86_64)
2.下载源码
打开网址:http://dev.mysql.com/downloads/mysql/#downloads
会看到下图:
我们需要的时linux6下的源码包,所以点击上图右侧的 Looking for previous GA versions? ,会看到下图:
Select Version: 项选择5.6.31,Select Platform: 项选择Source Code,从下面的下载列表里选择 Red Hat Enterprise Linux 6 / Oracle Linux 6 (Architecture Independent), RPM Package ,下载。
下载时需要Oracle帐号,没有就注册一个,免费的。
3.准备cmake
通过编写CMakeLists.txt,可以控制生成的Makefile,从而控制编译过程。CMake自动生成的Makefile不仅可以通过make命令构建项目生成目标文件,还支持安装(make install)、测试安装的程序是否能正确执行(make test,或者ctest)、生成当前平台的安装包(make package)、生成源码包(make package_source)、产生Dashboard显示数据并上传等高级功能,只要在CMakeLists.txt中简单配置,就可以完成很多复杂的功能,包括写测试用例。如果有嵌套目录,子目录下可以有自己的CMakeLists.txt。总之,CMake是一个非常强大的编译自动配置工具,支持各种平台。
检查是否已经安装cmake,如果没有安装,需要先下载安装,下载地址:https://cmake.org/download/
[root@test1 mysql]# cmake
-bash: cmake: command not found
[root@test1 mysql]#
没有安装,下载安装程序如cmake-3.5.2.tar.gz,不要下载二进制文件如cmake-3.5.2-Linux-x86_64.tar.gz,安装:
[root@test1 mysql]# tar -zxvf cmake-3.5.2.tar.gz
cmake-3.5.2/.gitattributes
cmake-3.5.2/.hooks-config.bash
cmake-3.5.2/Auxiliary/
。。。
[root@test1 mysql]# cd cmake-3.5.2
[root@test1 cmake-3.5.2]# ls
Auxiliary cmake_uninstall.cmake.in DartConfig.cmake Source
bootstrap CompileFlags.cmake doxygen.config Templates
CMakeCPack.cmake configure Help Tests
CMakeCPackOptions.cmake.in CONTRIBUTING.rst Licenses Utilities
CMakeGraphVizOptions.cmake Copyright.txt Modules
CMakeLists.txt CTestConfig.cmake Packaging
CMakeLogo.gif CTestCustom.cmake.in README.rst
[root@test1 cmake-3.5.2]# ./bootstrap && gmake && gmake install
安装完cmake后,验证一下是否安装成功:
[root@test1 cmake-3.5.2]# cmake
Usage
cmake [options] <path-to-source>
cmake [options] <path-to-existing-build>
Specify a source directory to (re-)generate a build system for it in the
current working directory. Specify an existing build directory to
re-generate its build system.
Run 'cmake --help' for more information.
[root@test1 cmake-3.5.2]#
4.把下载的mysql-5.6.31.tar.gz上传到服务器,解压缩
[root@test1 mysql]# tar -zxvf mysql-5.6.31.tar.gz
[root@test1 mysql]# cd mysql-5.6.31
[root@test1 mysql-5.6.31]# ls
BUILD CMakeLists.txt configure.cmake Docs include libmysql man mysys_ssl README sql storage tests vio
client cmd-line-utils COPYING Doxyfile-perfschema INSTALL libmysqld mysql-test packaging regex sql-bench strings unittest win
cmake config.h.cmake dbug extra libevent libservices mysys plugin scripts sql-common support-files VERSION zlib
[root@test1 mysql-5.6.31]#
5.创建用户
[root@test1 mysql-5.6.31]# groupadd mysql
[root@test1 mysql-5.6.31]# useradd -r -g mysql -s /bin/false mysql
增加一个mysql用户:
-r:系统用户
-g:用户属组
-s:用户shell,/bin/false表示用户不能登录系统,与/bin/nologin相同
6.使用cmake配置源程序目录
cmake有许多选项可以配置,例如程序的安装目录、要安装的存储引擎等等,可以先切换到mysql的源码目录,执行cmake . -L 或 cmake . -LH 或 cmake . -LAH命令查看,也可以通过ccmake . 方式用命令行进行修改。
[root@test1 mysql]# cd mysql-5.6.31
[root@test1 mysql-5.6.31]# cmake . -L #查看选项
-- Running cmake version 3.5.2
-- MySQL 5.6.31
-- Packaging as: mysql-5.6.31-Linux-x86_64
-- HAVE_VISIBILITY_HIDDEN
-- HAVE_VISIBILITY_HIDDEN
-- HAVE_VISIBILITY_HIDDEN
-- Using cmake version 3.5.2
-- Not building NDB
-- Library mysqlclient depends on OSLIBS -lpthread;m;rt;dl
-- Library mysqlserver depends on OSLIBS -lpthread;m;rt;crypt;dl
-- CMAKE_BUILD_TYPE: RelWithDebInfo
-- COMPILE_DEFINITIONS: HAVE_CONFIG_H
-- CMAKE_C_FLAGS: -Wall -Wextra -Wformat-security -Wvla -Wwrite-strings -Wdeclaration-after-statement
-- CMAKE_CXX_FLAGS: -Wall -Wextra -Wformat-security -Wvla -Woverloaded-virtual -Wno-unused-parameter
-- CMAKE_C_FLAGS_RELWITHDEBINFO: -O3 -g -fabi-version=2 -fno-omit-frame-pointer -fno-strict-aliasing -DDBUG_OFF
-- CMAKE_CXX_FLAGS_RELWITHDEBINFO: -O3 -g -fabi-version=2 -fno-omit-frame-pointer -fno-strict-aliasing -DDBUG_OFF
-- Configuring done
-- Generating done
-- Build files have been written to: /root/mysql/mysql-5.6.31
-- Cache values
CMAKE_BUILD_TYPE:STRING=RelWithDebInfo
CMAKE_INSTALL_PREFIX:PATH=/usr/local/mysql
COMMUNITY_BUILD:BOOL=ON
ENABLED_PROFILING:BOOL=ON
ENABLE_DEBUG_SYNC:BOOL=ON
ENABLE_DOWNLOADS:BOOL=OFF
ENABLE_GCOV:BOOL=OFF
ENABLE_GPROF:BOOL=OFF
ENABLE_MEMCACHED_SASL:BOOL=OFF
ENABLE_MEMCACHED_SASL_PWDB:BOOL=OFF
FEATURE_SET:STRING=community
INSTALL_LAYOUT:STRING=STANDALONE
MYSQL_DATADIR:PATH=/usr/local/mysql/data
MYSQL_MAINTAINER_MODE:BOOL=OFF
OPTIMIZER_TRACE:BOOL=ON
TMPDIR:PATH=P_tmpdir
WITH_ARCHIVE_STORAGE_ENGINE:BOOL=ON
WITH_ASAN:BOOL=OFF
WITH_BLACKHOLE_STORAGE_ENGINE:BOOL=ON
WITH_DEBUG:BOOL=OFF
WITH_DEFAULT_COMPILER_OPTIONS:BOOL=ON
WITH_DEFAULT_FEATURE_SET:BOOL=ON
WITH_EDITLINE:STRING=bundled
WITH_EMBEDDED_SERVER:BOOL=ON
WITH_EMBEDDED_SHARED_LIBRARY:BOOL=OFF
WITH_EXAMPLE_STORAGE_ENGINE:BOOL=ON
WITH_EXTRA_CHARSETS:STRING=all
WITH_FEDERATED_STORAGE_ENGINE:BOOL=ON
WITH_INNOBASE_STORAGE_ENGINE:BOOL=ON
WITH_INNODB_MEMCACHED:BOOL=OFF
WITH_LIBEVENT:STRING=bundled
WITH_LIBWRAP:BOOL=OFF
WITH_NUMA:BOOL=ON
WITH_PARTITION_STORAGE_ENGINE:BOOL=ON
WITH_PERFSCHEMA_STORAGE_ENGINE:BOOL=ON
WITH_PIC:BOOL=OFF
WITH_SSL:STRING=bundled
WITH_SYMVER16:BOOL=OFF
WITH_UNIT_TESTS:BOOL=ON
WITH_VALGRIND:BOOL=OFF
WITH_ZLIB:STRING=bundled
[root@test1 mysql-5.6.31]# ccmake . #以命令行方式查看和修改选项
部分参数说明:
# -DCMAKE_INSTALL_PREFIX=/usr/local/mysql
# -DMYSQL_DATADIR=/usr/local/mysql/data
# -DSYSCONFDIR=/etc
# -DWITH_MYISAM_STORAGE_ENGINE=1
# -DWITH_INNOBASE_STORAGE_ENGINE=1
# -DWITH_MEMORY_STORAGE_ENGINE=1
# -DWITH_READLINE=1
# -DMYSQL_UNIX_ADDR=/tmp/mysqld.sock
# -DMYSQL_TCP_PORT=3306
# -DENABLED_LOCAL_INFILE=1
# -DWITH_PARTITION_STORAGE_ENGINE=1
# -DEXTRA_CHARSETS=all
# -DDEFAULT_CHARSET=utf8
# -DDEFAULT_COLLATION=utf8_general_ci
这里要将安装目录和数据目录更换到/u01目录,执行如下命令进行配置:
[root@test1 mysql-5.6.31]# mkdir bld
[root@test1 mysql-5.6.31]# cd bld
[root@test1 bld]# cmake .. -DCMAKE_INSTALL_PREFIX=/u01/mysql -DMYSQL_DATADIR=/u01/mysql/data
-- Running cmake version 3.5.2
-- Found Git: /usr/bin/git (found version "1.7.1")
-- The C compiler identification is GNU 4.4.7
。。。
-- Configuring done
-- Generating done
-- Build files have been written to: /root/mysql/mysql-5.6.31/bld
[root@test1 bld]# ls
client cmake_install.cmake CPackSourceConfig.cmake Docs info_macros.cmake libmysqld Makefile mysys plugin sql strings unittest zlib
CMakeCache.txt cmd-line-utils CTestTestfile.cmake extra libevent libservices man mysys_ssl regex sql-bench support-files VERSION.dep
CMakeFiles CPackConfig.cmake dbug include libmysql make_dist.cmake mysql-test packaging scripts storage tests vio
[root@test1 bld]#
7.安装
[root@test1 bld]# make
Scanning dependencies of target abi_check
[ 0%] Built target abi_check
Scanning dependencies of target INFO_SRC
[ 0%] Built target INFO_SRC
。。。
[100%] Building CXX object mysql-test/lib/My/SafeProcess/CMakeFiles/my_safe_process.dir/safe_process.cc.o
[100%] Linking CXX executable my_safe_process
[100%] Built target my_safe_process
[root@test1 bld]#
[root@test1 bld]# make install #安装,此时也可以指定安装目录,如安装到/u01/mysql可以这样执行:make install DESTDIR=/u01/mysql。由于我们前面cmake时指定好了安装目录,这里只需默认安装即可。
[ 0%] Built target abi_check
[ 0%] Built target INFO_SRC
[ 0%] Built target INFO_BIN
。。。
[root@test1 bld]#
8.查看安装结果:
[root@test1 mysql]# cd /u01/mysql
[root@test1 mysql]# ls
bin COPYING data docs include lib man mysql-test README scripts share sql-bench support-files
[root@test1 mysql]# cd data
[root@test1 data]# ls
test
[root@test1 data]# cd test
[root@test1 test]# ls
db.opt
[root@test1 test]#
9.初始化数据目录
[root@test1 mysql]# cd /u01/mysql/
[root@test1 mysql]# ll
total 68
drwxr-xr-x 2 root root 4096 Jun 7 11:44 bin
-rw-r--r-- 1 root root 17987 May 17 06:19 COPYING
drwxr-xr-x 3 mysql mysql 4096 Jun 7 11:44 data
drwxr-xr-x 2 root root 4096 Jun 7 11:51 docs
drwxr-xr-x 3 root root 4096 Jun 7 11:44 include
drwxr-xr-x 3 root root 4096 Jun 7 11:44 lib
drwxr-xr-x 4 root root 4096 Jun 7 11:44 man
drwxr-xr-x 10 root root 4096 Jun 7 11:45 mysql-test
-rw-r--r-- 1 root root 2496 May 17 06:19 README
drwxr-xr-x 2 root root 4096 Jun 7 11:44 scripts
drwxr-xr-x 28 root root 4096 Jun 7 11:45 share
drwxr-xr-x 4 root root 4096 Jun 7 11:45 sql-bench
drwxr-xr-x 2 root root 4096 Jun 7 11:45 support-files
[root@test1 mysql]# chown mysql.mysql -R * #修改用户权限到mysql用户,使其具有读写权
[root@test1 mysql]# ll
total 68
drwxr-xr-x 2 mysql mysql 4096 Jun 7 11:44 bin
-rw-r--r-- 1 mysql mysql 17987 May 17 06:19 COPYING
drwxr-xr-x 3 mysql mysql 4096 Jun 7 11:44 data
drwxr-xr-x 2 mysql mysql 4096 Jun 7 11:51 docs
drwxr-xr-x 3 mysql mysql 4096 Jun 7 11:44 include
drwxr-xr-x 3 mysql mysql 4096 Jun 7 11:44 lib
drwxr-xr-x 4 mysql mysql 4096 Jun 7 11:44 man
drwxr-xr-x 10 mysql mysql 4096 Jun 7 11:45 mysql-test
-rw-r--r-- 1 mysql mysql 2496 May 17 06:19 README
drwxr-xr-x 2 mysql mysql 4096 Jun 7 11:44 scripts
drwxr-xr-x 28 mysql mysql 4096 Jun 7 11:45 share
drwxr-xr-x 4 mysql mysql 4096 Jun 7 11:45 sql-bench
drwxr-xr-x 2 mysql mysql 4096 Jun 7 11:45 support-files
[root@test1 mysql]scripts/mysql_install_db --user=mysql --basedir=/u01/mysql --datadir=/u01/mysql/data
。。。
Installing MySQL system tables...2016-06-07 12:56:32 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2016-06-07 12:56:32 0 [Note] ./bin/mysqld (mysqld 5.6.31-log) starting as process 32501 ...
2016-06-07 12:56:32 32501 [Note] InnoDB: Using atomics to ref count buffer pool pages
。。。
2016-06-07 12:56:36 32524 [Note] InnoDB: Starting shutdown...
2016-06-07 12:56:37 32524 [Note] InnoDB: Shutdown completed; log sequence number 168661276
OK
。。。
完成。mysql_install_db命令是用来创建服务器的数据目录,包括mysql数据库、权限表、performance_schema库等等。
[root@test1 mysql]# cd data
[root@test1 data]# ls
ibdata1 ib_logfile0 ib_logfile1 mysql mysql-bin.000001 mysql-bin.000002 mysql-bin.index performance_schema test
[root@test1 data]#
[root@test1 data]# cd .. #切换到mysql安装目录
[root@test1 mysql]# pwd
/u01/mysql
[root@test1 mysql]# chown -R root . #再次修改权限
[root@test1 mysql]# ll
total 76
drwxr-xr-x 2 root mysql 4096 Jun 7 11:44 bin
-rw-r--r-- 1 root mysql 17987 May 17 06:19 COPYING
drwxr-xr-x 5 root mysql 4096 Jun 7 13:33 data
drwxr-xr-x 2 root mysql 4096 Jun 7 11:51 docs
drwxr-xr-x 3 root mysql 4096 Jun 7 11:44 include
drwxr-xr-x 3 root mysql 4096 Jun 7 11:44 lib
drwxr-xr-x 4 root mysql 4096 Jun 7 11:44 man
-rw-r--r-- 1 root root 943 Jun 7 12:52 my.cnf
-rw-r--r-- 1 root root 943 Jun 7 13:33 my-new.cnf
drwxr-xr-x 10 root mysql 4096 Jun 7 11:45 mysql-test
-rw-r--r-- 1 root mysql 2496 May 17 06:19 README
drwxr-xr-x 2 root mysql 4096 Jun 7 11:44 scripts
drwxr-xr-x 28 root mysql 4096 Jun 7 11:45 share
drwxr-xr-x 4 root mysql 4096 Jun 7 11:45 sql-bench
drwxr-xr-x 2 root mysql 4096 Jun 7 11:45 support-files
[root@test1 mysql]# chown -R mysql data #data目录仍属于mysql用户
[root@test1 mysql]# ll
total 76
drwxr-xr-x 2 root mysql 4096 Jun 7 11:44 bin
-rw-r--r-- 1 root mysql 17987 May 17 06:19 COPYING
drwxr-xr-x 5 mysql mysql 4096 Jun 7 13:33 data
drwxr-xr-x 2 root mysql 4096 Jun 7 11:51 docs
drwxr-xr-x 3 root mysql 4096 Jun 7 11:44 include
drwxr-xr-x 3 root mysql 4096 Jun 7 11:44 lib
drwxr-xr-x 4 root mysql 4096 Jun 7 11:44 man
-rw-r--r-- 1 root root 943 Jun 7 12:52 my.cnf
-rw-r--r-- 1 root root 943 Jun 7 13:33 my-new.cnf
drwxr-xr-x 10 root mysql 4096 Jun 7 11:45 mysql-test
-rw-r--r-- 1 root mysql 2496 May 17 06:19 README
drwxr-xr-x 2 root mysql 4096 Jun 7 11:44 scripts
drwxr-xr-x 28 root mysql 4096 Jun 7 11:45 share
drwxr-xr-x 4 root mysql 4096 Jun 7 11:45 sql-bench
drwxr-xr-x 2 root mysql 4096 Jun 7 11:45 support-files
[root@test1 mysql]#
10.my.cnf配置文件
安装到此时会在mysql的安装目录有一个my.cnf文件,该文件是mysql服务器配置文件的模板文件,将该my.cnf复制到/etc目录下进行适当修改即可。模板的内容如下:
[root@test1 mysql]# cat my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
[mysqld]
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
# port = .....
# server_id = .....
# socket = .....
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
[root@test1 mysql]#
这里只是进行了简单配置:
[root@test1 mysql]# mkdir log #先创建存放各种文件的目录,文件的集中存放会对以后的管理带来方便
[root@test1 mysql]# chown mysql.mysql log #修改权限
[root@test1 mysql]# vi /etc/my.cnf
[mysqld]
basedir=/u01/mysql
datadir=/u01/mysql/data
socket=/u01/mysql/log/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-bin=mysql-bin
server-id=1
[mysqld_safe]
log-error=/u01/mysql/log/mysqld.log
pid-file=/u01/mysql/log/mysqld.pid
[client]
socket=/u01/mysql/log/mysql.sock
11.启动mysql
[root@test1 ~]# echo 'export PATH=$PATH:/u01/mysql/bin' >> /etc/profile #配置PATH变量
[root@test1 ~]# source /etc/profile #加载PATH变量
[root@test1 ~]# mysqld_safe --user=mysql & #启动mysql
[1] 8183
[root@test1 mysql]# 160607 22:50:22 mysqld_safe Logging to '/u01/mysql/log/mysqld.log'.
160607 22:50:22 mysqld_safe Starting mysqld daemon with databases from /u01/mysql/data
[root@test1 mysql]# tail /u01/mysql/log/mysqld.log
2016-06-07 22:50:23 8381 [Note] InnoDB: 128 rollback segment(s) are active.
2016-06-07 22:50:23 8381 [Note] InnoDB: Waiting for purge to start
2016-06-07 22:50:23 8381 [Note] InnoDB: 5.6.31 started; log sequence number 1626027
2016-06-07 22:50:23 8381 [Note] Server hostname (bind-address): '*'; port: 3306
2016-06-07 22:50:23 8381 [Note] IPv6 is available.
2016-06-07 22:50:23 8381 [Note] - '::' resolves to '::';
2016-06-07 22:50:23 8381 [Note] Server socket created on IP: '::'.
2016-06-07 22:50:23 8381 [Note] Event Scheduler: Loaded 0 events
2016-06-07 22:50:23 8381 [Note] /u01/mysql/bin/mysqld: ready for connections.
Version: '5.6.31-log' socket: '/u01/mysql/log/mysql.sock' port: 3306 Source distribution
启动成功。
12.制作MySQL服务
[root@test1 mysql]# cp support-files/mysql.server /etc/init.d/mysql
[root@test1 mysql]# /etc/init.d/mysql start
Starting MySQL.. [ OK ]
[root@test1 ~]# chkconfig --add mysql
[root@test1 ~]# chkconfig --level 35 mysql on
现在MySQL可以开机自动启动了。
13.修改root密码
[root@test1 log]# mysqladmin -uroot password
New password:
Confirm new password:
[root@test1 log]#
14.验证
[root@test1 log]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.6.31-log Source distribution
Copyright (c) 2000, 2016, 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
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select host,user,password from user;
+-----------+------+-------------------------------------------+
| host | user | password |
+-----------+------+-------------------------------------------+
| localhost | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| test1 | root | |
| 127.0.0.1 | root | |
| ::1 | root | |
| localhost | | |
| test1 | | |
+-----------+------+-------------------------------------------+
6 rows in set (0.02 sec)
mysql>
MySQL源码安装完成。