Centos7.3自动化脚本安装MySQL5.7
·
按照以下方法安装会有个问题,那就是使用mysqldump等命令时,可能会提示socket文件找不到,需要添加参数--socket=/data/mysql/run/mysql.sock,或者把my.cnf里的socket对应目录路径改为/tmp/mysql.sock
以下脚本仅适用于测试环境,生产环境请谨慎使用!
本次使用了三个脚本。
分别复制执行以下脚本即可。
脚本1:配置my.cnf文件
#!/bin/bash cat > /etc/my.cnf <<EOF [mysql] # CLIENT # port = 3306 socket = /data/mysql/run/mysql.sock disable-auto-rehash default-character-set=gbk [mysqld] # GENERAL # server_id = 128 port = 3306 user = mysql explicit_defaults_for_timestamp=true default-storage-engine = InnoDB character_set_server = gbk auto_increment_increment = 2 auto_increment_offset = 1 lower_case_table_names = 1 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 = 134217728 max_connections = 8192 max_user_connections = 8000 open_files_limit = 65535 skip-name-resolve sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES sysdate-is-now = 1 # 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 = 15 sync-binlog = 1 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=1 # CACHES AND LIMITS # tmp-table-size = 32M max-heap-table-size = 32M query-cache-type = 0 query-cache-size = 0 max-connections = 500 thread-cache-size = 50 open-files-limit = 65535 table-definition-cache = 1024 table-open-cache = 2048 # 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 = 2 innodb-log-file-size = 256M innodb-flush-log-at-trx-commit = 1 innodb-file-per-table = 1 innodb-buffer-pool-size = 6G # LOGGING # general_log = off log-error = /data/mysql/log/mysql-error.log log-queries-not-using-indexes = 1 slow-query-log = 1 slow-query-log-file = /data/mysql/log/mysql-slow.log log_slave_updates=ON EOF
脚本2:安装mysql
该脚本执行MySQL的安装,安装时采用--initialize-insecure选项不生成初始密码即安装完成后可以免密登录
#!/bin/bash groupadd mysql useradd -g mysql mysql tar -xvf mysql-5.7.* -C /tmp mv /tmp/mysql-5.7* ./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 755 /data/mysql cd /data/mysql/bin ./mysqld --defaults-extra-file=/etc/my.cnf --basedir=/data/mysql --datadir=/data/mysql/data --user=mysql --initialize-insecure 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 0 sed -i 's/=enforcing/=disabled/g' /etc/selinux/config echo "MySQL安装完成!" echo "请执行'source /etc/profile.d/mysql.sh'" echo "请执行'systemctl start mysqld'"
脚本3:MySQL初始化配置(可选)
该脚本内容主要配置MySQL密码为123456,以及删除一些默认用户
#!/bin/bash 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就可以卸载干净