所有的网上下载的源码包都放在 /tmp 目录下面的
安装mysql-5.7.17内存要求在2G以上,不然会中断卡死
首先 安装依赖工具包
安装必要的软件:ncurses-devel、cmake、bison、boost
ncurses-devel、cmake、bison都可以用yum安装。Boost需要源码安装,版本有要求,尽可能下载包含boost的mysql源码包(一般用boost_1_59_0)。依赖包建议尽可能使用yum安装
MySQL从5.5开始使用cmake来编译安装了
我这里编译安装了一下以下这些工具包
1.安装cmake
下载:https://cmake.org/files/v3.6/cmake-3.6.3.tar.gz
编译安装cmake
[root@localhost ~]# tar -zxf cmake-3.6.3.tar.gz && cd cmake-3.6.3 [root@localhost cmake-3.6.3]# ./configure [root@localhost cmake-3.6.3]# make && make install ###[默认安装路径是/usr/local/bin] [root@localhost cmake-3.6.3]# cmake –version ###[查看cmake版本] cmake version 3.6.3 CMake suite maintained and supported by Kitware (kitware.com/cmake). [root@localhost cmake-3.6.3]# cd .. && rm -rf cmake-3.6.3*
2.安装ncurses
[root@localhost ~]# yum -y install ncurses-devel
3.安装bison
下载: http://git.typecodes.com/libs/ccpp/bison-3.0.tar.gz
编译安装bison
#######bison:GNU分析器生成器 [root@localhost ~]# wget -c http://git.typecodes.com/libs/ccpp/bison-3.0.tar.gz [root@localhost~]# tar -zxf bison-3.0.tar.gz && cd bison-3.0/ && ./configure [root@localhost bison-3.0]# make && make install [root@localhost bison-3.0]# cd ~ && rm -rf bison-3.0*
4.安装boost
下载 boost_1_59_0并上传到/tmp/目录(一定要这个版本以上,尝试多次安装1.58.0版本导致后面安装mysql5.7.9的时候会报找不到这个库的错误)
[root@typecodes ~]# tar -zxf boost_1_59_0.tar.gz && cd boost_1_59_0/ [root@localhost boost_1_59_0]# ./bootstrap.sh [root@localhost boost_1_59_0]# ./b2 stage threading=multi link=shared 这里会报如下错误: common.copy stage/lib/libboost_wave.so.1.59.0 ln-UNIX stage/lib/libboost_wave.so ...failed updating 29 targets... ...skipped 6 targets... ...updated 139 targets... ####这个命令等编译安装完mysql后再删除 [root@localhost boost_1_59_0]# cd ~ && rm -rf boost_1_59_0*
安装mysql
下载在官网下载自己所需版本,选择Community Server -> (Select Platform)Source Code
Generic Linux (Architecture Independent), Compressed TAR Archive
Includes Boost Headers (mysql-boost-5.7.17.tar.gz) (包含boost的源码包)
下载地址:https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-boost-5.7.17.tar.gz
5.7版本在cmake的时候需要增加两个参数 -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/tmp/mysql-5.7.17/boost
编译安装mysql
[root@localhost tmp]# tar -zxf mysql-boost-5.7.17.tar.gz
[root@localhost tmp]# cd mysql-5.7.17
[root@localhost mysql-5.7.17]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/var/mysql/data -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 -DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 -DENABLE_DOWNLOADS=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_DEBUG=0 -DMYSQL_MAINTAINER_MODE=0 -DWITH_SSL:STRING=bundled -DWITH_ZLIB:STRING=bundled -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/tmp/mysql-5.7.17/boost
编译参数解释
编译的时候删掉后面的中文,
参数解析:
-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的通讯目录]
-DMYSQL_TCP_PORT=3306 \ [MySQL的监听端口]
-DENABLED_LOCAL_INFILE=1 \ [启用加载本地数据]
-DENABLE_DOWNLOADS=1 \ [编译时允许自主下载相关文件]
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DEXTRA_CHARSETS=all \ [使MySQL支持所有的扩展字符]
-DDEFAULT_CHARSET=utf8 \ [设置默认字符集为utf8]
-DDEFAULT_COLLATION=utf8_general_ci \ [设置默认字符校对]
-DWITH_DEBUG=0 \ [禁用调试模式]
-DMYSQL_MAINTAINER_MODE=0 \
-DWITH_SSL:STRING=bundled \ [通讯时支持ssl协议]
-DWITH_ZLIB:STRING=bundled [允许使用zlib library] \
-DDOWNLOAD_BOOST=1 \
-DWITH_BOOST=/tmp/boost_1_59_0 [如果下载的包不包含boost用这个]
-DWITH_BOOST=/tmp/mysql-5.7.17/boost [如果下载的包包含boost用这个]
开始make命令编译时,比较耗内存和cpu,起初分配给虚拟机是4核1G内存,编译到50%左右卡住了,无奈中断把内存改为2G就顺利编译,耗时差不多1个小时。如果主机内存在1G并且没有设置linux交换空间,不建议编译安装mysql-5.7
[root@localhost mysql-5.7.17]# make and make install
查看MySQL的安装目录/usr/local/mysql/下面是否生成了相关目录文件(最重要的当然是bin、sbin和lib目录)。如果lib目录下面没有生成如图所示的.so动态库文件和.a静态库文件,那么说明安装不成功(即使成功了也可能会导致php进程无法找到mysql的相关库文件)
配置MySQL的配置文件my.cnf
先把编译生成的my.cnf文件备份,再配置my.cnf
[root@localhost ~]# cd /etc
[root@localhost ~]# cp my.cnf my.cnf_bak
[root@localhost ~]# vim ./my.cnf
配置来自:https://github.com/vfhky/mylnmp/blob/master/MySql/my.cnf
# 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/lib/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 = /var/lib/mysql/data
port=3306
server-id = 1
socket=/var/lib/mysql/mysql.sock
character-set-server = utf8
log-error = /var/lib/mysql/log/error.log
pid-file = /var/lib/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
#[mysqld]
#datadir=/var/lib/mysql
#socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
#symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
#[mysqld_safe]
#log-error=/var/log/mariadb/mariadb.log
#pid-file=/var/run/mariadb/mariadb.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
添加mysql的环境变量
将MySQL编译生成的bin目录添加到当前Linux系统的环境变量中
[root@localhost ~]# echo -e '\n export PATH=/usr/local/mysql/bin:$PATH \n' >> /etc/profile
[root@localhost ~]# source /etc/profile
创建MySQL数据库文件存放路径及相关安全配置
在Linux主机上创建一个目录/var/lib/mysql/data,用于存放MySQL的数据库文件。同时设置其用户和用户组为之前创建的mysql,权限为700。这样其它用户是无法进行读写的,尽量保证数据库的安全。
[root@localhost ~]# mkdir -p/var/lib/mysql/data && chown -R root:mysql /usr/local/mysql
[root@localhost ~]# chown -R mysql:mysql /var/lib/mysql/data
[root@localhost ~]# chmod -R go-rwx /var/lib/mysql/data
创建MySQL日志和数据存放目录
下面配置的MySQL日志存放目录以及权限都是根据前面my.cnf文件写的,也就是两者需要保持一致。
[root@localhost ~]# mkdir -p/var/lib/mysql && mkdir -p /var/lib/mysql/log
[root@localhost ~]# chown -R mysql:mysql /var/lib/mysql
初始化MySQL自身的数据库
在MySQL安装目录的/usr/local/mysql/bin路径下,执行mysqld命令,初始化MySQL自身的数据库。
[root@localhost bin]# mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/var/lib/mysql/data
执行完后,可以通过[root@localhost bin]# ls -lrt /var/lib/mysql/data/ 命令查看是否生成了MySQL自身的数据库文件。
设置开机启动
######配置开机自启动
[root@localhost ~]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@localhost ~]# chmod +x /etc/init.d/mysqld #增加可执行权限
[root@localhost ~]# chkconfig --add mysqld #添加到sysV服务
[root@localhost ~]# chkconfig mysqld on
启动MySQL服务
在完成上面的操作后,就可以正式使用MySQL服务了。启动MySQL进程服务的命令如下:
[root@localhost ~]# mysqld_safe --user=mysql --datadir=/var/lib/mysql/data --log-error=/var/lib/mysql/log/error.log [直接回车]
[1] 10274
150513 21:28:16 mysqld_safe Logging to ‘/var/lib/mysql/log/error.log’.
150513 21:28:16 mysqld_safe Starting mysqld daemon with databases from /mydata/mysql/data
######y
[root@localhost ~]# service mysqld start
Starting MySQL.. SUCCESS!
[root@localhost ~]#
然后使用下面这命令ps -ef | grep mysql和netstat -tunpl | grep 3306查看MySQL服务进程和端口监听情况
配置数据库密码
[root@localhost ~]# mysql_secure_installation
Securing the MySQL server deployment.
Connecting to MySQL using a blank password.
VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?
Press y|Y for Yes, any other key for No: y
There are three levels of password validation policy:
LOW Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 1
Please set the password for root here.
New password: Test764552..
Re-enter new password: Test764552..
#这里的密码应包含大小写字母、数字、和标点符号,不然可能会不让通过
Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : yy^H
Success.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : n
... skipping.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.
All done!
配置端口3306允许外部访问
centos 7 中使用的是firewall-cmd命令
[root@localhost ~]# firewall-cmd --list-all-zones #查看所有的zone信息
[root@localhost ~]# firewall-cmd --get-default-zone #查看默认zone是哪一个
[root@localhost ~]# firewall-cmd --zone=internal --change-zone=p3p1 #临时修改接口p3p1所属的zone为internal
[root@localhost ~]# firewall-cmd --add-service=http #暂时开放http
[root@localhost ~]# firewall-cmd --permanent --add-service=http #永久开放http
[root@localhost ~]# firewall-cmd --zone=public --add-port=80/tcp --permanent #在public中永久开放80端口
[root@localhost ~]# firewall-cmd --permanent --zone=public --remove-service=ssh #从public zone中移除服务
[root@localhost ~]# firewall-cmd --reload #重新加载配置
打开3306端口(因为nginx中已经设置了默认zone public,所以这里可以直接设置,如果没有设置,需要按nginx中那样配置)
firewall-cmd --add-port=3306/tcp --permanent
[root@localhost ~]# firewall-cmd --reload #重新加载配置
顺便提一下另外一个方法,但是不建议采用该方法:
方法二:
1.下载对应当前系统版本的mysql rpm包(package)
在mysql官网下载rpm包安装,网址http://dev.mysql.com/downloads/repo/yum/
2.选择第一个下载,然后传入服务器;或者获取rpm包地址直接服务器上用wget下载;然后安装rpm包,
wget http://repo.mysql.com//mysql57-community-release-el7-9.noarch.rpm
3.建立mysql的yum仓库(默认yum是没有nginx的)
rpm -ivh mysql57-community-release-el7-9.noarch.rpm
4.然后yum安装
yum install mysql