安装环境
[root@localhost ~]# cat /etc/CentOS-release CentOS Linux release 7.0.1406 (Core)
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++
部分常用编译选项
- -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 官方文档编译选项说明
- [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 unknownCMake 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=
This CMake script will look for boost in . 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_BOOSTWITH-LIBWRAPWITH_MEMORY_STORAGE_ENGINEWITH_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]#
socket=/var/run/mysql/mysql.sockbasedir = /usr/local/mysqldatadir = /mymnt/sqldatasocket=/var/run/mysql/mysql.socklog-error = /var/log/mysql/error.logpid-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
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]# mysqlERROR 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
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/mysqldbin/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
百度到一个 mysql 配置文件样例:
- # 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
CentOS 7.2 安装 MySQL 5.7.13 http://www.linuxidc.com/Linux/2017-03/141300.htm
MySQL 数据库操作教程 http://www.linuxidc.com/Linux/2017-02/141092.htm
深入理解 MySQL 的四种隔离级别 http://www.linuxidc.com/Linux/2017-02/140848.htm
CentOS 7.2 下 RPM 方式安装 MySQL5.6 http://www.linuxidc.com/Linux/2017-02/140795.htm
CentOS7 下 MySQL5.7 安装与配置(YUM) http://www.linuxidc.com/Linux/2017-02/140423.htm
CentOS7 下 MySQL5.7.13 源码编译安装与配置 http://www.linuxidc.com/Linux/2017-02/140424.htm
来源: http://www.linuxidc.com/Linux/2017-03/142004.htm