一、安装环境需求
根据上述的部署需求来来配置相应的硬件和划分目录空间。
二、环境准备:
1、修改主机名
[root@mysql ~]hostnamectl set-hostname mysql
2、检查swap分区
[root@mysql ~]free -h
3、检查文件系统分区
虚拟机分区的时候需要给磁盘预留充足的空间,我们的所有MySQL相关文件,都会创建在/data目录下。
[root@mysql ~] df -h
4、检查操作系统版本
[root@mysql ~]# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 7.6 (Maipo)
5、检查操作系统内核信息
[root@mysql ~]# uname -a
Linux mysql 3.10.0-957.el7.x86_64 #1 SMP Thu Oct 4 20:48:51 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
6、检查并关闭防火墙
虽然MySQL数据库支持启动防火墙服务iptables,但是本身的权限控制及加密连接已经能够很好地保证安全性,故建议关闭系统自带的防火墙服务iptables。
关闭数据库主机防火墙的命令如下:
[root@mysql ~]# systemctl status firewalld
[root@mysql ~]# systemctl stop firewalld
[root@mysql ~]# systemctl disable firewalld
[root@mysql ~]# systemctl status firewalld
7、检查是否安装MySQL,如果安装就先卸载
[root@mysql ~]# rpm -qa |grep mysql
[root@mysql ~]# yum remove mysql*
8、关闭selinux
将/etc/selinux/config文件中的SELINUX=enabled修改为SELINUX=disabled,该修改重启主机生效。
[root@mysql ~]# vi /etc/selinux/config
[root@mysql ~]# cat /etc/selinux/config|grep ^SELINUX=
SELINUX=disabled
9、创建目录
[root@mysql ~] mkdir -p /data/software
[root@mysql ~] mkdir -p /data/mysql
[root@mysql ~] mkdir -p /data/mysql/data
[root@mysql ~] mkdir -p /data/mysql/log
[root@mysql ~] mkdir -p /data/mysql/tmp
10、用户创建:
useradd mysql
11、配置本地yum源安装依赖包
#########yum源配置
[root@mysql ~]# cd /etc/yum.repos.d/
[root@mysql yum.repos.d]# vi yum.repo
[base]
name=base
baseurl=file:///mnt
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
[root@mysql yum.repos.d]# mount /dev/sr0 /mnt
[root@mysql yum.repos.d]# yum clean all
[root@mysql yum.repos.d]# yum makecache
#安装所需依赖包
yum install cmake make gcc gcc-c++ bison libaio ncurses-devel perl perl-DBI perl-DBD-MySQL perl-Time-HiRes readline-devel numactl zlib-devel curldevel -y
12、修改资源限制
为放开操作系统对主机用户所产生的进程使用主机资源(文件句柄,线程等)的限制,需要重新设置MYSQL数据库用户的SHELL资源限制。
在安装数据库前需要为MySQL配置启动SHELL资源的限制条件,如下:
在/etc/security/limits.conf文件中对MYSQL数据库用户进程SHELL资源做如下限制。
vi /etc/security/limits.conf
mysql soft nofile 65535
mysql hard nofile 65535
mysql soft nproc 65535
mysql hard nproc 65535
13、内核参数调整
为了使MySQL数据库能更加稳定、高效的运行,可以配置一些内核参数。
在参数文件/etc/sysctl.conf中机配置如下内核参数,并用sysctl -p命令使其生效:
net.ipv4.tcp_max_syn_backlog = 65535
net.ipv4.tcp_max_tw_buckets = 8000
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout = 10
二、安装:
1、下载包文件:
下载安装包,并上传文件至/data/software目录。下载地址:
https://dev.mysql.com/downloads/mysql/
解压安装包并设置软链接
[root@mysql ~]# cd /data/software/
[root@mysql ~]# tar -xvf mysql-8.0.37-linux-glibc2.12-x86_64.tar.xz
[root@mysql ~]# ln -s /data/software/mysql-8.0.37-linux-glibc2.12-x86_64.tar.xz /usr/local/mysql
2、权限调整:
chown -R mysql:mysql /usr/local/mysql /data/mysql/*
三、配置文件:
vi /etc/my.cnf
[client] port = 3306
socket = /data/mysql/tmp/mysql.sock
[mysql]
prompt="\u@db \R:\m:\s [\d]> "
no-auto-rehash
[mysqld]
user = mysql
port = 3306
basedir = /usr/local/mysql
datadir = /data/mysql/data
socket = /data/mysql/tmp/mysql.sock
pid-file = /data/mysql/tmp/mysql.pid
character-set-server=utf8mb4
collation-server = utf8mb4_general_ci
sql_mode='NO_UNSIGNED_SUBTRACTION,NO_ENGINE_SUBSTITUTION'
open_files_limit = 65535
innodb_open_files = 65535
back_log=1024
max_connections = 512
max_connect_errors=1000000
interactive_timeout=300
wait_timeout=300
max_allowed_packet = 1024M
secure_file_priv=''
log-error=/data/mysql/log/error.log
slow_query_log=ON
slow_query_log_file=/data/mysql/log/slow_mysql.log
long_query_time=2
innodb_flush_log_at_trx_commit=1
innodb_log_file_size =1G
innodb_log_files_in_group=3
innodb_log_group_home_dir=./
log-bin-trust-function-creators=1
sync_binlog = 1
binlog_cache_size = 16M
max_binlog_cache_size = 1G
max_binlog_size=1G
expire_logs_days = 30
log-bin= /data/mysql/log/binlog-mysql
binlog_format=row
binlog_row_image=full
server-id = 1
default_authentication_plugin =mysql_native_password
# 大小根据实际系统内存情况而定
innodb_buffer_pool_size=4G
innodb_buffer_pool_instances=2
四、初始化
1、初始化操作
[root@mysql]# /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/data/mysql/data --user=mysql --initialize
[root@mysql]# ls /data/mysql/data
2、查看密码
[root@mysql soft]# cat /data/mysql/log/error.log | grep password
2024-05-15T07:55:42.353273Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: E2e2o>7quwaS
五、启动数据库:
[root@mysql]# service mysql start
[root@mysql]# /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf &
六、安装后调整
#环境变量调整
[root@mysql]# echo 'export PATH=/usr/local/mysql/bin:$PATH' >> /etc/profile
[root@mysql ~]# source /etc/profile
#修改密码
[root@mysql ~]# /usr/local/mysql/bin/mysqladmin -uroot -p password
Enter password: New password:
Confirm new password:
[root@localhost ~]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@localhost ~]# chkconfig --add mysqld
[root@localhost ~]# chkconfig mysqld on
# 登入Mysql
[root@mysql soft]# mysql -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.37 MySQL Community Server - GPL
Copyright (c) 2000, 2024, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> select version();
+-----------+
| version() |
+-----------+
| 8.0.37 |
+-----------+
1 row in set (0.00 sec)
七、忘记密码问题处理
如果在登录的时候,记不清密码了,可以使用此方法,跳过登录密码验证,可以直接进入数据库。
vi /etc/my.cnf
[mysqld]
skip-grant-tables
修改之后重启数据库
systemctl restart mysqld
重启数据库之后登录还是需要提示输入密码,这时直接回车就可以
[root@mysql ~]# mysql -p
Enter password:
八、基础常用命令
查看数据库
mysql> show databases;
切换数据库
mysql> use mysql;
查看当前用户
mysql> select current_user();