yum安装见我的另一篇文章:https://www.jianshu.com/p/0680bf8fa2f9
一、准备工作(安装mysql安装依赖的环境)
1、Mysql5.7版本更新后有很多变化,安装必须要BOOST库(版本需为1.59.0)
1)boost库下载地址:https://sourceforge.net/projects/boost/files/boost/1.59.0/
[root@VM download]# wget https://jaist.dl.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz
2)检查文件MD5值是否匹配:若不匹配需重新下载
[root@VM download]# md5sum boost_1_59_0.tar.gz
51528a0e3b33d9e10aaa311d9eb451e3 boost_1_59_0.tar.gz
3)解压后放在指定位置备用
[root@VM download]# tar -vxzf boost_1_59_0.tar.gz
[root@VM download]# mv boost_1_59_0 /usr/local/boost_1_59_0
2、安装依赖包
yum -y install gcc gcc-c++ ncurses ncurses-devel cmake
二、下载mysql + 编译安装
1、官网下载地址:https://dev.mysql.com/downloads/mysql/
1)下载文件
[root@VM download]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.21.tar.gz
2)检查文件MD5值是否匹配:若不匹配需重新下载
[root@VM download]# md5sum mysql-5.7.21.tar.gz
e26523b174bdc3fd0fde6f36791ce17e mysql-5.7.21.tar.gz
3)解压文件
[root@VM download]# tar -vxzf mysql-5.7.21.tar.gz
2、编译和安装
1)进入到解压文件目录,依次运行以下命令:
[root@VM mysql-5.7.21]# cmake . \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/data/mysql \
-DWITH_BOOST=/usr/local/boost_1_59_0 \
-DSYSCONFDIR=/etc \
-DDEFAULT_CHARSET=utf8mb4 \
-DDEFAULT_COLLATION=utf8mb4_general_ci \
-DENABLED_LOCAL_INFILE=1 \
-DEXTRA_CHARSETS=all
[root@VM mysql-5.7.21]# make #时间会很长可能大于1小时
[root@VM mysql-5.7.21]# make install
- -DCMAKE_INSTALL_PREFIX:mysql安装目录
- -DMYSQL_DATADIR:数据存放目录
- -DWITH_BOOST:boost源码路径
- -DSYSCONFDIR:my.cnf配置文件目录
- -DEFAULT_CHARSET:数据库默认字符编码
- -DDEFAULT_COLLATION:默认排序规则
- -DENABLED_LOCAL_INFILE:允许从本文件导入数据
- -DEXTRA_CHARSETS:安装所有字符集
注:更多预编译配置参数请参考mysql官方文档说明:http://dev.mysql.com/doc/refman/5.7/en/source-configuration-options.html#cmake-general-options
2)错误解决 -- make 时报错:
主要原因大体上是因为内存不足,有点坑 临时使用交换分区来解决吧
sudo dd if=/dev/zero of=/swapfile bs=64M count=16
sudo mkswap /swapfile
sudo swapon /swapfile
编译出错了,需根据下面操作,重新编译安装。(网上搜到的方法,但是删除后也没有解决问题,最后将整个文件都删除了,重新解压了一份)
[root@VM mysql-5.7.21] make clean
[root@VM mysql-5.7.21] rm -f CMakeCache.txt
[root@VM mysql-5.7.21] make && make install
在编译完成后,需运行以下代码:
sudo swapoff /swapfile
sudo rm /swapfile
三、配置MySQL
1、设置权限
1)使用下面的命令查看是否有mysql用户及用户组
cat /etc/passwd 查看用户列表
cat /etc/group 查看用户组列表
2)如果没有就创建mysql数据库管理用户和组
[root@VM download]# groupadd mysql
[root@VM download]# useradd -r -g mysql -s /bin/false mysql
3)修改/usr/local/mysql权限
chown -R mysql:mysql /usr/local/mysql
2、修改/etc/my.cnf文件和/etc/my.cnf.d/mysql-clients.cnf文件
1)修改/etc/my.cnf文件
### /etc/my.cnf
[mysqld]
socket=/var/lib/mysql/mysql.sock
#指定server端字符集
character-set-server = utf8
collation-server = utf8_general_ci
# 解决问题:TIMESTAMP with implicit DEFAULT value is deprecated
explicit_defaults_for_timestamp=true
# mysql程序安装目录
basedir=/usr/local/mysql
# mysql数据文件目录
datadir=/data/mysql
pid-file = /data/mysql/mysql.pid
log_error = /data/mysql/mysql-error.log
# 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
#更改字符集为utf-8
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
2)修改/etc/my.cnf.d/mysql-clients.cnf文件
### /etc/my.cnf.d/mysql-clients.cnf
#
# These groups are read by MariaDB command-line tools
# Use it for options that affect only one utility
#
# 在配置文件中添加“[client]”选项和“[mysql]”选项
# 并使用这两个选项下的“socket”参数值,与“[mysqld]”选项下的“socket”参数值,指向的socket文件路径完全一致
# 解决问题: connect to local MySQL server through socket /var/lib/mysql/mysql.sock
[mysql]
default-character-set=utf8
socket=/var/lib/mysql/mysql.sock
[client]
default-character-set=utf8
socket=/var/lib/mysql/mysql.sock
[mysql_upgrade]
[mysqladmin]
[mysqlbinlog]
[mysqlcheck]
[mysqldump]
[mysqlimport]
[mysqlshow]
[mysqlslap]
四、初始化数据库并将mysql加入系统服务
1、初始化数据库
[root@VM mysql-5.7.21]# cd /usr/local/mysql/bin/
#两种方式任选一种
#1)不生成root密码
[root@VM bin]# ./mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql
#2)生成root随机密码,在/data/mysql/mysql-error.log文件中
[root@VM bin]# ./mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql
2、将mysql加入系统服务
[root@VM bin]# cd /usr/local/mysql/support-files/
[root@VM support-files]# cp mysql.server /etc/init.d/mysql
[root@VM support-files]# chkconfig --add mysql # mysql加入系统服务
[root@VM support-files]# chkconfig mysql on # 开机启动
[root@VM support-files]# service mysql start
运行service mysql start报错
1)mysqld_safe error: log-error set to '/var/log/mariadb/mariadb.log', however file don't exists. Create writable for user 'mysql'. ERROR! The server quit without updating PID file (/data/mysql/mysql.pid).
解决方法:
[root@VM support-files]# mkdir /var/log/mariadb
[root@VM support-files]# touch /var/log/mariadb/mariadb.log
[root@VM support-files]# chown -R mysql:mysql /var/log/mariadb/
2)mysqld_safe Directory '/var/lib/mysql' for UNIX socket file don't exists. ERROR! The server quit without updating PID file (/data/mysql/mysql.pid).
解决方法:
[root@VM support-files]# mkdir /var/lib/mysql
[root@VM support-files]# chown -R mysql:mysql /var/lib/mysql/
解决了这两个问题就启动成功了:
[root@VM support-files]# service mysql start
Starting MySQL. SUCCESS!
mysql服务的启动/重启/停止
service mysql start #启动mysql服务
service mysql restart #重启mysql服务
service mysql stop #停止mysql服务
五、登录mysql
1、环境变量PATH,要不不能直接调用mysql
vim /etc/profile
# 在profile文件末尾增加两行
PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
export PATH
# 使PATH搜索路径立即生效:
source /etc/profile
2、登录
1)查看初始密码
[root@VM mysql]# cat /data/mysql/mysql-error.log | grep password
2018-04-10T15:54:18.596286Z 1 [Note] A temporary password is generated for root@localhost: +uAqjuq/g7hv
2)登录成功
[root@VM_38_201_centos mysql]# mysql -uroot -p+uAqjuq/g7hv
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 6
Server version: 5.7.21
Copyright (c) 2000, 2018, 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>
3)修改登录密码
mysql> set password=password('123654');
Query OK, 0 rows affected, 1 warning (0.00 sec)
参考文档:
https://blog.csdn.net/langzi7758521/article/details/51435985
https://blog.csdn.net/u013887008/article/details/79115183
http://www.cnblogs.com/galengao/p/5755788.html
https://blog.csdn.net/xyang81/article/details/51792144
https://www.cnblogs.com/xiangshui/p/5194990.html
https://blog.csdn.net/fxnawm/article/details/78497314
https://www.cnblogs.com/shengdimaya/p/8027507.html