signed

QiShunwang

“诚信为本、客户至上”

linux中安装mysql数据库

2020/8/20 13:48:32   来源:

此文档以mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz为例。
 创建mysql组。
命令:groupadd mysql
 创建mysql用户。
命令:useradd –r –g mysql
 创建mysql安装目录。
命令:mkdir -p /usr/local/mysql
 创建mysql数据目录。
命令:mkdir -p /data/mysql/mysqldata
 创建mysql日志主目录。
命令:mkdir -p /data/mysql/log
 创建mysql相关日志目录。
命令:mkdir -p /data/mysql/log/{iblog,binlog,relaylog}
 创建mysql线程运行目录。
命令:mkdir -p /data/mysql/run
 创建mysql临时文件目录。
命令:mkdir -p /data/mysql/tmp
 将mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz压缩包用FTP工具复制至/usr/local/mysql目录中
 进入/usr/local/mysql目录。
命令:cd /usr/local/mysql
解压mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz压缩包,解压完成后会生成mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz文件夹。
命令:tar –zxvf mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz
 删除mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz压缩包。
命令:rm –rf mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz
 目录授权,命令:chown –R mysql /usr/local/mysql
命令:chown –R mysql /data/mysql/mysqldata
 配置my.cnf文件。
 命令vi /etc/my.cnf,按i进行编辑,写入以下内容:
[mysql]
port=3306
socket=/data/mysql/run/mysql.sock
default-character-set=utf8
disable-auto-rehash

[mysqld]
server_id = 1 #mysql唯一标识,每个mysql不同
port = 3306
user = mysql
explicit_defaults_for_timestamp=true
default-storage-engine = INNODB
character_set_server =utf8
collation_server = utf8_general_ci
lower_case_table_names = 1
socket = /data/mysql/run/mysql.sock
pid-file = /data/mysql/run/mysqld.pid
event_scheduler=1

key-buffer-size = 64M
myisam-recover-options= FORCE,BACKUP

max_allowed_packet = 32M
max_heap_table_size = 128M
read_rnd_buffer_size = 16M
max_connections = 3000
max_connect_errors = 10000
max_user_connections = 1000
open_files_limit = 65535
table_open_cache = 4096
skip-name-resolve
skip-symbolic-links
skip-external-locking
skip-slave-start
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
sysdate-is-now= 1

basedir = /usr/local/mysql/mysql-5.7.29-linux-glibc2.12-x86_64
datadir = /data/mysql/mysqldata
tmpdir = /data/mysql/tmp

log-bin=/data/mysql/log/binlog/mysql-bin #binlog存放路径(/data/mysql/log/binlog/为存放路径,mysql-bin为文件名称)
log-bin-index = /data/mysql/log/binlog/mysql-bin.index #binlog-index索引文件存放路径
expire-logs-days = 30 #binlog日志保留期限
sync-binlog = 1
binlog_format = ROW #binglog日志记录内容
#RELAY LOGGING
relay-log= /data/mysql/log/relaylog/mysql-relay-bin
relay-log-index=/data/mysql/log/relaylog/mysql-relay-bin.index
sync_relay_log=1

tmp-table-size = 32M
max-heap-table-size = 32M
query-cache-type = 0
query-cache-size = 0
max-connections = 500
back_log = 50
thread-cache-size = 50
open-files-limit = 65535
table-definition-cache = 1024
table-open-cache = 2048
transaction_isolation = READ-COMMITTED
innodb_file_per_table
innodb_status_file = 1
innodb_open_files = 2048

innodb_log_group_home_dir=/data/mysql/log/iblog
innodb_data_home_dir=/data/mysql/log/iblog
innodb-flush-method = O_DIRECT
innodb-log-files-in-group = 2
innodb-log-file-size = 256M
innodb-flush-log-at-trx-commit = 1
innodb-file-per-table = 1
innodb-buffer-pool-size = 6G
innodb_log_buffer_size=100M
innodb_buffer_pool_size = 4G
innodb_log_file_size = 512M
innodb_write_io_threads=16
innodb_read_io_threads=16
innodb_lock_wait_timeout = 120

innodb_thread_concurrency=16
innodb_flush_log_at_trx_commit=2
innodb_max_dirty_pages_pct=80
innodb_io_capacity=200
sync_binlog=0

general_log = off
log-error = /data/mysql/log/mysql-error.log
log-queries-not-using-indexes = 1
slow-query-log = 1
slow-query-log-file = /data/mysql/log/mysql-slow.log
log_slave_updates=ON
connect-timeout = 10
wait-timeout = 28800
interactive-timeout = 28800
slave-net-timeout = 5 #检测主从节点间连接正常间隔时间
net_read_timeout = 30
net_write_timeout = 60
net_retry_count = 10
net_buffer_length = 16384
thread_stack = 512K
thread_cache_size = 64
query_cache_size = 256M
query_cache_limit = 4M
query_cache_min_res_unit = 2K
sort_buffer_size = 16M
join_buffer_size = 16M
tmp_table_size = 128M
long_query_time = 6
wait_timeout=31536000
interactive_timeout=31536000

[mysqldump]
quick
max_allowed_packet = 64M
connect-timeout = 3

 进入mysql安装文件bin目录。
命令:cd /usr/local/mysql/mysql-5.7.29-linux-glibc2.12-x86_64/bin
 初始化mysql。
命令:./mysqld --defaults-extra-file=/etc/my.cnf --user=mysql --basedir=/usr/local/mysql/mysql-5.7.29-linux-glibc2.12-x86_64 --datadir=/data/mysql/mysqldata --initialize-insecure
 进入mysql安装文件support-files目录。
命令:cd /usr/local/mysql/mysql-5.7.29-linux-glibc2.12-x86_64/support-files
 复制mysql.server文件为mysql。
命令:cp mysql.server mysql
 编辑mysql文件。
命令:vi mysql,按i进行编辑,在basedir=后加入mysql安装路径,在datadir=后加入mysql数据文件路径:
在这里插入图片描述
 复制mysql文件至init.d。
命令:cp mysql /etc/init.d/mysql
 进入init.d目录。
命令:cd /etc/init.d
 为mysql文件授予可执行权限。
命令:chmod +x /etc/init.d/mysql
 为mysql文件设置开机启动。
命令:chkconfig --add mysql
 启动mysql服务。
命令:service mysql start
 进入mysql安装文件bin目录。
命令:cd /usr/local/mysql/mysql-5.7.29-linux-glibc2.12-x86_64/bin
 登陆mysql。
命令:./mysql -u root –p
 设置mysql登陆密码。
命令:set password=password(“要修改为的密码”);
 为mysql设置登陆授权。
命令:grant all privileges on . to’root’ @’%’ identified by ‘root用户密码’;
 刷新mysql授权。
命令:flush privileges;
 检查mysql是否成功安装:
在这里插入图片描述