mysql二进制包安装和初始化

MySql5.7二进制安装脚本

1、下载mysql
wget -O ~/mysql-5.7.20.tar.gz https://downloads.mysql.com/archives/get/file/mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz

2、运行sh mysql.sh

3、完成安装后设置mysql密码mysqladmin -uroot -p password 123 默认密码为空 回车即可

4、启动mysql/etc/init.d/mysql start

#!/bin/bash
Mysql_Version='mysql-5.7.20'
Soft_Dir='/www/server'
MySql_Base_Dir='/www/server/mysql'
MySql_Data_Dir='/www/server/mysql/var'

Init()
{
    # 创建用户和组
    groupadd mysql
    useradd -s /sbin/nologin -M -g mysql mysql

    cat > /etc/my.cnf<<EOF
[client]
port        = 3306
socket      = /tmp/mysql.sock

[mysqld]
port        = 3306
socket      = /tmp/mysql.sock
basedir = ${MySql_Base_Dir}
datadir = ${MySql_Data_Dir}
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
thread_cache_size = 8
query_cache_size = 8M
tmp_table_size = 16M
performance_schema_max_table_instances = 500

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   = 1
expire_logs_days = 10
early-plugin-load = ""

default_storage_engine = InnoDB
innodb_file_per_table = 1
innodb_data_home_dir = ${MySql_Data_Dir}
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = ${MySql_Data_Dir}
innodb_buffer_pool_size = 16M
innodb_log_file_size = 5M
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 = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout

EOF

	cd ~
    mkdir -p ${MySql_Data_Dir}
    tar -zxf ${Mysql_Version}.tar.gz -C ${MySql_Base_Dir} --strip-components 1
#    chown -R mysql:mysql ${MySql_Base_Dir}

    cat >> /etc/profile<<EOF
    export PATH=${MySql_Base_Dir}/bin:"\$PATH"
EOF

    source /etc/profile

    mysqld --initialize-insecure --basedir=${MySql_Base_Dir} --datadir=${MySql_Data_Dir} --user=mysql
    chown -R mysql:mysql ${MySql_Base_Dir}

    \cp ${MySql_Base_Dir}/support-files/mysql.server /etc/init.d/mysql
    chmod 755 /etc/init.d/mysql
    /etc/init.d/mysql start
}


Init

systemd方式启动mysql

vim /etc/systemd/system/mysqld.service 
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/www/server/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000

systemctl daemon-reload mysqld
systemctl start/stop/restart/status mysqld