Centos7.3 之mysql5.7二进制安装

时间:2021-02-13 05:20:26

MySQL5.7二进制文件下载地址

链接:https://pan.baidu.com/s/1B5Vh-hQ4ksuvneCimFD2zA
提取码:9k7x
复制这段内容后打开百度网盘手机App,操作更方便哦

下载的文件请放在root目录下

按照以下方法安装会有个问题,那就是使用mysqldump等命令时,可能会提示socket文件找不到,需要添加参数--socket=/data/mysql/run/mysql.sock,或者把my.cnf里的socket对应目录路径改为/tmp/mysql.sock

以下内容为一键安装脚本。脚本仅适用于测试环境,生产环境请谨慎适用!

#!/bin/bash
#yum install openssl openssl-devel libaio* -y > /dev/null
rpm -qa |grep mysql
groupadd mysql
useradd -g mysql mysql
echo "unpacking,please wait! This will take about two minitues."
tar -xf mysql-5.7.-el7-x86_64.tar.gz
echo "unpacking finish.doing next..."
#rm -rf *.tar.gz
mv mysql-5.7.-el7-x86_64 mysql
mkdir /data
mv mysql /data/mysql
rm -rf /usr/local/mysql
ln -svf /data/mysql /usr/local/
cd /usr/local/mysql
mkdir -pv /data/mysql/data
mkdir -pv /data/mysql/log/iblog
mkdir -pv /data/mysql/log/binlog
mkdir -pv /data/mysql/log/relaylog
mkdir -pv /data/mysql/run
mkdir -pv /data/mysql/tmp
chown -R mysql:mysql /data/mysql
chmod -R /data/mysql
cat > /etc/my.cnf <<EOF
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
#[mysqld]
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at % of total RAM for dedicated server, else %.
# innodb_buffer_pool_size = 128M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
# port = .....
# server_id = .....
# socket = .....
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
[mysql]
# CLIENT #
port =
socket = /data/mysql/run/mysql.sock
disable-auto-rehash
default-character-set=gbk
[mysqld]
# GENERAL #
server_id =
port =
user = mysql
explicit_defaults_for_timestamp=true
default-storage-engine = InnoDB
character_set_server = gbk
auto_increment_increment =
auto_increment_offset =
lower_case_table_names =
socket = /data/mysql/run/mysql.sock
pid_file = /data/mysql/run/mysqld.pid
# MyISAM #
key-buffer-size = 32M
myisam-recover-options = FORCE,BACKUP
# SAFETY #
max_allowed_packet =
max_connections =
max_user_connections =
open_files_limit =
skip-name-resolve
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
sysdate-is-now =
# DATA STORAGE #
basedir = /data/mysql
datadir = /data/mysql/data/
tmpdir = /data/mysql/tmp
# BINARY LOGGING #
log-bin = /data/mysql/log/binlog/master-bin
log-bin-index = /data/mysql/log/binlog/master-bin.index
expire-logs-days =
sync-binlog =
binlog_format = ROW
#RELAY LOGGING
relay-log=/data/mysql/log/relaylog/master-relay-bin
relay-log-index=/data/mysql/log/relaylog/master-relay-bin.index
sync_relay_log=
# CACHES AND LIMITS #
tmp-table-size = 32M
max-heap-table-size = 32M
query-cache-type =
query-cache-size =
max-connections =
thread-cache-size =
open-files-limit =
table-definition-cache =
table-open-cache =
# INNODB #
innodb_log_group_home_dir = /data/mysql/log/iblog
innodb_data_home_dir = /data/mysql/log/iblog
innodb-flush-method = O_DIRECT
innodb-log-files-in-group =
innodb-log-file-size = 256M
innodb-flush-log-at-trx-commit =
innodb-file-per-table =
innodb-buffer-pool-size = 6G
# LOGGING #
general_log = off
log-error = /data/mysql/log/mysql-error.log
log-queries-not-using-indexes =
slow-query-log =
slow-query-log-file = /data/mysql/log/mysql-slow.log
log_slave_updates=ON
EOF cd /data/mysql/bin
./mysqld --defaults-extra-file=/etc/my.cnf --basedir=/data/mysql --datadir=/data/mysql/data --user=mysql --initialize-insecure
./mysql_ssl_rsa_setup --basedir=/data/mysql --datadir=/data/mysql/data
echo "PATH=/data/mysql/bin:$PATH" >/etc/profile.d/mysql.sh
if [ -f /data/mysql/support-files/mysql.server ]; then
cp /data/mysql/support-files/mysql.server /etc/init.d/mysqld
else
echo "this is no mysql.server"
fi
sed -i 's/^mysqld_pid_file_path=.*$/mysqld_pid_file_path=\/data\/mysql\/run\/mysqld.pid/g' /etc/init.d/mysqld
source /etc/profile.d/mysql.sh
chkconfig --add mysqld
systemctl stop firewalld
systemctl disable firewalld
setenforce
sed -i 's/=enforcing/=disabled/g' /etc/selinux/config
echo "mysql install finish"
echo "please run 'source /etc/profile.d/mysql.sh'"
echo "please run 'systemctl start mysqld'"

请在MySQL安装完成后,可根据自己的具体需求逐条在命令行中运行以下命令

mysql -e "DELETE FROM mysql.user WHERE User='';"
mysql -e "DELETE FROM mysql.user WHERE host='localhost';"
mysql -e "DELETE FROM mysql.user WHERE host='localhost.localdomain';"
mysql -e "DELETE FROM mysql.user WHERE host='::1';"
mysql -e "SELECT host, user, authentication_string FROM mysql.user WHERE user = 'root';"
mysql -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;"
mysql -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1' IDENTIFIED BY '123456' WITH GRANT OPTION;"
mysql -e "FLUSH PRIVILEGES;"

 PS:

如果安装失败,或者想卸载

只需要删除/data/mysql和/usr/local/mysql就可以卸载干净了