安装环境
[root@localhost ~]# cat /etc/centos-release
CentOS Linux release 7.0.1406 (Core)
0x01 准备工作
1、到mysql官网下载mysql-community-5.7.11-1.el7.src.rpm源码包
rpm -ih mysql-community-5.7.11-1.el7.src.rpm会在用户目录下生成一个rpmbuild,从SOURCES文件夹内可以获得源码mysql-5.7.11.tar.gz,解压进入源码目录准备安装
2、因为mysql需要使用cmake编译,直接从yum从光盘的源中找到cmake
[root@localhost ~]# yum info cmake
名称 :cmake
架构 :x86_64
版本 :2.8.11
发布 :4.el7
大小 :6.7 M
3、准备开发环境yum groupinstall Additional Development,yum groupinstall Development tools之前的版本是Development tools和Development Libraries两个软件包组,我这里没有安装,因为只需要gcc和gcc-c++
0x02 mysql编译选项
部分常用编译选项
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql [MySQL安装的根目录]
-DMYSQL_DATADIR=/mydata/mysql/data [MySQL数据库文件存放目录]
-DSYSCONFDIR=/etc [MySQL配置文件所在目录]
-DMYSQL_USER=mysql [MySQL用户名]
-DWITH_MYISAM_STORAGE_ENGINE=1 [MySQL的数据库引擎]
-DWITH_INNOBASE_STORAGE_ENGINE=1 [MySQL的数据库引擎]
-DWITH_ARCHIVE_STORAGE_ENGINE=1 [MySQL的数据库引擎]
-DWITH_MEMORY_STORAGE_ENGINE=1 [MySQL的数据库引擎]
-DWITH_READLINE=1 [MySQL的readline library,批量导入数据]
-DMYSQL_UNIX_ADDR=/var/run/mysql/mysql.sock [MySQL的通讯目录]
-DWITH-LIBWRAP=0 [是否支持libwrap]
-DENABLE_DOWNLOADS=1 [编译时允许自主下载相关文件]
-DDEFAULT_CHARSET=utf8 [设置默认字符集为utf8]
-DDEFAULT_COLLATION=utf8_general_ci [设置默认排序字符集规则]
http://dev.mysql.com/doc/refman/5.7/en/source-configuration-options.html 官方文档编译选项说明
0x03 安装过程(gcc / boost_1_59_0 / CURSES_LIBRARY )
[root@localhost ~]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/mymnt/sqldata -DSYSCONFDIR=/etc -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DMYSQL_UNIX_ADDR=/var/run/mysql/mysql.sock -DWITH-LIBWRAP=0 -DENABLE_DOWNLOADS=1 -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci
-- Running cmake version 2.8.11
-- Could NOT find Git (missing: GIT_EXECUTABLE)
-- Configuring with MAX_INDEXES = 64U
-- The C compiler identification is unknown
-- The CXX compiler identification is unknown
CMake Error: your C compiler: "CMAKE_C_COMPILER-NOTFOUND" was not found. Please set CMAKE_C_COMPILER to a valid compiler path or name.
CMake Error: your CXX compiler: "CMAKE_CXX_COMPILER-NOTFOUND" was not found. Please set CMAKE_CXX_COMPILER to a valid compiler path or name.
CMake Error at cmake/os/Linux.cmake:41 (MESSAGE):
Unsupported compiler!
Call Stack (most recent call first):
CMakeLists.txt:162 (INCLUDE)
这是没有安装gcc和gcc-c++
[root@localhost mysql-5.7.11]# yum install gcc gcc-c++
再cmake一次
-- MySQL 5.7.11
-- Packaging as: mysql-5.7.11-Linux-x86_64
-- Looked for boost/version.hpp in and
-- BOOST_INCLUDE_DIR BOOST_INCLUDE_DIR-NOTFOUND
-- LOCAL_BOOST_DIR
-- LOCAL_BOOST_ZIP
-- Could not find (the correct version of) boost.
-- MySQL currently requires boost_1_59_0
CMake Error at cmake/boost.cmake:81 (MESSAGE):
You can download it with -DDOWNLOAD_BOOST=1 -DWITH_BOOST=<directory>
This CMake script will look for boost in <directory>. If it is not there,
it will download and unpack it (in that directory) for you.
If you are inside a firewall, you may need to use an http proxy:
export http_proxy=http://example.com:80
Call Stack (most recent call first):
cmake/boost.cmake:238 (COULD_NOT_FIND_BOOST)
CMakeLists.txt:443 (INCLUDE)
-- Configuring incomplete, errors occurred!
这个就已经说明了You can download it with -DDOWNLOAD_BOOST=1 -DWITH_BOOST=<directory>,Boost库是一个经过千锤百炼、可移植、提供源代码的C++库,作为标准库的后备,是C++标准化进程的发动机之一。
先从boost官网下载这个库boost_1_59_0.tar.gz
解压后把目录添加到-DWITH_BOOST编译选项中
继续编译报错如下
-- Could NOT find Curses (missing: CURSES_LIBRARY CURSES_INCLUDE_PATH)
CMake Error at cmake/readline.cmake:64 (MESSAGE):
Curses library not found. Please install appropriate package,
remove CMakeCache.txt and rerun cmake.On Debian/Ubuntu, package name is libncurses5-dev, on Redhat and derivates it is ncurses-devel.
Call Stack (most recent call first):
cmake/readline.cmake:107 (FIND_CURSES)
cmake/readline.cmake:181 (MYSQL_USE_BUNDLED_EDITLINE)
CMakeLists.txt:471 (MYSQL_CHECK_EDITLINE)
这个错误是说明缺少Curses库yum install ncurses-devel,需要注意的是每次编译错误都要移除CMakeCache.txt,最后出现 Build files have been written 即成功
CMake Warning:
Manually-specified variables were not used by the project:
DOWNLOAD_BOOST
WITH-LIBWRAP
WITH_MEMORY_STORAGE_ENGINE
WITH_READLINE
-- Build files have been written to: /root/mysql-5.7.11
使用make大约两个小时的样子编译完成,到百分之五十几的时候停留的时间比较长。
make install
出现一堆安装信息即完成安装
-- Installing: /usr/local/mysql/mysql-test/./cmake_install.cmake
-- Installing: /usr/local/mysql/mysql-test/./CTestTestfile.cmake
-- Up-to-date: /usr/local/mysql/mysql-test/mtr
-- Up-to-date: /usr/local/mysql/mysql-test/mysql-test-run
-- Installing: /usr/local/mysql/mysql-test/lib/My/SafeProcess/my_safe_process
-- Up-to-date: /usr/local/mysql/mysql-test/lib/My/SafeProcess/my_safe_process
-- Installing: /usr/local/mysql/mysql-test/lib/My/SafeProcess/Base.pm
-- Installing: /usr/local/mysql/support-files/my-default.cnf
-- Installing: /usr/local/mysql/support-files/mysqld_multi.server
-- Installing: /usr/local/mysql/support-files/mysql-log-rotate
-- Installing: /usr/local/mysql/support-files/magic
-- Installing: /usr/local/mysql/share/aclocal/mysql.m4
-- Installing: /usr/local/mysql/support-files/mysql.server
[root@localhost mysql-5.7.11]#
0x04 安装后目录创建
socket=/var/run/mysql/mysql.sock
basedir = /usr/local/mysql
datadir = /mymnt/sqldata
socket=/var/run/mysql/mysql.sock
log-error = /var/log/mysql/error.log
pid-file = /var/log/mysql/mysql.pid
如配置文件关于目录相关的部分,我们需要创建/var/run/mysql , /mymnt/sqldata , /var/log
创建mysql用户和组,将数据目录 /mymnt/sqldata 和安装目录/var/run/mysql的属主(组)更改成mysql
[root@localhost mysql]# groupadd -r mysql
[root@localhost mysql]# useradd -r -g mysql -s /sbin/nologin mysql
[root@localhost mysql]# mkdir /var/run/mysql
[root@localhost mysql]# mkdir /var/log/mysql
[root@localhost mysql]# chown mysql:mysql /var/run/mysql/
[root@localhost mysql]# chown mysql:mysql /var/log/mysql/
[root@localhost mysql]# chown mysql:mysql /mymnt/sqldata/
[root@localhost mysql]# chown -R mysql:mysql /usr/local/mysql
0x05 初始化
1、mysql-5.7版本的my.cnf文件在support-files/下并没有模板文件,自行添加配置文件
2、将安装目录下support-files/mysql.server 启动脚本复制到 /etc/init.d/mysqld,以便实现开机启动。也可以[root@localhost mysql]# ./support-files/mysql.server start 临时启动
未启动时执行mysql命令会报错如下:
[root@localhost mysql]# mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysql/mysql.sock' (2)
3、添加环境变量以便使用初始化命令 export PATH=/usr/local/mysql/bin:$PATH
4、初始化
mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/mymnt/sqldata
0x06 其他
1、mysqld_safe
mysqld 和 mysqld_safe都可以启动mysql。直接运行mysqld程序来启动MySQL服务的方法很少见,mysqld_safe脚本会在启动MySQL服务器后继续监控其运行情况,并在其死机时重新启动它。
我们可以发现这是一个shell脚本,在Unix和NetWare中推荐使用mysqld_safe来启动mysqld服务器。mysqld_safe增加了一些安全特性,例如当出现错误时重启服务器并向错误日志文件写入运行时间信息。
[root@localhost mysql]# file bin/mysqld
bin/mysqld: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=0x772828c9d330090391cd89dbe699f041a378944d, not stripped
[root@localhost mysql]# file bin/mysqld_safe
bin/mysqld_safe: POSIX shell script, ASCII text executable
2、mysql_secure_installation
MySQL安全配置向导,运行mysql_secure_installation会执行几个设置:
a)为root用户设置密码
b)删除匿名账号
c)取消root用户远程登录
d)删除test库和对test库的访问权限
e)刷新授权表使修改生效
通过这几项的设置能够提高mysql库的安全。建议生产环境中mysql安装这完成后一定要运行一次mysql_secure_installation
3、为了方便其他程序调用(如php),将MySQL数据库的动态链接库目录添加至系统链接库,echo "/usr/local/mysql/lib" > /etc/ld.so.conf.d/mysql.conf
0x07 附:my.cnf示例
百度到一个mysql配置文件样例:
- 作者:vfhky
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL. [client]
port=3306
socket=/var/run/mysql/mysql.sock [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.
user = mysql
basedir = /usr/local/mysql
datadir = /mydata/mysql/data
port=3306
server-id = 1
socket=/var/run/mysql/mysql.sock character-set-server = utf8
log-error = /var/log/mysql/error.log
pid-file = /var/log/mysql/mysql.pid
general_log = 1
skip-name-resolve
#skip-networking
back_log = 300 max_connections = 1000
max_connect_errors = 6000
open_files_limit = 65535
table_open_cache = 128
max_allowed_packet = 4M
binlog_cache_size = 1M
max_heap_table_size = 8M
tmp_table_size = 16M read_buffer_size = 2M
read_rnd_buffer_size = 8M
sort_buffer_size = 8M
join_buffer_size = 28M
key_buffer_size = 4M thread_cache_size = 8 query_cache_type = 1
query_cache_size = 8M
query_cache_limit = 2M ft_min_word_len = 4 log_bin = mysql-bin
binlog_format = mixed
expire_logs_days = 30 performance_schema = 0
explicit_defaults_for_timestamp #lower_case_table_names = 1 myisam_sort_buffer_size = 8M
myisam_repair_threads = 1 interactive_timeout = 28800
wait_timeout = 28800 # 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 # Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0 # Recommended in standard MySQL setup
sql_mode=NO_ENGINE_SUBSTITUTION,NO_AUTO_CREATE_USER,STRICT_TRANS_TABLES [mysqldump]
quick
max_allowed_packet = 16M [myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M