liunx7下centos6.8编译安装mysql-5.7.18

来源:互联网 发布:重生之女土匪知乎 编辑:程序博客网 时间:2024/06/03 22:55

一,准备工作

如果cmake已经首先安装好,那么编译Mysql,先对当前目录.查看依赖关系 

cmake –graphviz

1,在当前环境下查看3306端口是否被防火墙开启:

firewall-cmd --query-post=3306/tcp

如果为no,则执行以下命令开启3306端口通过防火墙

firewall-cmd --permanent --add-post=3306/tcp

执行后显示success

然后重启防火墙

systemctl stop firewalld.service

systemctl start firewalld.service

再次查询3306端口是否开启:

firewall-cmd -query-post=3306/tcp

显示为yes

2,创建mysql运行的用户组和用户

groupadd -r mysql && useradd -r -g mysql -s /bin/false -M mysql

3,编译安装所需的依赖包

######CMake编译工具

cd /usr/local/src

wget https://cmake.org/files/v3.8/cmake-3.8.2.tar.gz  --可用最新版本

tar -zxf cmake-3.8.2.tar.gz

cd cmake-3.8.2.tar.gz

./configure --prefix=/usr/local/related/cmake

make && make install

######Ncurses:提供功能键定义(快捷键),屏幕绘制以及基于文本终端的图形互动功能的动态库

安装mysql 需要的为ncurses-devel

它只有rpm包所以只能安装rpm包或者使用yum安装 yum -y install ncurses-devel

这里演示下rpm安装,如若需要指定rpm的安装目录:

可以使用prefix参数。
rpm -i –prefix=/usr/bin abc.rpm将abc.rpm包安装到/usr/bin目录下。

但是这个ncurses-devel不支持指定目录,会报错:

警告:ncurses-devel-6.0-10.20170520.fc27.x86_64.rpm: 头V3 RSA/SHA256 Signature, 密钥 ID f5282ee4: NOKEY
错误:软件包 ncurses-devel 不能重定位

所以只能默认安装

rpm -ivh ncurses-devel-6.0-10.20170520.fc27.x86_64.rpm

安装它需要N多依赖,所以还是yum吧

------------------------------------------------------------------------------------------------------------------------------------

下面的对于ncurses安装没用。

#cd /usr/local/src

#wget http://ftp.gnu.org/gnu/ncurses/ncurses-6.0.tar.gz --可用最新版本

#tar -zxf ncurses-6.0.tar.gz

#cd ncurses-6.0

#./configure --prefix=/usr/local/related/ncuress

#make && make install

#######bison:GNU分析器生成器

cd /usr/local/src

wget http://ftp.gnu.org/gnu/bison/bison-3.0.4.tar.gz

tar -zxf bison-3.0.4.tar.gz

cd bison-3.0.4

./configure --prefix=/usr/local/related/bison

make && make install

如果没有安装M4则会报:

GNU M4 1.4.6 or later is required; 1.4.16 or newer is recommended.

先编译安装GNU M4

cd /usr/local/src

wget ftp://ftp.gnu.org/gnu/m4/m4-1.4.6.tar.gz --可用最新版

tar -zxf m4-1.4.6.tar.gz

cd m4-1.4.6

./configure --prefix=/usr/local/related/m4

make && make install

然后需要将m4加入环境变量$PATH

vim /etc/profile

export PATH=“$PATH:/usr/local/related/m4/bin”

source /etc/profile

最后再次编译安装bison即可

make clean

./configure --prefix=/usr/local/related/bison

make && make install

3,在mysql5.7后编译安装需要boost的支持

#######Boost库:一个开源可移植的C++库,是C++标准化进程的开发引擎之一

cd /usr/local/src

wget https://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz/download

tar -zxf boost_1_59_0.tar.gz -C /usr/local/related

只解压出即可,不用编译安装

因为ncurses和bison均没有使用yum安装,所以在编译mysql时必须指定ncurses的位置否则会报错:

Could NOT find Curses (missing: CURSES_LIBRARY CURSES_INCLUDE_PATH

Curses library not found. 

Please install appropriate package,

remove CMakeCache.txt and rerun cmake.On Debian/Ubuntu, packagename is libncurses5-dev, on Redhat and derivates it isncurses-devel.


#必须解决他:

#用-D参数定义宏,指定头文件和库的所在目录
#即在编译mysql时cmke加上-DCURSES_LIBRARY=/usr/local/related/ncurses/lib/libncurses.a -DCURSES_INCLUDE_PATH=/usr/local/related/ncurses/include即可

解决这个问题后,对于bison也会报找不到的提示但是不影响编译进行,但是还是要解决,只需将bison的bin目录添加到环境变量即可。

vim /etc/profile

export PATH="$PATH:/usr/local/related/bison/bin"

最后开始编译mysql

cmake

-DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DMYSQL_TCP_PORT=3306 -DMYSQL_USER=mysql -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_DEBUG=0 -DWITH_SSL:STRING=bundled -DWITH_ZLIB:STRING=bundled -DMYSQL_MAINTAINER_MODE=0 -DENABLE_DOWNLOADS=1 -DDOWNLOAD_BOOST=1 -DCURSES_LIBRARY=/usr/local/related/ncurses/lib/libncurses.a -DCURSES_INCLUDE_PATH=/usr/local/related/ncurses/include -DWITH_BOOST=/usr/local/related/boost_1_59_0/

如果编译失败记得删除掉源码包目录里面的CMakeCache.txt文件后重新进行编译。

编译完毕后执行make && make install 即可。

编译安装时间较长,因为我们指定编译参数得时候,指定了mysql得数据存储的目录,这个时候可以先把目录都创建出来:

mkdir /usr/local/mysql
mkdir /usr/local/mysql/data
mkdir /usr/local/mysql/logs
mkdir /usr/local/mysql/pids

创建完毕后将目录的所有者变更为mysql

chown -R mysql:mysql /usr/local/mysql   -R为递归的将目录下的文件夹和文件统一设定

当make install 完成后需配置/etc/my.cnf文件,最终如下:

[client]port    = 3306socket    = /usr/local/mysql/mysql.sock[mysqld]user = mysql#bind-address = 10.0.1.108port  = 3306pid-file = /usr/local/mysql/pids/mysqld.pidsocket  = /usr/local/mysql/mysql.sockbasedir = /usr/local/mysqldatadir = /usr/local/mysql/data#tmpdir  = /usr/local/mysql/tmp#skip-grant-tables#rpl_semi_sync_master_enabled=1#rpl_semi_sync_master_timeout=1000#rpl_semi_sync_slave_enabled=1sql_mode        = NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES# Log#general-log = 1#general_log_file = /mysqllog/bitvclog/3306/mysql.logslow-query-log = 1slow-query-log-file = /usr/local/mysql/logs/slow.loglog-slow-admin-statementslog-slow-slave-statementslong-query-time = 1log-error = /usr/local/mysql/logs/mysqld.loglog-warnings  = 2back_log = 512max_connections = 2048max_connect_errors = 10000connect_timeout = 60skip-external-lockingskip-name-resolve#skip-grant-tables#skip-symbolic-links#skip-innodb_checksums#skip-innodb_doublewriteexplicit_defaults_for_timestampkey_buffer_size = 16Mmax_allowed_packet = 32Mtable_open_cache = 512sort_buffer_size = 8Mread_buffer_size = 4Mread_rnd_buffer_size = 32Mmyisam_sort_buffer_size = 1Mthread_cache_size = 32query_cache_size = 128Mquery_cache_type = 0server-id = 1283306log-bin                 = mysql-binbinlog_format           = mixedsync_binlog             = 1expire-logs-days        = 7max_binlog_size         = 256Msync_binlog                     = 30default-storage-engine = InnoDBinnodb_max_dirty_pages_pct = 90innodb_file_per_table = 1innodb_io_capacity = 100000character-set-server = utf8innodb_log_buffer_size = 8Minnodb_flush_log_at_trx_commit = 1innodb_lock_wait_timeout = 50[mysqldump]quickmax_allowed_packet = 32M[mysql]no-auto-rehash#safe-updates[myisamchk]key_buffer_size = 128Msort_buffer_size = 128Mread_buffer = 2Mwrite_buffer = 2M[mysqlhotcopy]interactive-timeout

主要是修改了数据存放目录,日志文件目录,socket目录等路径信息与前面所建目录一致。

如果不想使用默认加载顺序中的配置文件,可以在启动命令上增加 --defaults-file='path/to/my.cnf'来指定要使用的配置文件,但在我这里一直改动默认my.cnf的位置不成功,可能版本问题。所以还是放在了/etc/下面

4.性能调优
由于编译的时间是在太长了,这个地方的性能调优不涉及任何的sql优化,只针对一点:使用jemalloc来替换默认的内存管理
首先我们需要从网站上下载jemalloc,官网地址:
http://www.canonware.com/jemalloc/

从github上可以下载最新的源码:
https://github.com/jemalloc/jemalloc/releases

编译jemalloc:

./configure --libdir=/usr/local/libmakemake install

为了保证能够找到jemalloc库,我们需要设置一下库路径,

echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.confldconfig //执行这个命令可以刷新库路径

我们有两种方式来使用jemalloc来优化mysql
在编译的时候加入参数:

-DCMAKE_EXE_LINKER_FLAGS="-ljemalloc" -DWITH_SAFEMALLOC=OFF

然后重新编译一遍。
也可以修改 /usr/local/mysql/bin/mysqld_safe
在# executing mysqld_safe 下面加上

LD_PRELOAD=/usr/local/lib/libjemalloc.so
5.开机自启动的配置
通常我们需要重启服务器,最好在重启服务器的时候就启动mysql

首先将安装目录中的(注意是安装目录并非源码包目录)suppost-files/mysql.server 复制为/etc/init.d/mysqld

然后给予执行权限chmod a+x /etc/init.d/mysqld

加入系统服务 chkconfig --add mysqld

设置开机自启动 chkconfig mysqld on

6,将mysql服务加入系统环境变量 /etc/profile

vim /etc/profile

注意除了bin目录外还有lib目录也需要加入到环境变量

export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH

然后执行 source /etc/profile 来更新环境变量使其生效

7,随后我们也需要对数据库进行初始化操作。自 mysql5.7 开始,初始化系统表不再使用 mysql_install_db 工具, 而是使用 mysqld –initialize-insecure –user=mysql , 其中 –initialize 表示默认生成一个安全的密码, –initialize-insecure 表示不生成密码, 密码为空,注意这只是初始化而已,并不会启动mysqld

mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

执行命令后可能会出现几个warnning:

2017-07-17T03:36:18.954218Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-07-17T03:36:22.391313Z 0 [Warning] InnoDB: New log files created, LSN=45790
2017-07-17T03:36:22.847155Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2017-07-17T03:36:23.109030Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 1add7612-6aa1-11e7-8eb1-000c291881a9.
2017-07-17T03:36:23.112328Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2017-07-17T03:36:23.115175Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.

关闭第一个是说

从 5.6开始,timestamp 的默认行为已经是 deprecated 了。

在MySQL 5.6.6之前,TIMESTAMP的默认行为:

•TIMESTAMP列如果没有明确声明NULL属性,默认为NOT NULL。(而其他数据类型,如果没有显示声明为NOT NULL,则允许NULL值。)设置TIMESTAMP的列值为NULL,会自动存储为当前timestamp。
•表中的第一个TIMESTAMP列,如果没有声明NULL属性、DEFAULT或者 ON UPDATE,会自动分配 DEFAULT CURRENT_TIMESTAMP和ON UPDATE CURRENT_TIMESTAMP 属性。
•表中第二个TIMESTAMP列,如果没有声明为NULL或者DEFAULT子句,默认自动分配'0000-00-00 00:00:00′。插入行时没有指明改列的值,该列默认分配'0000-00-00 00:00:00′,且没有警告。

重启MySQL后错误消失,这时TIMESTAMP的行为如下:

•TIMESTAMP如果没有显示声明NOT NULL,是允许NULL值的,可以直接设置改列为NULL,而没有默认填充行为。
•TIMESTAMP不会默认分配DEFAULT CURRENT_TIMESTAMP 和 ON UPDATE CURRENT_TIMESTAMP属性。
•声明为NOT NULL且没有默认子句的TIMESTAMP列是没有默认值的。往数据表中插入列,又没有给TIMESTAMP列赋值时,如果是严格SQL模式,会抛出一 个错误,如果严格SQL模式没有启用,该列会赋值为'0000-00-00 00:00:00′,同时出现一个警告。(这和MySQL处理其他时间类型数据一样,如DATETIME)
(参见:http://www.jb51.net/article/71054.htm)

也就是 explicit_defaults_for_timestamp 关闭了 timestamp 类型字段锁拥有的一些会让人感到奇怪的默认行为,加入了该参数之后,如果还需要为 timestamp类型的字段指定默认行为,那么就需要显示的在创建表时显示的指定。explicit_defaults_for_timestamp 也就是这个意思:显示指定默认值为timestamp类型的字段。

要关闭这个警告需要修改/etc/my.cnf

mysqld
explicit_defaults_for_timestamp=true
其余警告自己翻译下即可,没有影响。

如果不是第一次初始化会报错如下:
[root@localhost ~]# mysqld --initialize-insecure --user=mysql --datadir=/usr/local/mysql/data --basedir=/usr/local/mysql
2017-07-17T04:06:09.586245Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting.
2017-07-17T04:06:09.586290Z 0 [ERROR] Aborting
意思是说data 目录应该为空不应有文件,所以将data下的文件删除即可。
初始化之后开启mysqld服务端
/etc/init.d/mysqld start
最后我们将root密码修改一下:

mysql -u rootmysql> use mysql;mysql>update user set authentication_string=password('123456') where user='root' and Host = 'localhost';mysql> FLUSH PRIVILEGES;

最后特别提醒注意的一点是,新版的mysql数据库下的user表中已经没有Password字段了

而是将加密后的用户密码存储于authentication_string字段
8.tips
执行make命令的时候可以加-j(多进程)参数来加快编译速度
make -j “cpu 核数”

比如已知CPU是4核心的,可以使用:
make -j 4

-


原创粉丝点击