CentOS 源码安装MySQL5.7

一、安装方式
源码安装,源码下载地址wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-boost-5.7.27.tar.gz。源码包名称mysql-boost-5.7.27.tar.gz,此版本包含boost库,在解压后的boost路径下。安装时,可以-DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/mysql-5.7.27/boost/指定boost路径

二、安装路径
基础路径 /usr/local/mysql
数据文件路径 /usr/local/mysql/data

三、安装依赖
yum -y install autoconf automake libtool cmake ncurses-devel openssl-devel lzo-devel zlib-devel gcc gcc-c++

四、添加用户信息
添加系统mysql组和mysql用户
添加系统mysql组 groupadd mysql
添加mysql用户 useradd -r -g mysql mysql (添加完成后可用id mysql查看)
切到mysql目录 cd /usr/local/mysql
修改当前目录拥有者为mysql用户 chown -R mysql.mysql ./

五、配置预编译
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DSYSCONFDIR=/etc -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 -DWITHOUT_FEDERATED_STORAGE_ENGINE=1 -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all -DENABLED_LOCAL_INFILE=1 -DWITH_READLINE=1 -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock -DMYSQL_TCP_PORT=3306 -DMYSQL_USER=mysql -DCOMPILATION_COMMENT="lq-edition" -DENABLE_DTRACE=0 -DOPTIMIZER_TRACE=1 -DWITH_DEBUG=1 -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/mysql-5.7.27/boost/
出现如下警告
CMake Warning:
Manually-specified variables were not used by the project:
MYSQL_USER
WITH_READLINE
-- Build files have been written to: /usr/local/mysql-5.7.27
忽略即可。

六、编译和安装
编译 make
安装 make install
如果服务器内存较小,可以使用如下指令
make -j `grep processor /proc/cpuinfo | wc -l` #编译时会消耗很大内存,小内存可能无法编译完成

七、初始化数据库
cd /usr/local/mysql/bin/
./mysqld --initialize --user=mysql
获取到密码 ,/7Xa7Y#hN_X

八、开启ssl功能
cd /usr/local/mysql/bin/
./mysql_ssl_rsa_setup

九、测试启动mysql
cd /usr/local/mysql/bin/
./mysqld_safe --user=mysql
出现如下错误

mysqld_safe error:log-error set to '/var/log/mariadb/mariadb.log',however file does not exists,Create writable for you 'mysql'.

是mysql用户没有/var/log/mariadb/mariadb.log权限
执行如下操作
mkdir /var/log/mariadb
touch /var/log/mariadb/mariadb.log
chown -R mysql:mysql /var/log/mariadb

从新执行./mysqld_safe --user=mysql

执行成功

 

十、启动mysql服务并更改密码
cd /usr/local/mysql/support-files
./mysql.server start

上述方法一直报错,因为创建mysql用户的使用使用了chown -R mysql:mysql ./来配置/user/local/mysql目录的所属路径,但是该方法应该是ubuntu的指令,所以导致安装的时候数据库的权限出了问题,centos的指令应该是chown -R mysql.mysql ./,第四步已经更正,这里应该不会再出现此错误,如果还是出现此错误,按照如下方式操作。
这里为了正常启动数据库,而不用重装,采用如下方式
配置/etc/my.cnf文件如下

[mysqld]
datadir=/var/lib/mysql
#socket=/var/lib/mysql/mysql.sock
socket=/usr/local/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

然后执行如下指令,以root权限启动数据库
cd /usr/local/mysql/bin
./mysqld --user=root

十一、配置文件位置
/etc/my.cnf

十二、修改密码
SET PASSWORD FOR 'root'@'%' = PASSWORD('password');

十三、添加环境变量
编辑 ~/.bashrc文件,vim ~/.bashrc,添加如下内容
export MYSQL_HOME=/usr/local/mysql
export PATH=$PATH:$MYSQL_HOME/bin
执行如下指令,使环境变量生效
source ~/.bashrc

十四、运行数据库远程连接
执行下面命令
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;
刷新权限表
FLUSH PRIVILEGES;

CREATE DATABASE IF NOT EXISTS test_db default charset utf8 COLLATE utf8_general_ci;

–uroot –ppassword –Dtest_db

 

posted @ 2019-09-05 22:14  东临碣石,以观沧海  阅读(794)  评论(0编辑  收藏  举报