mysql安装

安装

环境

系统:centos7.5
软件版本:mysql-5.7.23
数据库目录:/data/mysql
数据库root密码:zaq.123456
二进制安装目录:/usr/local/mysql

二进制方式

0.使用脚本安装

# root 密码随机已输入到数据库默认配置文件中
curl -O https://gitee.com/yx571304/my_oschina/raw/master/mysql/mysql-install-bin.sh
sh mysql-install-bin.sh

1.创建服务运行用户,组

groupadd -g 1200 mysql
useradd -r -g mysql -u 1200 -s /sbin/nologin mysql

2.创建数据目录

datadir=/data/mysql         # 数据库数据存储目录
mkdir -p $datadir
basedir=/usr/local/mysql    # 数据库安装目录

3.安装 jemalloc

rm -rf /etc/yum.repos.d/*
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
curl -o /etc/yum.repos.d/CentOS.repo http://mirrors.aliyun.com/repo/Centos-7.repo
sed -i '/aliyuncs.com/d' /etc/yum.repos.d/CentOS.repo /etc/yum.repos.d/epel.repo
yum install -y jemalloc-devel

4.下载软件,解压

file_url="http://mirrors.ustc.edu.cn/mysql-ftp/Downloads/MySQL-5.7/mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz"
curl -OL $file_url --progress
tar xvf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.23-linux-glibc2.12-x86_64/ $basedir

5.配置服务

cp $basedir/support-files/mysql.server /etc/init.d/mysqld
sed -i "s@^basedir=.*@basedir=$basedir@" /etc/init.d/mysqld
sed -i "s@^datadir=.*@datadir=$datadir@" /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on

6.创建mysql配置文件

cat <<EOF   >/etc/my.cnf
[client]
port = 3306
socket = /tmp/mysql.sock
default-character-set = utf8mb4

[mysql]
prompt="MySQL [\\d]> "
no-auto-rehash

[mysqld]
skip-ssl
port = 3306
user = mysql
server-id = 1
bind-address = 0.0.0.0
log_timestamps = SYSTEM
socket = /tmp/mysql.sock

basedir = $basedir
datadir = $datadir
character-set-server = utf8mb4
pid-file = $datadir/mysql.pid
init-connect = 'SET NAMES utf8mb4'

back_log = 300
#skip-networking
skip-name-resolve

max_connections = 1000
max_connect_errors = 6000
open_files_limit = 65535
table_open_cache = 128
max_allowed_packet = 500M
binlog_cache_size = 1M
max_heap_table_size = 8M
tmp_table_size = 16M

read_buffer_size = 2M
read_rnd_buffer_size = 8M
sort_buffer_size = 8M
join_buffer_size = 8M
key_buffer_size = 4M

thread_cache_size = 8

query_cache_type = 1
query_cache_size = 8M
query_cache_limit = 2M

ft_min_word_len = 4

log_bin = mysql-bin
binlog_format = mixed
expire_logs_days = 7

slow_query_log = 1
long_query_time = 1
log_error = $datadir/mysql-error.log
slow_query_log_file = $datadir/mysql-slow.log

performance_schema = 0
explicit_defaults_for_timestamp

#lower_case_table_names = 1

skip-external-locking

default_storage_engine = InnoDB
#default-storage-engine = MyISAM
innodb_file_per_table = 1
innodb_open_files = 500
innodb_buffer_pool_size = 64M
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_thread_concurrency = 0
innodb_purge_threads = 1
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 2M
innodb_log_file_size = 32M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120

bulk_insert_buffer_size = 8M
myisam_sort_buffer_size = 8M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1

interactive_timeout = 28800
wait_timeout = 28800

[mysqldump]
quick
max_allowed_packet = 500M

[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M
EOF

7.数据库配置优化

cp /etc/my.cnf{,.bak}
Mem=`free -m | awk '/Mem:/{print $2}'`
sed -i "s@max_connections.*@max_connections = $((${Mem}/3))@" /etc/my.cnf
if [ ${Mem} -gt 1500 -a ${Mem} -le 2500 ]; then
    #  1500MB < 实际内存 <= 2500MB
    sed -i 's@^thread_cache_size.*@thread_cache_size = 16@' /etc/my.cnf
    sed -i 's@^query_cache_size.*@query_cache_size = 16M@' /etc/my.cnf
    sed -i 's@^myisam_sort_buffer_size.*@myisam_sort_buffer_size = 16M@' /etc/my.cnf
    sed -i 's@^key_buffer_size.*@key_buffer_size = 16M@' /etc/my.cnf
    sed -i 's@^innodb_buffer_pool_size.*@innodb_buffer_pool_size = 128M@' /etc/my.cnf
    sed -i 's@^tmp_table_size.*@tmp_table_size = 32M@' /etc/my.cnf
    sed -i 's@^table_open_cache.*@table_open_cache = 256@' /etc/my.cnf
elif [ ${Mem} -gt 2500 -a ${Mem} -le 3500 ]; then
    #  2500MB < 实际内存 <= 3500MB
    sed -i 's@^thread_cache_size.*@thread_cache_size = 32@' /etc/my.cnf
    sed -i 's@^query_cache_size.*@query_cache_size = 32M@' /etc/my.cnf
    sed -i 's@^myisam_sort_buffer_size.*@myisam_sort_buffer_size = 32M@' /etc/my.cnf
    sed -i 's@^key_buffer_size.*@key_buffer_size = 64M@' /etc/my.cnf
    sed -i 's@^innodb_buffer_pool_size.*@innodb_buffer_pool_size = 512M@' /etc/my.cnf
    sed -i 's@^tmp_table_size.*@tmp_table_size = 64M@' /etc/my.cnf
    sed -i 's@^table_open_cache.*@table_open_cache = 512@' /etc/my.cnf
elif [ ${Mem} -gt 3500 ]; then
    #  3500MB < 实际内存
    sed -i 's@^thread_cache_size.*@thread_cache_size = 64@' /etc/my.cnf
    sed -i 's@^query_cache_size.*@query_cache_size = 64M@' /etc/my.cnf
    sed -i 's@^myisam_sort_buffer_size.*@myisam_sort_buffer_size = 64M@' /etc/my.cnf
    sed -i 's@^key_buffer_size.*@key_buffer_size = 256M@' /etc/my.cnf
    sed -i 's@^innodb_buffer_pool_size.*@innodb_buffer_pool_size = 1024M@' /etc/my.cnf
    sed -i 's@^tmp_table_size.*@tmp_table_size = 128M@' /etc/my.cnf
    sed -i 's@^table_open_cache.*@table_open_cache = 1024@' /etc/my.cnf
fi

8.初始化数据库

sed -i 's@executing mysqld_safe@executing mysqld_safe\nexport LD_PRELOAD=/usr/lib64/libjemalloc.so@' $basedir/bin/mysqld_safe
$basedir/bin/mysqld --initialize-insecure --user=mysql --basedir=$basedir --datadir=$datadir
chmod 600 /etc/my.cnf
chown mysql.mysql -R $datadir
systemctl start mysqld

9.添加环境变量

echo "export PATH=/usr/local/mysql/bin:\$PATH" >> /etc/profile
. /etc/profile

10.初始化root密码, 权限

dbrootpwd=zaq.123456
mysql -e "grant all privileges on *.* to root@'127.0.0.1' identified by \"${dbrootpwd}\" with grant option;"
mysql -e "grant all privileges on *.* to root@'localhost' identified by \"${dbrootpwd}\" with grant option;"
mysql -uroot -p${dbrootpwd} -e "reset master;"

11.配置mysql库文件

rm -rf /etc/ld.so.conf.d/mariadb-x86_64.conf
echo "/usr/local/mysql/lib" > /etc/ld.so.conf.d/mysql.conf
ldconfig
systemctl restart mysqld

12.开启root远程登录权限

mysql -uroot -p${dbrootpwd} -e "grant all privileges on *.* to root@'%' identified by \"${dbrootpwd}\" with grant option;"
mysql -uroot -p${dbrootpwd} -e "SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user;"

13.配置防火墙,Selinux

firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --reload
firewall-cmd --zone=public --query-port=3306/tcp
firewall-cmd --zone=public --list-ports
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

YUM方式

1.配置YUM源

yum install -y https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm
yum repolist all | grep mysql                     # 查看所有 mysql 子存储库
yum install -y yum-utils                          # 安装 yum 配置工具管理 YUM 源
yum-config-manager --disable mysql80-community    # 禁用 mysql80 子存储库
yum-config-manager --enable mysql57-community     # 启用 mysql57 子存储库
yum repolist enabled | grep mysql                 # 查看已启用的 mysql 子存储库

2.安装mysql

yum install -y mysql-community-server

3.启动 mysql 服务,跟随系统启动

systemctl start mysqld
systemctl status mysqld
systemctl enable mysqld

4.修改root密码

grep 'temporary password' /var/log/mysqld.log    # 查看初始化密码
def_pass=$(grep 'temporary password' /var/log/mysqld.log | awk '{print $NF}')
sql="alter user 'root'@'localhost' identified by 'zaq.123456';"
mysql -uroot -p$def_pass -e "$sql"

转载于:https://my.oschina.net/yx571304/blog/2872381

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值