CentOS 7 安装MySQL5.7
2020-02-17 本文已影响0人
学术界末流打工人
前期准备
关闭防火墙
systemctl stop firewalld.service
systemctl disable firewalld.service
关闭selinux
vi /etc/selinux/config
将SELINUX=enforcing改为SELINUX=disabled
然后临时关闭一下避免重启服务器
setenforce 0
# 需要查看一下SELinux状态,如果仍然为运行状态需要重启服务器
sestatus
修改yum为国内源(阿里云)
- 备份原来的yum文件
# 先安装wget
yum -y install wget
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo_bak
- 下载阿里云yum配置到 /etc/yum.repos.d/
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
- yum更新
yum makecache
yum -y update
安装MySQL
使用yum安装MySQL
MySQL官网下载yum repo配置文件。
wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
rpm安装
yum -y localinstall mysql57-community-release-el7-11.noarch.rpm
安装Mys
yum -y install mysql-community-server
# 此处可能需要安装
yum -y install deltarpm
启动MySQL服务
systemctl start mysqld
设置开机自启动
systemctl enable mysqld
systemctl daemon-reload
获取安装时默认密码
# 方式1
grep "A temporary password" /var/log/mysqld.log
# 方式2
vim /var/log/mysqld.log
使用临时密码登录
mysql -u root -p临时密码
修改管理员密码
ALTER USER 'root'@'localhost' IDENTIFIED BY 'password';
设置允许远程登录
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;
# 退出
quit;
源码安装MySQL
安装依赖包
yum install -y gcc gcc-c++ cmake ncurses ncurses-devel bison
源码包下载
源码包下载系统内wget
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-boost-5.7.25.tar.gz
添加用户
useradd -s /sbin/nologin mysql
创建目录并配置权限
mkdir -p /data/mysql/data
chown -R mysql:mysql /data/mysql
mkdir /usr/local/mysql
将下载好的mysql 解压到/usr/local/mysql 目录下
tar -zxvf mysql-boost-5.7.25.tar.gz -C /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
配置MySQL
vi /etc/my.cnf
改为一下内容
[client]
port = 3306
socket = /tmp/mysql.sock
[mysqld]
port = 3306
socket = /tmp/mysql.sock
user = mysql
basedir = /usr/local/mysql
datadir = /data/mysql/data
pid-file = /data/mysql/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
# 注:/data/mysql/data目录下一定要为空才行
chown -R mysql:mysql /data/mysql/data
初始化MySQL
cd ../bin
./mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/datad
注:到这一步很容易出问题,在初始化的时候一定要加上面的参数,而且在执行这一步操作前/data/mysql/data 这个目录必须是空的;在这里指定的basedir 和 datadir 目录必须要和/etc/my.cnf 配置的目录一直才行。
cp /usr/local/mysql/mysql-5.7.25/support-files/mysql.server /etc/init.d/mysqld
#加权限
chmod a+x /etc/init.d/mysqld
启动MySQL
service mysqld start
测试MySQL
./mysql -hlocalhost -uroot -p
# 无需密码直接回车