Linux编译安装Mysql5.7.9

前言

Linux下默认安装mysql会在默认目录安装生成相关文件,可配置性差,当然也可以下载免安装版,但总的来说实在不能随心所欲。正好,此前从未进行过编译方式安装mysql,正好练手。期间报错无数,查询无数前辈总结总算安装完成。因未记录引用地址,此处无法说明感谢,如有雷同,并非巧合!

安装依赖包

1
yum -y install gcc gcc-c++ ncurses ncurses-devel cmake autoconf automake zlib libxml libgcrypt libtool bison

清理已经存在的mysql

查询mysql安装

1
rpm -qa | grep -i mysql

删除

1
rpm -e 

查询mysql服务

1
chkconfig --list | grep -i mysql

删除服务

1
chkconfig --list | grep -i mysql 

mysql分散文件查询及删除

1
2
whereis mysql
rm -rf

下载源码包、解压并安装bootst

1
2
3
4
5
6
7
8
9
cd ~/Download
wget http://downloads.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz
wget http://cdn.mysql.com/Downloads/MySQL-5.7/mysql-5.7.9.tar.gz
tar xzf boost_1_59_0.tar.gz
tar xzf mysql-5.7.9.tar.gz
cd boost_1_59_0
./bootstrap.sh
./b2 stage threading=multi link=shared
./b2 install threading=multi link=shared

创建mysql用户、组、mysql安装目录、数据文件目录拥有者及权限

1
2
3
4
groupadd -r mysql
useradd -r -g mysql -s /sbin/nologin -M mysql
mkdir -p /home/mysql/data
chown -R root:mysql /home/mysql && chown -R mysql:mysql /home/mysql/data && chmod -R 755 /home/mysql/data

预编译

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
cd mysql-5.7.9
cmake . -DCMAKE_INSTALL_PREFIX=/home/mysql \ #MySQL安装的根目录,默认/usr/local/mysql
-DMYSQL_DATADIR=/home/mysql/data \ #MySQL数据库文件存放目录
-DDOWNLOAD_BOOST=1 \ #启用BOOST库从MySQL 5.7.5开始Boost库是必需的
-DWITH_BOOST=/usr/local/include \ #BOOST库地址
-DSYSCONFDIR=/home/mysql \ #MySQL配置文件目录
-DMYSQL_USER=mysql \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DENABLED_LOCAL_INFILE=1 \ #指定是否允许本地执行LOAD DATA INFILE,默认OFF
-DENABLE_DTRACE=0 \
-DWITH_SSL=bundled \ #Centos需要
-DDEFAULT_CHARSET=utf8 \ #MySQL缺省字符集,默认latin1
-DDEFAULT_COLLATION=utf8_general_ci \ #MySQL缺省的校对规则,默认latin1_general_ci
-DWITH_EMBEDDED_SERVER=1

编译并安装

1
2
3
4
5
make -j `grep processor /proc/cpuinfo | wc -l`
make install
如果出错,重新运行配置,需要删除CMakeCache.txt 文件
make clean
rm -f CMakeCache.txt

设置配置

复制配置文件并加入以下参数

1
2
3
4
5
6
7
8
9
10
11
12
13
cp /home/mysql/support-files/my-default.cnf /home/mysql/my.cnf
vim /home/mysql/my.cnf
basedir = /home/mysql
datadir = /home/mysql/data
pid_file = /home/mysql/mysql.pid
log_error = /home/mysql/mysql.log
port = 3306
character_set_server = utf8
explicit_defaults_for_timestamp=true

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,
NO_AUTO_CREATE_USER
show_compatibility_56=ON

复制启动文件

1
cp /home/mysql/support-files/mysql.server /etc/init.d/mysqld

添加执行权限及自动启动服务

1
2
3
4
chmod +x /etc/init.d/mysqld
chkconfig --list mysqld
chkconfig --add mysqld
chkconfig --level 345 mysqld on

设置环境变量

1
2
vim ~/.bash_profile
PATH=/home/mysql/bin:$PATH

初始化数据库

1
/home/mysql/bin/mysqld --initialize --user=mysql --basedir=/home/mysql --datadir=/home/mysql/data 

此命令会在日志文件/home/mysql/mysql.log中生成密码

启动mysql服务及检查

1
ps -ef | grep mysqll && netstat -tunpl | grep 3306

修改root默认密码及配置远程访问

1
2
3
4
5
6
mysql -u root -p
重置密码
mysql> SET PASSWORD = PASSWORD('myroot'); 或
/home/mysql/bin/mysqladmin -u root -p password
设置root用户远程访问
mysql> grant all privileges on *.* to 'root'@'%' identified by 'myroot' with grant option;