天天看點

mysql源碼安裝配置_MySQL5.7.17源碼編譯安裝與配置

一、下载MySQL源码

官网: http://www.mysql.com

下载下来的文件为:mysql-boost-5.7.17.tar.gz

二、解压源码,准备环境

[[email protected] software]# tar -zxf mysql-boost-5.7.17.tar.gz

[[email protected] software]# ls

[[email protected] software]# cd mysql-5.7.17/

[[email protected] mysql-5.7.17]# ls

[[email protected] mysql-5.7.17]# vim README

[[email protected] mysql-5.7.17]# vim INSTALL

mysql源碼安裝配置_MySQL5.7.17源碼編譯安裝與配置
mysql源碼安裝配置_MySQL5.7.17源碼編譯安裝與配置

帮助文档里给我们的是一个网址,打开看看

mysql源碼安裝配置_MySQL5.7.17源碼編譯安裝與配置

可以用cmake的方式,这里我们看到这里只列出了几个选项,更多选项又被链接到了另外一个网址,打开看看

mysql源碼安裝配置_MySQL5.7.17源碼編譯安裝與配置

这么多选项,我们根据需要进行选择,先看看从cmake –help

mysql源碼安裝配置_MySQL5.7.17源碼編譯安裝與配置

这就尴尬了,没有安装cmake,看看本机yum源里的cmake版本

mysql源碼安裝配置_MySQL5.7.17源碼編譯安裝與配置

cmake跨平台工具是用来预编译mysql源码的,用于设置mysql的编译参数。如:安装目录、数据存放目录、字符编码、排序规则等。所以安装yum源里带的即可

[[email protected] mysql-5.7.17]# yum install -y cmake.x86_64

1

mysql源碼安裝配置_MySQL5.7.17源碼編譯安裝與配置

cmake –help似乎没提供什么我们需要的选项,还是参照mysql的官方文档来吧

mysql源碼安裝配置_MySQL5.7.17源碼編譯安裝與配置

编译环境

官方文档说明:http://dev.mysql.com/doc/refman/5.7/en/source-installation.html

从官方文档知:需要以下依赖库

make 3.75

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

gcc4.4.6

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

Boost1.59.0

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

bison2.1

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

ncurses

字符终端处理库

由于我们下载的源码包含boost,所以我们只需要安装其他的依赖库即可

[[email protected] mysql-5.7.17]# yum install -y make.x86_64 gcc-c++.x86_64 bison.x86_64 ncurses.x86_64

1

mysql源碼安裝配置_MySQL5.7.17源碼編譯安裝與配置

依赖库安装好了,我们将boost移至/usr/local下,方便使用

[r[email protected] mysql-5.7.17]# cp -r boost/boost_1_59_0/ /usr/local/

[[email protected] mysql-5.7.17]# ls /usr/local/boost_1_59_0/

boost

由于是源码安装,不同于玉米安装,不会自动帮我们创建用户,所以需要我们创建用户

[[email protected] mysql-5.7.17]# groupadd mysql #添加mysql用户组

[[email protected] mysql-5.7.17]# useradd -r -g mysql -s /bin/false mysql #添加mysql用户

1

2

三、编译安装

配置mysql预编译参数

[[email protected] mysql-5.7.17]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DWITH_BOOST=/usr/local/boost_1_59_0/ -DSYSCONFDIR=/etc -DEFAULT_CHARSET=utf8mb4 -DDEFAULT_COLLATION=utf8mb4_general_ci -DENABLED_LOCAL_INFILE=1 -DEXTRA_CHARSETS=all

DCMAKE_INSTALL_PREFIX:安装路径

DMYSQL_DATADIR:数据存放目录

DWITH_BOOST:boost源码路径

DSYSCONFDIR:my.cnf配置文件目录

DEFAULT_CHARSET:数据库默认字符编码

DDEFAULT_COLLATION:默认排序规则

DENABLED_LOCAL_INFILE:允许从本文件导入数据

DEXTRA_CHARSETS:安装所有字符集

mysql源碼安裝配置_MySQL5.7.17源碼編譯安裝與配置

预编译报错,提示remove CMakeCache.txt ,redhat还需要ncurses-devel , yum 安装之

[[email protected] mysql-5.7.17]# rm -rf CMakeCache.txt

[[email protected] mysql-5.7.17]# yun install -y ncurses-devel

继续执行cmake预编译

编译并安装

[[email protected] mysql-5.7.17]# make ##可加参数-j根据CPU核数指定编译时的线程数,可以加快编译速度。默认为1个线程编译,经测试四核CPU,2G的内存,单先程编译完需要20分钟左右。

[[email protected] mysql-5.7.17]# makeinstall

mysql源碼安裝配置_MySQL5.7.17源碼編譯安裝與配置
mysql源碼安裝配置_MySQL5.7.17源碼編譯安裝與配置

至此,编译安装完成,下面进行简单的配置使用

mysql简单配置

初始化系统数据库

[[email protected] mysql-5.7.17]# cd /usr/local/mysql/

[[email protected] mysql]# ll -d

drwxr-xr-x 10 root root 4096 Sep 23 22:45 .

[[email protected] mysql]# chown -R mysql:mysql .

[[email protected] mysql]# ll -d

drwxr-xr-x 10 mysql mysql 4096 Sep 23 22:45 .

[[email protected] mysql]# ./bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

2017-09-24T03:01:29.063554Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).

2017-09-24T03:01:29.068209Z 0 [ERROR] COLLATION 'utf8mb4_general_ci' is not valid for CHARACTER SET 'latin1'

2017-09-24T03:01:29.068253Z 0 [ERROR] Aborting

提示字符集有错

[[email protected] mysql]# vim /etc/my.cnf ##编辑:增加两个编码变量

#改成如下,如果没有该文件,可拷贝一份,命令如下

##cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cn

[client]

default-character-set=gbk ##增加的编码变量

[mysqld]

character_set_server=gbk ##增加的编码变量

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

user=mysql

在初始化即可

mysql源碼安裝配置_MySQL5.7.17源碼編譯安裝與配置

如果使用–initialize参数初始化系统数据库之后,会在~/.mysql_secret文件中生成root用户的一个临时密码,同时也在初始化日志中打印出来了,如下图(fy

mysql源碼安裝配置_MySQL5.7.17源碼編譯安裝與配置

配置mysql服务并启动服务

[[email protected] init.d]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

[[email protected] init.d]# chkconfig --add mysqld ## 添加到系统服务

[[email protected] init.d]# chkconfig mysqld on ## 开机启动

[[email protected] ~]# /etc/init.d/mysqld ##查看提供哪些用法

Usage: mysqld {start|stop|restart|reload|force-reload|status} [ MySQL server options ]

[[email protected] ~]# /etc/init.d/mysqld start ##测试开启服务

Starting MySQL. SUCCESS!

[[email protected] ~]# /etc/init.d/mysqld stop ##测试停止服务

Shutting down MySQL. SUCCESS!

[[email protected] ~]# /etc/init.d/mysqld start ##开启服务

[[email protected] ~]# ln -s /usr/local/mysql/bin/mysql /usr/local/bin/mysql ##添加软链接将mysql加入系统环境变量,或者通过配置环境变量,export PATH=/usr/local/mysql/bin:$PATH 或者vim /etc/profile

测试启动

[[email protected] ~]# mysql -uroot -pfy

-bash: !U95r: event not found ##秘密包含特殊字符,不能这样写

[[email protected] ~]# mysql -uroot -p

Enter password: ##输入密码

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) ##报错

[[email protected] ~]# ln -s /var/lib/mysql/mysql.sock /tmp ##软链接

[[email protected] ~]# mysql -uroot -p ##再来

Enter password:

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 6

Server version: 5.7.17

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> ##登陆成功

设置数据库密码

mysql> set password for [email protected]=password('mypasswd');

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

mysql>

mysql> exit

Bye

[[email protected] ~]# mysql -uroot -pmypasswd

mysql: [Warning] Using a password on the command line interface can be insecure.

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 7

Server version: 5.7.17 Source distribution

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

修改成功,可以进行数据库的操作了

mysql> show databases;

+--------------------+

| Database |

+--------------------+

| information_schema |

| mysql |

| performance_schema |

| sys |

+--------------------+

4 rows in set (0.00 sec)

mysql> CREATE database mydatabase

-> ;

Query OK, 1 row affected (0.00 sec)

mysql> SHOW databases

-> ;

+--------------------+

| Database |

+--------------------+

| information_schema |

| mydatabase |

| mysql |

| performance_schema |

| sys |

+--------------------+

5 rows in set (0.00 sec)

mysql> use mydatabase

Database changed

mysql> show tables

-> ;

Empty set (0.00 sec)

mysql>

mysql源碼安裝配置_MySQL5.7.17源碼編譯安裝與配置