以前最烦就是源码编译安装mysql5.7 ,原因无它 — 很麻烦,但最近因学习需要避免不了,主要介绍在centos7 下源码编译安装mysql5.7 。
环境 : MiniCentos7 ( 下载地址:https://www.centos.org/download/ )
安装
安装依赖包
yum install -y gcc gcc-c++ cmake ncurses ncurses-devel bison
下载相应源码包
mysql5.7 编译安装需要boost 库,这里官网下载含boost的源码包
这里提供下载地址
https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-boost-5.7.25.tar.gz
添加用户
useradd -s /sbin/nologin mysql
建立所需目录并更改所有者为mysql
mkdir -p /data/mysql/data
chown -R mysql:mysql /data/mysql/data
将下载好的mysql 解压到/usr/local/mysql 目录下
mkdir -p /usr/local/mysql && tar -zxvf mysql-boost-5.7.25.tar.gz -C /usr/local/mysql/
切换到/usr/local/mysql 目录下,编译安装
cd /usr/local/mysql/mysql-5.7.25
&& cmake -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_BOOST=boost && make && make install
配置
编辑/etc/my.cnf ( 如果没有则创建)
我这边的一个配置文件内容如下
[client]
port = 3306
socket = /dev/shm/mysql.sock
[mysqld]
port = 3306
socket = /dev/shm/mysql.sock
user = mysql
basedir = /usr/local/mysql
datadir = /data/mysql/data
pid-file = /run/mysql.pid
log_error = /data/mysql/mysql-error.log
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /data/mysql/mysql-slow.log
skip-external-locking
key_buffer_size = 32M
max_allowed_packet = 1024M
table_open_cache = 128
sort_buffer_size = 768K
net_buffer_length = 8K
read_buffer_size = 768K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
thread_cache_size = 16
query_cache_size = 16M
tmp_table_size = 32M
performance_schema_max_table_instances = 1000
explicit_defaults_for_timestamp = true
#skip-networking
max_connections = 500
max_connect_errors = 100
open_files_limit = 65535
log_bin=mysql-bin
binlog_format=mixed
server_id = 232
expire_logs_days = 10
early-plugin-load = ""
default_storage_engine = InnoDB
innodb_file_per_table = 1
innodb_buffer_pool_size = 128M
innodb_log_file_size = 32M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 32M
sort_buffer_size = 768K
read_buffer = 2M
write_buffer = 2M
以上配置内容非必须,可依据实际情况,做相应的调整!
把安装目录用户和组更改为mysql
chown -R mysql:mysql /usr/local/mysql
chown -R mysql:mysql /data/mysql/data
把数据库数据目录用户和组更改为mysql ( 数据库数据目录:/data/mysql/data),方法同上!
注:/data/mysql/data目录下一定要为空才行
初始化mysql
/usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/data
注:到这一步很容易出问题,在初始化的时候一定要加上面的参数,而且在执行这一步操作前/data/mysql/data 这个目录必须是空的;在这里指定的basedir 和 datadir 目录必须要和/etc/my.cnf 配置的目录一直才行。
拷贝可执行配置文件
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
注:有些网友文章在这一步中会写到从/usr/local/mysql/support-files 这个目录复制my-default.cnf 到/etc/下作为my.cnf 配置文件,我这里没有这个文件,所以直接在配置环节,自己配置了一个my.cnf文件。
启动
systemctl start mysqld
注:这一步也是很容易出问题的地方,当这里出现问题的时候,不要慌,看mysql 错误日志的内容,一般根据mysql 错误日志的提示都能很好的解决问题。
如果Starting MySQL.. ERROR! The server quit without updating PID file, 可以修改my.cnf 修改pid文件目录为 /tmp/mysql.pid
修改环境变量,在/etc/profile 中新增一行
PATH=/usr/local/mysql/bin:$PATH
保存退出
source /etc/profile
测试连接
mysql -h localhost -uroot -p
设置开机自启动
systemctl enable mysqld
---配置MySQL可远程访问:---------------------------------------------------------------------------------------------------------------------------------------------------------------------
mysql -h localhost -uroot -p
Grant all privileges on *.* to 'root'@'%' identified by '---newPwd---' with grant option;
flush privileges;
调试过程: rm -rf /data/mysql/data mkdir -p /usr/local/mysql && chown -R mysql:mysql /usr/local/mysql mkdir -p /data/mysql/data && chown -R mysql:mysql /data/mysql/data touch /dev/shm/mysql.sock && chown mysql:mysql /dev/shm/mysql.sock && chmod 777 /dev/shm/mysql.sock touch /run/mysql.pid && chmod 777 /run/mysql.pid && chown mysql:mysql /run/mysql.pid /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/data mysql -h localhost -uroot -p Grant all privileges on *.* to 'root'@'%' identified by '---newPwd---' with grant option; update mysql.user set authentication_string = password("---newPwd---") where user = "root"; select Host, User, authentication_string from mysql.user; flush privileges; quit; systemctl start mysqld systemctl status mysqld systemctl restart mysqld ps -ef | grep mysql mysqld --defaults-file="F:WorkSoftWaremysqlmysql-5.7.23-winx64inmy.ini" --console --skip-grant-tables