前言
MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,属于 Oracle 旗下产品。MySQL是最流行的关系型数据库管理系统之一,在 WEB 应用方面,MySQL是最好的 RDBMS (Relational Database Management System,关系数据库管理系统) 应用软件之一。
MySQL命名方案使用的发行版本名称由三个数字和一个可选的后缀组成。例如mysql-5.7.39-1.el7.x86_64.rpm-bundle.tar。版本名称中的数字解释如下:
第一个数字(5): 是主版本号。
第二个数字(7): 是次要版本号。总而言之,主要和次要数字构成发行版本号。
第三个数字(39): 是发行系列中的版本号。对于每个新的错误修正版本,此值均递增。在通常最新版本是最佳选择。
里程碑: 后缀用于指示版本的稳定性。可能的后缀是:m N(例如, m1, m2, m3,…)表示里程碑编号。rc表示发布候选(RC)。通过MySQL的所有内部测试后,发布候选版本被认为是稳定的。没有后缀表示正式版。生产环境中的选型。
https://dev.mysql.com/downloads/mysql/去mysql官网
点击Archives,选择对应的产品版本、操作系统(安装方式)和操作系统版本,点击Down进行下载
[root@loclhost ~]# hostnamectl
Static hostname: mysql2.x88.com
Icon name: computer-vm
Chassis: vm
Machine ID: 28cae06eae1740acbdfe4f88be6ffeb0
Boot ID: 1f9fe175b6a54d2dab41033927790634
Virtualization: vmware
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-1160.71.1.el7.x86_64
Architecture: x86-64
[root@loclhost ~]# hostnamectl set-hostname mysql2.x88.com
[root@loclhost ~]# yum install lrzsz sysstat elinks wget net-tools bash-completion -y
查询防火墙状态
[root@loclhost ~]# systemctl status firewalld
关防火墙
[root@loclhost ~]# systemctl stop firewalld #关闭防火墙
[root@loclhost ~]# systemctl disable firewalld #开机不启动防火墙
[root@loclhost ~]# systemctl status firewalld #查询防火墙状态
查询SELINUX状态
[root@loclhost ~]# getenforce
关闭SELINUX
[root@loclhost ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config #永久关闭SELINUX
[root@loclhost ~]# setenforce 0 #临时关闭
[root@loclhost ~]# getenforce
重启服务器后,查看SELINUX状态
[root@loclhost ~]# init 6 #重启服务器
[root@mysql2 ~]# getenforce
创建一个目录,用来保存rpm包
[root@mysql2 ~]# df -h #查询磁盘容量
[root@mysql2 ~]# mkdir -p /data/install
[root@mysql2 ~]# cd /data/install/
上传rpm安装包
安装包下载参考本文章 2.2章节
如服务器有外网的情况下可以使用wget进行下载
[root@mysql2 install]# wget -O /data/install/mysql-5.7.39-1.el7.x86_64.rpm-bundle.tar https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.39-1.el7.x86_64.rpm-bundle.tar
[root@mysql2 install]# rz
[root@mysql2 install]# ll
总用量 543132
-rw-r--r-- 1 root root 556165120 4月 12 12:37 mysql-5.7.39-1.el7.x86_64.rpm-bundle.tar
[root@mysql2 install]# tar -xvf mysql-5.7.39-1.el7.x86_64.rpm-bundle.tar #解压rpm安装包
mysql-community-client-5.7.39-1.el7.x86_64.rpm
mysql-community-common-5.7.39-1.el7.x86_64.rpm
mysql-community-devel-5.7.39-1.el7.x86_64.rpm
mysql-community-embedded-5.7.39-1.el7.x86_64.rpm
mysql-community-embedded-compat-5.7.39-1.el7.x86_64.rpm
mysql-community-embedded-devel-5.7.39-1.el7.x86_64.rpm
mysql-community-libs-5.7.39-1.el7.x86_64.rpm
mysql-community-libs-compat-5.7.39-1.el7.x86_64.rpm
mysql-community-server-5.7.39-1.el7.x86_64.rpm
mysql-community-test-5.7.39-1.el7.x86_64.rpm
卸载CentOS自带的Mariadb
[root@mysql2 install]# rpm -qa | grep mariadb
mariadb-libs-5.5.68-1.el7.x86_64
[root@mysql2 install]# rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64 #--nodeps 参数是忽略依赖关系
安装mysql需要的依赖
[root@mysql2 install]# yum install net-tools prel -y
按照以下顺序进行mysql的安装
[root@mysql2 install]# rpm -ivh mysql-community-common-5.7.39-1.el7.x86_64.rpm
警告:mysql-community-common-5.7.39-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:mysql-community-common-5.7.39-1.e################################# [100%]
[root@mysql2 install]# rpm -ivh mysql-community-libs-5.7.39-1.el7.x86_64.rpm
警告:mysql-community-libs-5.7.39-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:mysql-community-libs-5.7.39-1.el7################################# [100%]
[root@mysql2 install]# rpm -ivh mysql-community-client-5.7.39-1.el7.x86_64.rpm
警告:mysql-community-client-5.7.39-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:mysql-community-client-5.7.39-1.e################################# [100%]
[root@mysql2 install]# rpm -ivh mysql-community-server-5.7.39-1.el7.x86_64.rpm
警告:mysql-community-server-5.7.39-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:mysql-community-server-5.7.39-1.e################################# [100%]
mysql常用命令
实际坏境中,需要掌握以下命令来管理mysql的启停
• systemctl start mysqld 启动
• systemctl stop mysqld 停止
• systemctl status mysqld 状态查询
• systemctl restart mysqld 重启
• systemctl enable mysqld 开机自动启动
• systemctl disable mysqld 关闭开机自动启动
启动mysql
[root@mysql2 install]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: inactive (dead)
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
[root@mysql2 install]# systemctl start mysqld
[root@mysql2 install]# lsof -i:3306
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 2938 mysql 21u IPv6 40870 0t0 TCP *:mysql (LISTEN)
修改初始化密码
安装MySQL的时候,会把初始密码报错在日志里面,日志的位置在/var/log目录下的mysqld.log文件里面,使用如下命令查询初始化密码
[root@mysql2 install]# cat /var/log/mysqld.log | grep 'password is generated'
查询到密码后,我们使用mysql -uroot -p命令输入查询到初始化密码登陆数据库
[root@mysql2 install]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.39
Copyright (c) 2000, 2022, 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>
登陆数据库成功后,使用如下命令修改root账户密码
mysql> alter user 'root'@'localhost' identified by 'Cjlr2023!';
Query OK, 0 rows affected (0.00 sec)
授权root账户所有权限,并且可以远程登陆
mysql> grant all privileges on *.* to 'root'@'%' identified by 'Cjlr2023!' with grant option;
Query OK, 0 rows affected, 1 warning (0.00 sec)
刷新权限
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
远程登陆测试
查看当前mysql安装情况
[root@mysql2 install]# rpm -qa | grep mysql
mysql-community-common-5.7.39-1.el7.x86_64
mysql-community-client-5.7.39-1.el7.x86_64
mysql-community-server-5.7.39-1.el7.x86_64
mysql-community-libs-5.7.39-1.el7.x86_64
停止mysql服务
[root@mysql2 install]# systemctl stop mysqld
删除之前安装的mysql
[root@mysql2 install]# rpm -evh --nodeps mysql-community-server-5.7.39-1.el7.x86_64 mysql-community-client-5.7.39-1.el7.x86_64 mysql-community-libs-5.7.39-1.el7.x86_64 mysql-community-common-5.7.39-1.el7.x86_64
准备中... ################################# [100%]
正在清理/删除...
1:mysql-community-server-5.7.39-1.e################################# [ 25%]
2:mysql-community-client-5.7.39-1.e################################# [ 50%]
3:mysql-community-libs-5.7.39-1.el7################################# [ 75%]
4:mysql-community-common-5.7.39-1.e################################# [100%]
查找之前mysql的目录,都删掉
[root@mysql2 install]# find / -name mysql
/etc/selinux/targeted/active/modules/100/mysql
/var/lib/mysql
/var/lib/mysql/mysql
/usr/share/mysql
[root@mysql2 install]# rm -rf /var/lib/mysql /etc/selinux/targeted/active/modules/100/mysql/ /usr/share/mysql/
删除mysql用户及用户组
[root@mysql2 install]# userdel -r mysql
[root@mysql2 install]# groupdel mysql
mysql创建用户规范
mysql:用户建议虚拟用户,只用于mysql进程运行使用,不允许登录、不创建家目录。
useradd -s /sbin/nologin -M mysql
mysql目录规范
系统目录 /opt/mysql/my-xx.xx
软连接 ln -s /opt/mysql/my-xx.xx /usr/local/mysql
数据目录 /data/mysql/my+port/{data,logs}
配置文件 /data/mysql/my+port/my+port.cnf
安装包下载参考本文章 2.2章节
如服务器有外网的情况下可以使用wget进行下载
[root@mysql2 install]# wget -O /data/install/mysql-5.7.39-1.el7.x86_64.rpm-bundle.tar https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.39-linux-glibc2.12-x86_64.tar.gz
选择mysql二进制包上传
[root@mysql2 install]# rz
解压二进制包
[root@mysql2 install]# tar -xvf mysql-5.7.39-linux-glibc2.12-x86_64.tar.gz
创建用户
[root@mysql2 install]# useradd -s /sbin/nologin -M mysql
创建目录
[root@mysql2 install]# mkdir -p /data/mysql/my3306/{data,logs}
更改用户和组
[root@mysql2 install]# chown -R mysql:mysql /data/mysql/
[root@mysql2 install]# chmod -R 750 /data/mysql/
将软件部分移动到指定目录
[root@mysql2 install]# mkdir -p /opt/mysql
[root@mysql2 install]# mv mysql-5.7.39-linux-glibc2.12-x86_64 /opt/mysql/
创建软连接
[root@mysql2 install]# ln -s /opt/mysql/mysql-5.7.39-linux-glibc2.12-x86_64/ /usr/local/mysql
删除系统自带的mariadb
[root@mysql2 install]# rpm -qa | grep mariadb
mariadb-libs-5.5.68-1.el7.x86_64
[root@mysql2 install]# rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64
修改mysql配置文件
[root@mysql2 install]# cat <> /data/mysql/my3306/my3306.cnf
[mysqld]
user=mysql
basedir=/usr/local/mysql
datadir=/data/mysql/my3306/data
socket = /data/mysql/my3306/mysql.sock
server_id = 1
port = 3306
log_error=/data/mysql/my3306/logs/error.log
log_bin=/data/mysql/my3306/logs/mysql-bin
binlog_format=row
gtid_mode=on
enforce_gtid_consistency=true
log_slave_updates=1
max_connections=1024
wait_timeout=60
sort_buffer_size=2M
max_allowed_packet=32M
join_buffer_size=2M
innodb_buffer_pool_size=128M
innodb_flush_log_at_trx_commit=1
innodb_log_buffer_size=32M
innodb_log_file_size=128M
innodb_log_files_in_group=2
binlog_cache_size=2M
max_binlog_cache_size=8M
max_binlog_size=512M
expire_logs_days=7
slow_query_log=on
slow_query_log_file=/data/mysql/my3306/logs/slow.log
long_query_time=0.5
log_queries_not_using_indexes=1
EOF
[root@mysql2 install]# more /data/mysql/my3306/my3306.cnf
安装依赖包
[root@mysql2 install]# yum install libaio-devel numactl -y
配置环境变量
[root@mysql2 install]# cat <> ~/.bash_profile
export PATH=/usr/local/mysql/bin:$PATH
EOF
初始化数据库
[root@mysql2 install]# mysqld --defaults-file=/data/mysql/my3306/my3306.cnf --initialize --user=mysql
--defaults-file: 如果参数文件没有在默认的位置,需要手工指定my.cnf的位置
--initialize:相对应的参数是---initialize-insecure。 “ 默认安全 ”安装(即包括生成随机初始的 root密码)。在这种情况下,密码被标记为已过期,您将需要选择一个新密码。使用--initialize-insecure,不会root生成密码。这是不安全的;可以直接登录进入。
--basedir=/usr/local/mysql 可选参数,如果my.cnf当中没有指定路径,需要手工指定,mysql软件位置。
--datadir=/opt/mysql/mysql/data 可选参数,如果my.cnf当中没有指定路径,需要手工指定,mysql数据目录位置。
--user=mysql 数据初始化后,所属的用户
查看密码
[root@mysql2 install]# grep -i 'password' /data/mysql/my3306/logs/error.log
后台启动mysql
nohup:结果默认会输出到nohup.out
&:后台运行
[root@mysql2 install]#nohup mysqld --defaults-file=/data/mysql/my3306/my3306.cnf &
[root@mysql2 install]# lsof -i:3306
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 7562 mysql 23u IPv6 97311 0t0 TCP *:mysql (LISTEN)
重置root密码
除了重置初始root密码的动作以外,其它全部默认即可。初始的root密码在错误日志当中
[root@mysql2 install]# mysql_secure_installation -uroot -p --socket=/data/mysql/my3306/mysql.sock
#输入日志里密码
Enter password:
Securing the MySQL server deployment.
The existing password for the user account root has expired. Please set a new password.
#设置新密码
New password:
#确认新密码
Re-enter new password:
#是否设置验证密码插件
VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?
Press y|Y for Yes, any other key for No:
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) :
... skipping.
# 是否删除匿名账户
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) :
... skipping.
# 是否禁止远程登录root用户
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) :
... skipping.
# 是否删除test 数据库
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) :
... skipping.
# 是否重新加载特权表
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) :
... skipping.
All done!
登陆mysql
[root@mysql2 install]# mysql -uroot -p --socket=/data/mysql/my3306/mysql.sock
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.39-log MySQL Community Server (GPL)
Copyright (c) 2000, 2022, 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>
授权root远程登陆的权限
mysql> grant all privileges on *.* to 'root'@'%' identified by 'Cjlr2023!' with grant option;
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
远程登陆测试
[root@mysql2 install]# mysqladmin -uroot -p123456 shutdown --socket=/data/mysql/my3306/mysql.sock
修改46、47、207、266行
[root@mysql2 support-files]# vim /usr/local/mysql/support-files/mysql.server
46 basedir=/usr/local/mysql #软件位置
47 datadir=/data/mysql/my3306/data #数据目录
207 conf=/data/mysql/my3306/my3306.cnf #参数文件
266 $bindir/mysqld_safe --datadir="$datadir" --socket=/data/mysql/my3306/mysql.sock --pid-file="$mysqld_pid_file_path" $other_args >/dev/nu ll & #添加socket文件位置
[root@mysql2 support-files]# ./mysql.server
Usage: mysql.server {start|stop|restart|reload|force-reload|status} [ MySQL server options ]
[root@mysql2 support-files]# ./mysql.server stop
Shutting down MySQL.. SUCCESS!
[root@mysql2 support-files]# ./mysql.server start
Starting MySQL. SUCCESS!
[root@mysql2 support-files]# ./mysql.server status
SUCCESS! MySQL running (3807)
[root@mysql2 support-files]# ./mysql.server reload
SUCCESS! Reloading service MySQL
添加到mysqld到service
[root@mysql2 support-files]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@mysql2 support-files]# chkconfig /etc/init.d/mysqld on
[root@mysql2 support-files]# chkconfig mysqld --list
注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。
要列出 systemd 服务,请执行 'systemctl list-unit-files'。
查看在具体 target 启用的服务请执行
'systemctl list-dependencies [target]'。
mysqld 0:关 1:关 2:开 3:开 4:开 5:开 6:关
[root@mysql2 support-files]# service mysqld stop
Shutting down MySQL.. SUCCESS!
[root@mysql2 support-files]# service mysqld start
Starting MySQL. SUCCESS!
[root@mysql2 support-files]# service mysqld status
SUCCESS! MySQL running (4080)
查询数据库状态
[root@mysql2 ~]# lsof -i:3306
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 4080 mysql 29u IPv6 40197 0t0 TCP *:mysql (LISTEN)
关闭数据库
[root@mysql2 install]# mysqladmin -uroot -p123456 shutdown --socket=/data/mysql/my3306/mysql.sock
如果配置了使用service控制mysql启动或关闭,删除service服务和脚本
[root@mysql2 ~]# chkconfig --del mysqld
[root@mysql2 ~]# rm -rf /etc/init.d/mysqld
删除软件目录
[root@mysql2 ~]# rm -rf /opt/mysql/
[root@mysql2 ~]# rm -rf /usr/local/mysql
删除数据目录
[root@mysql2 ~]# rm -rf /data/mysql/
查找之前mysql的目录,都删掉
[root@mysql2 ~]# find / -name mysql
/run/lock/subsys/mysql
/var/spool/mail/mysql
[root@mysql2 ~]# rm -rf /run/lock/subsys/mysql
[root@mysql2 ~]# rm -rf /var/spool/mail/mysql
删除mysql用户及用户组
[root@mysql2 install]# userdel -r mysql
[root@mysql2 install]# groupdel mysql