#!/usr/bin/env bash
echo "安装数据库..."
rpm -Uvh http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
yum install -y mysql-server

echo "设置mysql的配置文件"
echo "
[mysqld]
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
transaction-isolation = READ-COMMITTED
# Disabling symbolic-links is recommended to prevent assorted security risks;
# to do so, uncomment this line:
symbolic-links = 0

key_buffer_size = 32M
max_allowed_packet = 32M
thread_stack = 256K
thread_cache_size = 64
query_cache_limit = 8M
query_cache_size = 64M
query_cache_type = 1

max_connections = 550
#expire_logs_days = 10
#max_binlog_size = 100M

#log_bin should be on a disk with enough free space.
#Replace '/var/lib/mysql/mysql_binary_log' with an appropriate path for your
#system and chown the specified folder to the mysql user.
log_bin=/var/lib/mysql/mysql_binary_log

#In later versions of MySQL, if you enable the binary log and do not set
#a server_id, MySQL will not start. The server_id must be unique within
#the replicating group.
server_id=1

binlog_format = mixed

read_buffer_size = 2M
read_rnd_buffer_size = 16M
sort_buffer_size = 8M
join_buffer_size = 8M

# InnoDB settings
innodb_file_per_table = 1
innodb_flush_log_at_trx_commit  = 2
innodb_log_buffer_size = 64M
innodb_buffer_pool_size = 4G
innodb_thread_concurrency = 8
innodb_flush_method = O_DIRECT
innodb_log_file_size = 512M

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

sql_mode=STRICT_ALL_TABLES
" > /etc/my.cnf

echo "启动数据库"
systemctl start mysqld
systemctl enable mysqld

# 初始化数据库
echo "初始化数据库，请按提示输入！！！一般是Y、Y、N、Y、Y，请按需输入哈～～"
cd ~
/usr/bin/mysql_secure_installation

echo "登录和初始化数据库，请输入刚才修改后的密码！"
mysql -uroot -p -e "source /root/db_init.sql"
