mysql 编译安装


1、创建mysql帐号

[root@localhost ~]# useradd -s /sbin/nologin -M mysql


2、获取安装包

http://mirrors.sohu.com/mysql/

选择自己需要的版本(注意和二进制软件包名字的差异)

格式包名
mysql源码mysql-5.7.18.tar.gz
mysql二进制mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz

3、编译安装

(1)下载安装包

[root@localhost ~]# mkdir /opt/soft/

[root@localhost ~]# cd /opt/soft/

[root@localhost soft]# wget -q http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.18.tar.gz

(2)解压、配置

解压安装包

[root@localhost soft]# tar zxf mysql-5.7.18.tar.gz


建立目录

[root@localhost soft]# mkdir /opt/mysql57/

[root@localhost soft]# mkdir /opt/mysql57/data


MySQL 5.5版本以后,使用CMake编译工具

安装组件
[root@localhost soft]# yum -y install cmake make gcc gcc-c++ autoconf automake ncurses-devel libaio-devel bison-devel openssl-devel zlib-devel readline-devel 

编译配置:

可参考官方文档https://dev.mysql.com/doc/refman/5.7/en/source-configuration-options.html

-DCMAKE_INSTALL_PREFIX指定mysql安装文件目录
-DMYSQL_DATADIR指定mysql数据文件目录
-DSYSCONFDIR指定mysql配置文件目录
-DMYSQL_UNIX_ADDR指定mysql.sock路径
-DMYSQL_TCP_PORT指定mysql端口
-DDEFAULT_CHARSET指定默认字符集
-DDEFAULT_COLLATION指定默认排序规则(utf8_general_ci快速)
-DENABLED_LOCAL_INFILE从本地导入数据
-DWITH_INNOBASE_STORAGE_ENGINE安装innodb存储引擎
-DWITH_ARCHIVE_STORAGE_ENGINE安装archive存储引擎
-DWITH_BLACKHOLE_STORAGE_ENGINE安装blackhole存储引擎
-DWITHOUT_EXAMPLE_STORAGE_ENGINE不安装example存储引擎
-DWITHOUT_FEDERATED_STORAGE_ENGINE不安装federated存储引擎
-DWITHOUT_PARTITION_STORAGE_ENGINE不安装partition存储引擎
-DWITH_SSL启用ssl库支持
-DWITH_ZLIB启用zlib库支持
-DWITH_READLINE启用readline库支持
-DWITH_LIBWRAP关闭libwrap库支持
-DEXTRA_CHARSETS安装所有字符集
-DDOWNLOAD_BOOSTMySQL5.7.5后Boost库是必需

如果要重新配置要执行以下命令:

make clean 

rm CMakeCache.txt 


cmake . -DCMAKE_INSTALL_PREFIX=/opt/mysql57 \

-DMYSQL_DATADIR=/opt/mysql57/data \

-DSYSCONFDIR=/etc \

-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \

-DDEFAULT_CHARSET=utf8 \

-DDEFAULT_COLLATION=utf8_general_ci \

-DENABLED_LOCAL_INFILE=1 \

-DWITH_INNOBASE_STORAGE_ENGINE=1 \

-DWITH_ARCHIVE_STORAGE_ENGINE=1 \

-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \

-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \

-DWITHOUT_FEDERATED_STORAGE_ENGINE \

-DWITHOUT_PARTITION_STORAGE_ENGINE \

-DWITH_SSL=system \

-DWITH_ZLIB=system \

-DWITH_READLINE=1 \

-DWITH_LIBWRAP=0 \

-DDOWNLOAD_BOOST=1 \

-DWITH_BOOST=/usr/local/boost


(3)编译安装

[root@localhost mysql-5.7.18]# make -j `grep processor /proc/cpuinfo | wc -l`

[root@localhost mysql-5.7.18]# make install

[root@localhost ~]# ls /opt/mysql57/

bin  COPYING  docs  include  lib  man  mysql-test  README  share  support-files

安装成功!


(4)建立配置文件

注意事项:

  1、MySQL5.5数据库默认为用户提供了多个配置文件模板,但是MySQL5.5之后的support-files目录下已经没有配置文件模板了

 2、在CentOS6.6版操作系统最小化安装完成后,在/etc目录下会存在一个my.cnf,需要将此文件更名为其他的名字,如:/etc/my.cnf.bak,否则,该文件会干扰源码安装的MySQL的正确配置,造成无法启动。


[root@localhost ~]# mv /etc/my.cnf /etc/my.cnf.bak

#在启动MySQL服务时,会按照一定的顺序搜索my.cnf,先在/etc目录下找,找不到则会搜索"$basedir/my.cnf"

[root@localhost ~]# vi /etc/my.cnf

[client]

port=3306

socket=/tmp/mysql.sock


[mysqld]

user=mysql

port=3306

socket=/tmp/mysql.sock

character-set-server=utf8

collation-server=utf8_general_ci

basedir=/opt/mysql57/

datadir=/opt/mysql57/data/

tmpdir=/opt/mysql57/data/

pid-file=/opt/mysql57/data/mysql.pid

log_error=/opt/mysql57/data/mysql.err


server-id = 137

log_bin = /opt/mysql57/data/binlog


slow_query_log = ON

long_query_time = 2

slow_query_log_file = /opt/mysql57/data/query_log

log_queries_not_using_indexes = ON

以上只是简单示例


(5)授权mysql用户

[root@localhost ~]# chown -R mysql.mysql /opt/mysql57/


(6)初始化数据库

[root@localhost ~]# /opt/mysql57/bin/mysqld --initialize --basedir=/opt/mysql57/ --datadir=/opt/mysql57/data/ --user=mysql


[root@localhost data]# tail -f mysql.err 

2017-06-29T13:21:35.392220Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).

2017-06-29T13:21:36.231983Z 0 [Warning] InnoDB: New log files created, LSN=45790

2017-06-29T13:21:36.364585Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.

2017-06-29T13:21:36.434567Z 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: e09a3e70-5ccd-11e7-9dd2-c81f66c50de9.

2017-06-29T13:21:36.435687Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.

2017-06-29T13:21:36.437654Z 1 [Note] A temporary password is generated for root@localhost: Dh<pE9py7igK

初始化之后的密码文件也保存在这里一份

[root@localhost mysql57]# cat /root/.mysql_secret

# Password set for user 'root@localhost' at 2017-06-29 19:21:38 

Dh<pE9py7igK

(7) 建立启动脚本

[root@localhost mysql57]# cp support-files/mysql.server /etc/init.d/mysqld

[root@localhost mysql57]# chmod 755 /etc/init.d/mysqld

[root@localhost mysql57]# vi /etc/init.d/mysqld

basedir=/opt/mysql57/

datadir=/opt/mysql57/data/

mysqld_pid_file_path=/opt/mysql57/data/


[root@localhost data]# /etc/init.d/mysqld  start

Starting MySQL. SUCCESS! 


mysql服务开机启动

[root@localhost mysql]# chkconfig --add mysqld

[root@localhost mysql]# chkconfig mysqld on


设置环境变量

[root@localhost data]# echo 'export PATH=$PATH:/opt/mysql57/bin' >> /etc/profile

[root@localhost data]# source /etc/profile

[root@localhost ~]# echo $PATH     

/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/mysql57/bin:/root/bin

[root@localhost ~]# mysql -uroot  -p

mysql> show databases;

ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.


mysql> set password=password('123456');

Query OK, 0 rows affected, 1 warning (0.01 sec)


mysql> alter user 'root'@'localhost' password expire never;

Query OK, 0 rows affected (0.01 sec)


mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)


另一种:

grant all privileges on *.* to root@'%' identified by "123456" with grant option;


注意点:

安装的依赖库的说明

1> cmake

MySQL5.5后使用cmake跨平台工具预编译源码,用于设置mysql的编译参数。如:安装目录、数据存放目录、字符编码、排序规则等。安装最新版本即可。

2> make

mysql源代码是由C和C++语言编写,在linux下使用make对源码进行编译和构建,要求必须安装make 3.75或以上版本

3> gcc4.4.6

GCC是Linux下的C语言编译工具,mysql源码编译完全由C和C++编写,要求必须安装GCC4.4.6或以上版本

4> Boost1.59.0

mysql源码中用到了C++的Boost库,要求必须安装boost1.59.0或以上版本

5> bison2.1

Linux下C/C++语法分析器

6> ncurses

字符终端处理库