乐考论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 91|回复: 0

[centos] centos7安装mysql5.7脚本并配置开机启动

[复制链接]
发表于 2018-5-27 18:44:47 | 显示全部楼层 |阅读模式
首先检查你所用的Linux下有没有安装过mysql,有没有卸载干

同时也要卸载centos7自带数据库mariadb

#uninstall centos7 mariadb
rpm -e --nodeps mariadb-libs-5.5.37-1.el7_0.x86_64

rpm -qa|grep -i mysql

如果有则载

rpm -e mysql-5.6.30-linux-glibc2.5-x86_64 --nodeps

下文以Shell脚本形式给出,感兴趣的朋友可复制好自行制作脚本执行。脚本开始

#!/bin/sh


#version 2.0
#安装版本 5.7.x
#安装要求:
#需要可以联网,若无网络,可以先创建/data目录,将安装包上传到/data目录下

yum -y install libaio* wget autoconf

#判断/data目录是否存在,若不存在则创建,并且下载mysql
read  -p "Input a mysql port:" -t 30  mysql_port
read  -p "Input a mysql_server_id:" -t 30 mysql_server_id

#解压下载好的mysql
cd /opt
mkdir mysql
cd mysql
tar zxvf /data/mysql-5.7.16-linux-glibc2.5-x86_64.tar.gz -C /opt/mysql

#创建mysql用户

egrep "^mysql" /etc/group >& /dev/null
if [ $? -ne 0 ]
then
    groupadd mysql
fi

#create user if not exists
egrep "^mysql" /etc/passwd >& /dev/null
if [ $? -ne 0 ]
then
    useradd -g mysql -s /sbin/nologin -d /usr/local/mysql mysql
fi


#创建mysql软连接,并授权给mysql用户
cd /usr/local/
rm -rf /usr/local/mysql
ln -s /opt/mysql/mysql-5.7.16-linux-glibc2.5-x86_64 /usr/local/mysql
chown -R mysql:mysql /usr/local/mysql
chown -R mysql:mysql /usr/local/mysql/

#基于配置文件,创建mysql安装目录,并授权
mkdir -p /data/mysql
mkdir -p /data/mysql/mysql${mysql_port}
cd  /data/mysql/mysql${mysql_port}/
mkdir /data/mysql/mysql${mysql_port}/data
mkdir /data/mysql/mysql${mysql_port}/logs
mkdir /data/mysql/mysql${mysql_port}/tmp
chown -R mysql:mysql /data/mysql/mysql${mysql_port}

cat > /etc/my.cnf << EOF

[client]
port            = ${mysql_port}
socket          = /tmp/mysql.sock

# The MySQL server
[mysqld]
# Basic
port            = ${mysql_port}
user        = mysql
basedir         = /usr/local/mysql
datadir         = /data/mysql/mysql${mysql_port}/data
tmpdir          = /data/mysql/mysql${mysql_port}/tmp
socket          = /tmp/mysql.sock

log-bin     = /data/mysql/mysql${mysql_port}/logs/mysql-bin

log-error   = error.log
slow-query-log-file = slow.log
skip-external-locking
skip-name-resolve
log-slave-updates

lower_case_table_names  = 1  #忽略表名大小写

###############################
# FOR Percona 5.7
#extra_port = 3345
gtid-mode = on
enforce-gtid-consistency=on
#thread_handling=pool-of-threads
#thread_pool_oversubscribe=8
explicit_defaults_for_timestamp
###############################

server-id       =${mysql_server_id}

character-set-server = utf8
slow-query-log
binlog_format = row
max_binlog_size = 128M
binlog_cache_size = 1M
expire-logs-days = 5
back_log = 500
long_query_time=1
max_connections=1100
max_user_connections=1000
max_connect_errors=1000
sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
wait_timeout=100
interactive_timeout=100

connect_timeout = 20
slave-net-timeout=30

max-relay-log-size = 256M
relay-log = relay-bin
transaction_isolation = READ-COMMITTED

performance_schema=0
#myisam_recover
key_buffer_size = 64M
max_allowed_packet = 16M
#table_cache = 3096
table_open_cache = 6144
table_definition_cache = 4096

sort_buffer_size = 128K
read_buffer_size = 1M
read_rnd_buffer_size = 1M
join_buffer_size = 128K

myisam_sort_buffer_size = 32M
tmp_table_size = 32M
max_heap_table_size = 64M
query_cache_type=0
query_cache_size = 0
bulk_insert_buffer_size = 32M

thread_cache_size = 64
#thread_concurrency = 32
thread_stack = 192K
skip-slave-start

# InnoDB
innodb_data_home_dir = /data/mysql/mysql${mysql_port}/data
innodb_log_group_home_dir = /data/mysql/mysql${mysql_port}/logs
innodb_data_file_path = ibdata1:100M:autoextend

innodb_buffer_pool_size = 100M

innodb_buffer_pool_instances    = 8
#innodb_additional_mem_pool_size = 16M
innodb_log_file_size = 200M
innodb_log_buffer_size = 16M
innodb_log_files_in_group = 3
innodb_flush_log_at_trx_commit = 0
innodb_lock_wait_timeout = 10
innodb_sync_spin_loops = 40
innodb_max_dirty_pages_pct = 90
innodb_support_xa = 0
innodb_thread_concurrency = 0
innodb_thread_sleep_delay = 500
#innodb_file_io_threads    = 4
innodb_concurrency_tickets = 1000
log_bin_trust_function_creators = 1
innodb_flush_method = O_DIRECT
innodb_file_per_table
innodb_read_io_threads = 16
innodb_write_io_threads = 16
innodb_io_capacity = 2000
innodb_file_format = Barracuda
innodb_purge_threads=1
innodb_purge_batch_size = 32
innodb_old_blocks_pct=75
innodb_change_buffering=all
innodb_stats_on_metadata=OFF

[mysqldump]
quick
max_allowed_packet = 128M
#myisam_max_sort_file_size = 10G

[mysql]
no-auto-rehash
max_allowed_packet = 128M
prompt                         = '(product)\u@\h [\d]> '
default_character_set          = utf8


[myisamchk]
key_buffer_size = 64M
sort_buffer_size = 512k
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout

[mysqld_safe]
#malloc-lib= /usr/local/mysql/lib/mysql/libjemalloc.so

EOF

#初始化mysql

cd /usr/local/mysql
#./scripts/mysql_install_db --defaults-file=/etc/my.cnf
./bin/mysqld --initialize

#在/etc/init.d下创建mysql 启动脚本

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql

脚本结束下面是设置用户密码、配置远程访问权限

#添加环境变量,并使/etc/profile环境变量生效
echo "export PATH=$PATH:/usr/local/mysql/bin">>/etc/profile
sleep 3
source /etc/profile
#不过你记得初始化 之后   去error.log 看下初始化的密码是多少,如果提示找不到mysql的话 在手动执行以下source /etc/profile
#去error.log 看下初始化的密码
grep 'temporary password' /data/mysql/mysql3306/data/error.log
# 修改密码:
mysql -uroot -p 回车后输入上面获取的密码
set password=password('Yourpass2017');
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'yourpass2017' WITH GRANT OPTION;
flush privileges;
# 添加自启动服务
chkconfig --add mysql
chkconfig mysql on


回复

使用道具 举报

快速回复 返回顶部 返回列表
鲁ICP备16044790-1号 Copyright © 2008-2020 乐考网(www.5Lekao.com) 版权所有
| QQ| 乐考论坛