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 = 30386socket = /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.pidbasedir = /usr/local/mysqlport = 30386bind-address = 0.0.0.0explicit_defaults_for_timestamp = offsql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLESkey_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 = 100sysdate_is_now = 1#innodb = FORCE#innodb_strict_mode = 1secure-file-priv='/tmp'# 默认使用"mysql_native_password"插件认证default_authentication_plugin='mysql_native_password'# Replice #server-id = 1001#定义relay_log的位置和名称#relay_log = mysqld-relay-bingtid_mode = onenforce-gtid-consistencylog-slave-updates = onmaster_info_repository =TABLErelay_log_info_repository =TABLE# DATA STORAGE #datadir = /usr/local/mysql/data/tmpdir = /tmp# BINARY LOGGING #log_bin = /usr/local/mysql/sql_log/mysql-binmax_binlog_size = 1000Mbinlog_format = rowbinlog_expire_logs_seconds=2592000# sync_binlog = 1# CACHES AND LIMITS #tmp_table_size = 32Mmax_heap_table_size = 32Mmax_connections = 4000thread_cache_size = 2048open_files_limit = 65535table_definition_cache = 4096table_open_cache = 4096sort_buffer_size = 2Mread_buffer_size = 2Mread_rnd_buffer_size = 2M# thread_concurrency = 24join_buffer_size = 1M# table_cache = 32768thread_stack = 512kmax_length_for_sort_data = 16k# INNODB #innodb_flush_method = O_DIRECTinnodb_log_buffer_size = 16Minnodb_flush_log_at_trx_commit = 2innodb_file_per_table = 1innodb_buffer_pool_size = 256M#innodb_buffer_pool_instances = 8innodb_stats_on_metadata = offinnodb_open_files = 8192innodb_read_io_threads = 16innodb_write_io_threads = 16innodb_io_capacity = 20000innodb_thread_concurrency = 0innodb_lock_wait_timeout = 60innodb_old_blocks_time=1000innodb_use_native_aio = 1innodb_purge_threads=1innodb_change_buffering=allinnodb_log_file_size = 64Minnodb_log_files_in_group = 2innodb_data_file_path = ibdata1:256M:autoextendinnodb_rollback_on_timeout=on# LOGGING #log_error = /usr/local/mysql/sql_log/mysql-error.log# log_queries_not_using_indexes = 1# slow_query_log = 1slow_query_log_file = /usr/local/mysql/sql_log/slowlog.log# TimeOut ##interactive_timeout = 30#wait_timeout = 30#net_read_timeout = 60[mysqldump]quickmax_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 = 16Msort_buffer_size = 5Mread_buffer = 2Mwrite_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服务重启