1. 下载 Linux 版本 MySQL 压缩包
下载命令:
命令 : wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.19-linux-glibc2.12-x86_64.tar.xz # 下载地址
下载进度如图所示:
2. 解压
命令 : xz -d mysql-8.0.19-linux-glibc2.12-x86_64.tar.xz
#将.xz压缩包解压为.tar
命令 : tar xf mysql-8.0.19-linux-glibc2.12-x86_64.tar
#将.tar压缩包解压为文件夹
命令 : mv mysql-8.0.19-linux-glibc2.12-x86_64 /usr/local/mysql/
#移动至安装目录,这里是 /usr/local/mysql
操作流程如图:
3. 给MySQL建立系统账号
命令 : adduser mysql
4. 新建data、sql_log、undo目录
进入 MySQL 安装目录:
命令 : cd /usr/local/mysql
新建data、sql_log、undo目录:
命令 : mkdir data sql_log undo
给这三个目录建立属主:
命令 : chown mysql:mysql -R data/ sql_log/ undo
如图所示:
5. 添加mysql环境变量
命令 : vim /etc/profile
在 profile 文件末尾添加如下内容(按i编辑):
export PATH=$PATH:/usr/local/mysql/bin
如下图所示:
按ESC,然后输入:wq保存退出,如图所示:
使上述配置生效:
命令 : source /etc/profile
6. 新建MySQL配置文件
新建my.cnf配置文件:
命令 : vim /etc/my.cnf
在my.cnf中添加如下内容:
[client]
port = 30386
socket = /usr/local/mysql/data/mysql.sock
[mysqld]
#禁止DNS服务器连接
skip_name_resolve = 1
#跳过外部锁定
skip_external_locking = 1
#禁用符号链接以防止各种安全风险
skip_symbolic_links = 1
#启动用户
user = mysql
#默认的存储引擎
default_storage_engine = InnoDB
#用来指定socket文件的路径
socket = /usr/local/mysql/data/mysql.sock
#用来指定pid文件的路径
pid_file = /usr/local/mysql/data/mysqld.pid
basedir = /usr/local/mysql
port = 30386
bind-address = 0.0.0.0
explicit_defaults_for_timestamp = off
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
key_buffer_size = 32M
#myisam_recover = FORCE,BACKUP
# undo log #
innodb_undo_directory = /usr/local/mysql/undo
#innodb_undo_tablespaces = 8s
# SAFETY #
max_allowed_packet = 64M
#允许多少个连续的中断连接请求
max_connect_errors = 100
sysdate_is_now = 1
#innodb = FORCE
#innodb_strict_mode = 1
secure-file-priv='/tmp'
# 默认使用"mysql_native_password"插件认证
default_authentication_plugin='mysql_native_password'
# Replice #
server-id = 1001
#定义relay_log的位置和名称
#relay_log = mysqld-relay-bin
gtid_mode = on
enforce-gtid-consistency
log-slave-updates = on
master_info_repository =TABLE
relay_log_info_repository =TABLE
# DATA STORAGE #
datadir = /usr/local/mysql/data/
tmpdir = /tmp
# BINARY LOGGING #
log_bin = /usr/local/mysql/sql_log/mysql-bin
max_binlog_size = 1000M
binlog_format = row
binlog_expire_logs_seconds=2592000
# sync_binlog = 1
# CACHES AND LIMITS #
tmp_table_size = 32M
max_heap_table_size = 32M
max_connections = 4000
thread_cache_size = 2048
open_files_limit = 65535
table_definition_cache = 4096
table_open_cache = 4096
sort_buffer_size = 2M
read_buffer_size = 2M
read_rnd_buffer_size = 2M
# thread_concurrency = 24
join_buffer_size = 1M
# table_cache = 32768
thread_stack = 512k
max_length_for_sort_data = 16k
# INNODB #
innodb_flush_method = O_DIRECT
innodb_log_buffer_size = 16M
innodb_flush_log_at_trx_commit = 2
innodb_file_per_table = 1
innodb_buffer_pool_size = 256M
#innodb_buffer_pool_instances = 8
innodb_stats_on_metadata = off
innodb_open_files = 8192
innodb_read_io_threads = 16
innodb_write_io_threads = 16
innodb_io_capacity = 20000
innodb_thread_concurrency = 0
innodb_lock_wait_timeout = 60
innodb_old_blocks_time=1000
innodb_use_native_aio = 1
innodb_purge_threads=1
innodb_change_buffering=all
innodb_log_file_size = 64M
innodb_log_files_in_group = 2
innodb_data_file_path = ibdata1:256M:autoextend
innodb_rollback_on_timeout=on
# LOGGING #
log_error = /usr/local/mysql/sql_log/mysql-error.log
# log_queries_not_using_indexes = 1
# slow_query_log = 1
slow_query_log_file = /usr/local/mysql/sql_log/slowlog.log
# TimeOut #
#interactive_timeout = 30
#wait_timeout = 30
#net_read_timeout = 60
[mysqldump]
quick
max_allowed_packet = 100M
[mysql]
no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates
[myisamchk]
key_buffer_size = 16M
sort_buffer_size = 5M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
如图所示 :
7. 初始化MySQL
进入 MySQL 安装目录:
命令 : cd /usr/local/mysql
执行初始化:
命令 : mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
#需要耐心等待
Tips:在初始化过程中如果出现一下输出:
mysqld: error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory,可以使用命令:
可以使用命令
yum -y install numactl
来解决。出现这个错误是因为Linux缺少软件依赖包numactl,上述命令时通过yum仓库安装numactl依赖包。
8. 查询MySQL初始临时密码
进入 “/usr/local/mysql/sql_log”, 使用如下命令可查看初始临时密码,请做好相关记录,后面登录需要用到:
命令 : cd /usr/local/mysql/sql_log
命令 : grep password mysql-error.log
如图所示 :
9. 关闭 Linux 防火墙
新手在安装学习MySQL,通常会出现端口号被防火墙阻止了,从而一直接连不上MySQL,所以为了方便后面连接MySQL学习,可以关闭Linux防火墙:
命令 : systemctl status firewalld
#查看防火状态
命令 : systemctl disable firewalld
#关闭防火墙
10. 关闭selinux
SELinux(Security-Enhanced Linux) 是美国国家安全局(NSA)对于强制访问控制设计的一个安全系统,但其对很多软件比较敏感,会导致很多服务崩溃,所以建议关闭:
编辑selinux配置文件:
命令 :vim /etc/selinux/config
将selinux中参数改为”SELINUX=disabled”,按ESC,输入”:wq”保存退出,并使配置立即生效
命令 : source /etc/selinux/config
将其中参数改为SELINUX=disabled,并使配置立即生效
命令 : source /etc/selinux/config
如图所示:
11. 启动、停止、重启MySQL
进入”/usr/local/mysql/support-files”,操作命令:
命令 : cd /usr/local/mysql/support-files
命令 : cp mysql.server /etc/init.d/mysql.d
MySQL 服务命令 :
命令 : service mysqld start
#MySQL服务启动
命令 : service mysqld stop
#MySQL服务停止
命令 : service mysqld reload
#MySQL服务重启