CentOS7编译安装MySQL 5.7.31

一、安装编译环境及相关依赖

[root@keeplived_haproxy_mysql ~]# yum install -y cmake bison bison-devel libaio-devel gcc gcc-c++ git ncurses-devel openssl openssl-devel wget vim net-tools bash-comp* 

二、下载源码

本次安装的是MySql5.7.28版本,下载带有boost库的MySQL源码

[root@keeplived_haproxy_mysql ~]# cd /opt/
[root@keeplived_haproxy_mysql opt]# wget https://mirrors.huaweicloud.com/mysql/Downloads/MySQL-5.7/mysql-boost-5.7.31.tar.gz

备注:Mysql5.7版本更新后有很多变化,比如json等,连安装都有变化,他安装必须要BOOST库,不过mysql的官网源码有带boost库的

源码和不带boost库的源码两种,因此有两种安装方式,其实都是一样的,仅仅是不带boost库源码的需要单独安装boost

三、解压

[root@keeplived_haproxy_mysql opt]# tar xf mysql-boost-5.7.31.tar.gz 
[root@keeplived_haproxy_mysql opt]#cd mysql-5.7.31/
[root@keeplived_haproxy_mysql opt]# ll

四、进入MySQL源文件目录进行预编译操作

[root@keeplived_haproxy_mysql mysql-5.7.31]# cmake . -DBUILD_CONFIG=mysql_release \
-DINSTALL_LAYOUT=STANDALONE \
-DCMAKE_BUILD_TYPE=RelWithDebInfo \
-DENABLE_DTRACE=OFF \
-DWITH_EMBEDDED_SERVER=OFF \
-DWITH_INNODB_MEMCACHED=ON \
-DWITH_ZLIB=system \
-DWITH_PAM=ON \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DINSTALL_PLUGINDIR="/usr/local/mysql/mysql/lib/plugin" \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EDITLINE=bundled \
-DFEATURE_SET=community \
-DCOMPILATION_COMMENT="MySQL Server (GPL)" \
-DWITH_DEBUG=OFF \
-DWITH_BOOST=./boost

-DWITH_SSL=bundled 加此选项会报错,原因估计应该是openssl版本的问题。暂时没加。

.如果编译出现错误,请先删除CMakeCache.txt后,再重新编译:

rm -rf CMakeCache.txt

如果出现下面的提示就表示成功生成了编译环境:

-- Configuring done 
-- Generating done

六、编译并安装

[root@keeplived_haproxy_mysql mysql-5.7.31]# make && make install

无报错信息表示安装成功

八、初始化数据库

1、添加mysql数据库用户

[root@keeplived_haproxy_mysql mysql-5.7.31]# useradd -s /sbin/nologin mysql

2、新建数据库文件夹及日志文件夹,并更改用户为mysql

[root@keeplived_haproxy_mysql mysql-5.7.31]# mkdir /data/mysql -p
[root@keeplived_haproxy_mysql mysql-5.7.31]# chown -R mysql.mysql /data/mysql/
[root@keeplived_haproxy_mysql mysql-5.7.31]# mkdir /var/log/mysql
[root@keeplived_haproxy_mysql mysql-5.7.31]# chown -R mysql. /var/log/mysql

3、修改配置文件

[root@keeplived_haproxy_mysql mysql-5.7.31]# vim /etc/my.cnf
[mysqld]
basedir=/usr/local/mysql
datadir=/data/mysql
socket=/tmp/mysql.sock

[mysqld_safe]
log-error=/var/log/mysql/mysql.log
pid-file=/var/log/mysql/mysql.pid

!includedir /etc/my.cnf.d

4、初始化数据库(方法一)

[root@keeplived_haproxy_mysql mysql-5.7.31]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql

初始化数据库(方法二)

[root@master ~]# /usr/local/mysql/bin/mysql_install_db --basedir=/usr/local/mysql --datadir=/data/mysql --user=mysql

初始化成功后会生成默认密码

查看数据是否生成

九、配置启动文件及环境变更

[root@keeplived_haproxy_mysql ~]# cp /opt/mysql-5.7.31/support-files/mysql.server.sh /etc/init.d/mysqld57
[root@keeplived_haproxy_mysql ~]# chmod a+x /etc/init.d/mysqld57 

修改启动文件

[root@keeplived_haproxy_mysql ~]# vim /etc/init.d/mysqld57
basedir=/usr/local/mysql
datadir=/data/mysql
mysqld_pid_file_path=/tmp/mysqld57.pid

启动服务

[root@keeplived_haproxy_mysql ~]# /etc/init.d/mysqld57 start
Starting MySQL.Logging to '/var/log/mysql/mysql.log'.
 SUCCESS! 

查看日志文件

查看服务是否启动

可以看到提示,已经成功启动。

设置开机启动

[root@keeplived_haproxy_mysql ~]# chkconfig --add mysqld57
[root@keeplived_haproxy_mysql ~]# chkconfig mysqld57 on

配置MySQL环境变量

[root@keeplived_haproxy_mysql ~]# cat>>/etc/profile.d/mysql.sh<<EOF
export PATH=/usr/local/mysql/bin:$PATH
EOF
[root@keeplived_haproxy_mysql ~]# source /etc/profile.d/mysql.sh

十一、修改root的初始密码

MySQL从5.7开始不支持安装后使用空密码进行登录,使用初始化后生成的密码进行登陆。

Sh2icR_xgJhD

[root@keeplived_haproxy_mysql ~]# mysql -u root -pSh2icR_xgJhD

登陆Mysql并修改初始密码

使用如下方法修改密码

mysql> alter user 'root'@'localhost' identified by 'Aa123456';
Query OK, 0 rows affected (0.00 sec)

其中Aa123456为新设置的密码。

第二种方法:(没法生成密码的情况下使用如下情况来修改密码)

编辑/etc/my.cnf文件加入skip-grant-tables

[root@lnmp-zabbix ~]# vim /etc/my.cnf
skip-grant-tables
[root@lnmp-zabbix ~]# /etc/init.d/mysqld57 restart
[root@lnmp-zabbix ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.20 MySQL Server (GPL)

Copyright (c) 2000, 2017, 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> UPDATE mysql.user SET authentication_string=PASSWORD('Aa123456') where USER='root';           
Query OK, 1 row affected, 1 warning (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 1

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

删除/etc/my.cnf里边的skip-grant-tables并重新启动

[root@lnmp-zabbix ~]# vim /etc/my.cnf
# skip-grant-tables
[root@lnmp-zabbix ~]# /etc/init.d/mysqld57 restart

至此,mysql5.7.20安装完成

欢迎加入运维工程师交流群一起交流

发表评论

登录... 后才能评论