MySQL二进制安装

零、 环境

  • vmware centos7.2 4c8g
  • mysql release: 5.7.34
  • mysql installation path: /usr/local/mysql
  • mysql datadir: /data/mysql
  • mysql cnf: /etc/my.cnf

一、安装前准备

1.1 下载

下载地址

1.2 解压到指定目录

tar -xzvf mysql-5.7.43-linux-glibc2.12-x86_64.tar.gz

cp -Rf ./mysql-5.7.43-linux-glibc2.12-x86_64 /usr/local/mysql

1.3 创建相关目录和用户及组

groupadd mysql
useradd -g mysql mysql
mkdir -p /data/mysql
chown -R mysql:mysql /data/mysql
chmod -R 750 /data/mysql

1.4 为MySQL实例创建配置文件

vim /etc/my.cnf

[mysqld]

# 数据目录
datadir=/data/mysql/
# socket 目录
socket=/data/mysql/mysql.sock
# 日志
log-error=/data/mysql/mysql-err.log
#端口
port=3306
# 实例ID
server-id=1
innodb_buffer_pool_size = 50G
lower_case_table_names=0
# 缓存目录
tmpdir=/data/mysql/tmp/
# MySQL二进制文件基目录不是默认目录时,需要配置lc_messages_dir=/usr/local/mysql/share传递它的路径.否则,MySQL 将无法启动。
lc_messages_dir=/usr/local/mysql/share

二、 安装

2.1 初始化实例

/usr/local/mysql/bin/mysqld --no-defaults --initialize-insecure --user=mysql --datadir=/data/mysql/ --lower_case_table_names=0 

To initialize the data directory, invoke mysqld with the --initialize or --initialize-insecure option, depending on whether you want the server to generate a random initial password for the 'root'@'localhost' account, or to create that account with no password:

Use --initialize for “secure by default” installation (that is, including generation of a random initial root password). In this case, the password is marked as expired and you must choose a new one.

With --initialize-insecure , no root password is generated. This is insecure; it is assumed that you intend to assign a password to the account in a timely fashion before putting the server into production use.

可以使用 grep "password" /path to you mysql-err.log 找到使用--initialize生成的随机密码

补充:

  • 创建目录 mkdir -p /data/mysql/tmp 的步骤一定要在初始化实例之前
  • 初始化的时候检测到数据目录 /data/mysql下存在其他文件或者文件夹的时候会报错.

三、 使用 systemd 管理

# /usr/lib/systemd/system/mysql.service
# Copyright (c) 2016, 2021, Oracle and/or its affiliates.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2.0,
# as published by the Free Software Foundation.
#
# This program is also distributed with certain software (including
# but not limited to OpenSSL) that is licensed under separate terms,
# as designated in a particular file or component or in included license
# documentation. The authors of MySQL hereby grant you an additional
# permission to link the program and your derivative works with the
# separately licensed software that they have included with MySQL.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License, version 2.0, for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# systemd service file for MySQL forking server
#

[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
Type=forking
PIDFile=/data/mysql/mysqld.pid
# Disable service start and stop timeout logic of systemd for mysqld service.
TimeoutSec=0
# Execute pre and post scripts as root
PermissionsStartOnly=true
# Needed to create system tables
# ExecStartPre=/usr/bin/mysqld_pre_systemd %I
# Start main service
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --daemonize --pid-file=/data/mysql/mysqld.pid $MYSQLD_OPTS

# Use this to switch malloc implementation
EnvironmentFile=-/etc/sysconfig/mysql
# Sets open_files_limit
LimitNOFILE = 65536
Restart=on-failure
RestartPreventExitStatus=1
Environment=MYSQLD_PARENT_PID=1
PrivateTmp=false

3.1 启动MySQL

systemctl daemon-reload
systemctl enable mysql --now
systemctl status mysql

四、 MySQL 配置文件参数优化

仅供参考:

[client]                                        #客户端配置
port= 3306 #监听端口
socket= /tmp/mysql.sock #socket通信文件
default-character-set = utf8 #客户端字符编码

[mysql] #终端管理
prompt="MySQL [\d]> " #prompt="\\u@\\h:\\d>" 终端连接后显示后用户、主机、当前数据库
no-auto-rehash #auto-rehash 命令行自动补全功能

[mysqld] #服务器配置
port = 3306 #端口
socket = /tmp/mysql.sock #套接字
datadir = /data/mysql #数据库文件所在目录
basedir = /usr/local/mysql #基准路径,其它路径都相对于此路径
pid-file = /usr/local/mysql/mysql.pid #pid文件
user = mysql #管理用户
server-id=1 #本机序列号
init-connect = 'SET NAMES utf8' #当一个连接进来时,做一些操作,此处为连接创建时设置字符编码
character_set_server=utf8 #服务器默认字符集
#skip-networking #开启则彻底关闭mysql的TCP/IP连接方式
back_log = 300 #并发是等待的数量
open_files_limit = 65535 #打开的文件描述符限制
binlog_cache_size = 1M #事务未提交时会记录到cache,等到事务提交,则把日志持久化到磁盘,默认32k,如果事务比此值大,会使用磁盘临时文件替代
max_heap_table_size = 8M #用户创建的内存表最大值,为防止意外超大的内存表耗尽内存资源
tmp_table_size = 128M #内存中临时表的大小,如果超过此值,则转换为基于磁盘的表,默认32M
ft_min_word_len = 4 #全文索引最小长度,
log_bin = mysql-bin #二进制文件
binlog_format = mixed #二进制日志格式,默认statement,建议mixed
expire_logs_days = 7 #指定二进制日志的有效时间
log_error = /usr/local/mysql/mysql-error.log #错误日志
slow_query_log = 1 #默认不开启,on|off
long_query_time = 1 #超过多少秒的查询就写入日志
slow_query_log_file = /usr/local/mysql/mysql-slow.log #慢查询日志文件,mysqldumpslow -h自带的查询慢日志工具。实例mysqldumpslow -s c -t 20 host-slow.log
performance_schema = 0 #用于收集数据库服务器性能参数
explicit_defaults_for_timestamp #自动填补timestamp字段的值
lower_case_table_names = 1 #0存储和比较都是大小写敏感,1存储时转换为小写,比较时不区分大小写。2存储时区分大小写,比较时转换为小写
skip-external-locking #跳过外部锁定,即多台服务器使用同一个数据目录,那么每台必须开启external-locking

##network setting
skip-name-resolve #禁止mysql对外部连接DNS解析。如果打开,则所有远程主机授权都要使用IP方式,否则无法处理请求
bind-address = 0.0.0.0 #服务监听地址
max_connections = 1024 #指定Mysql允许的最大连接进程数,可以过'conn%'通配符查看当前状态的连接数量,以定夺该值的大小。
max_connect_errors = 102400 #对于同一主机,如果超过该值的中断错误连接,则该主机被禁止连接。解禁FLUSH HOST
max_allowed_packet = 32M #接受的数据包大小,增加该值十分的安全,这是因为只有当需要时才会分配额外的内存。去较小值是预防偶尔使用大信息而导致内存溢出
interactive_timeout = 28800 #默认28800,8小时。服务器关闭交互连接前等待活动的描述
wait_timeout = 28800 #默认28800,服务器关闭非交互连接之前等待活动的秒数

##innodb setting
default_storage_engine = InnoDB #默认存储引擎
innodb_file_per_table = 1 #独享表空间,关闭即可
innodb_open_files = 500 #默认300,限制Innodb能打开表的数据
innodb_buffer_pool_size = 1024M #数据和索引的缓冲池,典型值5-6G(8G内存),20-25GB(32GB内存),100-120GB(128GB内存)。
innodb_write_io_threads = 4 #写入磁盘IO
innodb_read_io_threads = 4 #读取磁盘IO
innodb_thread_concurrency = 0 #你的服务器CPU有几个就设置为几,建议用默认一般为8
innodb_purge_threads = 1 #最大值32,控制做purge(净化)操作的后台线程数
innodb_flush_log_at_trx_commit = 2 #为2表示事务日志在提交时写入日志,但日志文件每次刷新到磁盘一次
innodb_log_buffer_size = 2M #指定日志文件所用内存大小,1-8之间
innodb_log_file_size = 32M #指定日志文件大小
innodb_log_files_in_group = 3 #默认2,推荐3.mysql以循环方式将日志文件写入多个文件
innodb_max_dirty_pages_pct = 90 #默认75
innodb_lock_wait_timeout = 120 #默认50秒,

##cache setting
key_buffer_size = 32M #索引缓冲区大小,增加它可以得到更好的处理性能。对于4G左右服务器来说,该参数可设为256MB或384MB
table_open_cache = 512 #这个参数在5.1.3之后的版本中叫做table_open_cache,用于设置table高速缓存的数量。由于每个客户端连接都会至少访问一个表,
sort_buffer_size = 2M #查询排序时所能使用的缓冲区大小,是一个connection级参数,即500个链接会占用500*8=4G内存
join_buffer_size = 2M #联合查询所能使用的缓冲区大小,该参数分配的内存也是每个链接独享,默认2M
read_buffer_size = 2M #用于对myisam表全表扫描时使用缓冲区大小,读查询所能使用的缓冲区大小,该参数分配的内存也是每个链接独享
read_rnd_buffer_size = 8M #当需要时线程分配,当在排序后,从一个已经分配好的序列中读取行时,行数据从这个缓冲区读取防止磁盘寻道
thread_cache_size = 64 #服务器线程缓存,连接断开时放入缓存,线程重新被请求从缓存读取。设置规则:1GB内存配置8,2G内存配置18,3G内存32,4G或更高可配置更大
query_cache_type = 1 #是否开启查询缓存,默认0不开启,1缓存所有结果,2仅缓存select语句中通过SQL_CACHE指定的查询
query_cache_size = 64M #指定查询缓冲区大小 ,一个select工作后,db会把该语句缓冲,当同一个SQL再次来到,DB未检查到该表发生变化时,会直接把结果返给client
query_cache_limit = 2M #指定单个查询缓存所能使用的缓冲区大小
bulk_insert_buffer_size = 8M #默认8M,批量插入数据缓存大小,可以提高插入效率

myisam_sort_buffer_size = 64M #myisam表发生变化时,重新排序所需的缓冲,默认8M
myisam_max_sort_file_size = 10G #mysql重建索引时允许的临时文件大小

#event_scheduler =ON
#skip-external-locking
#sql_mode ='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'

[mysqldump]
quick #备份方式
max_allowed_packet = 500M #服务器发送和接受的最大包长度

[myisamchk]
key_buffer_size = 256M #key缓冲
sort_buffer_size = 8M #排序缓冲
read_buffer = 4M #读取缓冲
write_buffer = 4M #写入缓冲