MySQL——linux中手动安装mysql
步骤一:安装MySQL
1.运行以下命令更新YUM源:
查看代码
sudo rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
2.安装MySQL:
查看代码
sudo yum -y install mysql-community-server --enablerepo=mysql80-community --nogpgcheck
3.查看MySQL版本信息:
查看代码
mysql -V
mysql Ver 8.0.28 for Linux on x86_64 (MySQL Community Server - GPL)
#########################################################################
1. 进入Linux服务器指定目录执行命令:
wget http://repo.mysql.com/mysql57-community-release-el7-8.noarch.rpm
2.安装yum源
rpm -ivh mysql57-community-release-el7-8.noarch.rpm
3.查看源
ll /etc/yum.repos.d
4.安装MySQL: yum install -y mysql-server
查看代码
[root@mysql]# wget http://repo.mysql.com/mysql57-community-release-el7-8.noarch.rpm
--2022-09-11 11:58:07-- http://repo.mysql.com/mysql57-community-release-el7-8.noarch.rpm
正在解析主机 repo.mysql.com (repo.mysql.com)... 104.75.165.42
正在连接 repo.mysql.com (repo.mysql.com)|104.75.165.42|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:9116 (8.9K) [application/x-redhat-package-manager]
正在保存至: “mysql57-community-release-el7-8.noarch.rpm”
100%[=========================================================================================================================================================>] 9,116 --.-K/s 用时 0s
2022-09-11 11:58:09 (183 MB/s) - 已保存 “mysql57-community-release-el7-8.noarch.rpm” [9116/9116])
[root@mysql]# ll
总用量 12
-rw-r--r-- 1 root root 9116 4月 11 2016 mysql57-community-release-el7-8.noarch.rpm
[root@mysql]# rpm -ivh mysql57-community-release-el7-8.noarch.rpm
警告:mysql57-community-release-el7-8.noarch.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:mysql57-community-release-el7-8 ################################# [100%]
[root@mysql]# ll
总用量 12
-rw-r--r-- 1 root root 9116 4月 11 2016 mysql57-community-release-el7-8.noarch.rpm
[root@mysql]# ll /etc/yum.repos.d/
总用量 20
-rw-r--r-- 1 root root 675 9月 3 09:30 CentOS-Base.repo
-rw-r--r-- 1 root root 230 9月 3 09:30 epel.repo
-rw-r--r-- 1 root root 1222 3月 23 2016 mysql-community.repo
-rw-r--r-- 1 root root 1236 3月 23 2016 mysql-community-source.repo
-rw-r--r-- 1 root root 108 9月 3 23:45 nginx.repo
[root@mysql]# yum install -y mysql-server
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
正在解决依赖关系
--> 正在检查事务
---> 软件包 mysql-community-server.x86_64.0.5.7.39-1.el7 将被 安装
--> 正在处理依赖关系 mysql-community-common(x86-64) = 5.7.39-1.el7,它被软件包 mysql-community-server-5.7.39-1.el7.x86_64 需要
--> 正在处理依赖关系 mysql-community-client(x86-64) >= 5.7.9,它被软件包 mysql-community-server-5.7.39-1.el7.x86_64 需要
--> 正在检查事务
如果安装过程中出现以下异常时:
查看代码
从 file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql 检索密钥
导入 GPG key 0x5072E1F5:
用户ID : "MySQL Release Engineering <mysql-build@oss.oracle.com>"
指纹 : a4a9 4068 76fc bd3c 4567 70c8 8c71 8d3b 5072 e1f5
软件包 : mysql57-community-release-el7-8.noarch (installed)
来自 : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
mysql-community-client-5.7.39-1.el7.x86_64.rpm 的公钥尚未安装
失败的软件包是:mysql-community-client-5.7.39-1.el7.x86_64
GPG 密钥配置为:file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
执行命令:
查看代码
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
步骤二:配置MySQL
1.启动MySQL服务
查看代码
systemctl start mysqld
2.设置MySQL服务开机自启动
查看代码
systemctl enable mysqld
3.查看/var/log/mysqld.log文件,获取并修改root用户初始化密码
查看代码
[root@hua30 ~]# grep 'temporary password' /var/log/mysqld.log
2022-03-21T07:55:57.425757Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: w;-14>ww2*E1
4.执行命令:mysql_secure_installation,对MySQL进行安全性配置
查看代码
[root@huan30 ~]# mysql_secure_installation
# 1.重置root用户初始化密码
Enter password for user root: #输入已获取的root用户初始密码
The existing password for the user account root has expired. Please set a new password.
New password: #输入新的MySQL密码
Re-enter new password: #重复输入新的MySQL密码
The 'validate_password' component is installed on the server.
The subsequent steps will run with the existing configuration
of the component.
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) :Y #输入Y选择更新MySQL密码。您也可以输入N不再更新MySQL密码。
New password: #输入新的MySQL密码
Re-enter new password: #重复输入新的MySQL密码
Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) :Y #输入Y确认使用已设置的密码。
# 2.删除匿名用户
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) :Y #输入Y删除MySQL默认的匿名用户。
Success.
# 3.禁止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) :Y #输入Y禁止root远程登录。
Success.
# 4.删除test库以及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) :Y #输入Y删除test库以及对test库的访问权限。
- Dropping test database...
Success.
- Removing privileges on test database...
Success.
# 5.重新加载授权表
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) :Y #输入Y重新加载授权表。
Success.
All done!
5.查看MySQL运行状态:systemctl status mysqld/service mysqld status
查看代码
[root@hua30 ~]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since 一 2022-03-21 15:56:00 CST; 16h ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Main PID: 23110 (mysqld)
Status: "Server is operational"
Tasks: 40
Memory: 500.7M
CGroup: /system.slice/mysqld.service
└─23310 /usr/sbin/mysqld
3月 21 15:55:54 hua30 systemd[1]: Starting MySQL Server...
3月 21 15:56:00 hua30 systemd[1]: Started MySQL Server.
6.查看MySQL安装路径:find / -name mysql or whereis mysql
查看代码
[root@hua30 ~]# find / -name mysql
/var/lib/mysql
/var/lib/mysql/mysql
/usr/bin/mysql
/usr/lib64/mysql
/etc/selinux/targeted/active/modules/100/mysql
查看代码
[root@hua30 ~]# whereis mysql
mysql: /usr/bin/mysql /usr/lib64/mysql /usr/share/man/man1/mysql.1.gz
步骤三:登录MySQL
1.登录MySQL数据库:mysql -u root -p,回车输入密码
查看代码
[root@hua30 ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 48
Server version: 8.0.28 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>
2.创建MySQL远程登录账号:
注意 实际创建账号时,需将示例密码123456
更换为符合要求的密码,并妥善保存。密码要求:长度为8至30个字符,必须同时包含大小写英文字母、数字和特殊符号。可以使用以下特殊符号:()` ~!@#$%^&*-;‘<>,.?/
查看代码
mysql> create user 'mysql_user'@'%' identified by '123456'; #创建数据库用户mysql_user,并授予远程连接权限。
mysql> grant all privileges on *.* to 'mysql_user'@'%'; #为mysql_user用户授权数据库所有权限。
mysql> flush privileges; #刷新权限。
3.查看创建用户信息及密码解密方式
查看代码
mysql> select host,user,plugin from user;
+-----------+------------------+-----------------------+
| host | user | plugin |
+-----------+------------------+-----------------------+
| % | mysql_user | mysql_native_password |
| localhost | mysql.infoschema | caching_sha2_password | |
+-----------+------------------+-----------------------+
# plugin为密码解密方式
卸载MySQL
1.查看已安装的MySQL
查看代码
### rpm -qa | grep -i mysql
mysql-community-libs-8.0.30-1.el7.x86_64
mysql-community-server-8.0.30-1.el7.x86_64
mysql80-community-release-el7-3.noarch
mysql-community-common-8.0.30-1.el7.x86_64
mysql-community-client-8.0.30-1.el7.x86_64
mysql-community-icu-data-files-8.0.30-1.el7.x86_64
mysql-community-libs-compat-8.0.30-1.el7.x86_64
mysql-community-client-plugins-8.0.30-1.el7.x86_64
2.检查MySQL服务状态并关闭服务进程
查看代码
[root@~]# service mysqld status
Redirecting to /bin/systemctl status mysqld.service
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since 六 2022-09-03 09:41:07 CST; 1 weeks 1 days ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Main PID: 13115 (mysqld)
Status: "Server is operational"
CGroup: /system.slice/mysqld.service
└─13115 /usr/sbin/mysqld
9月 03 09:41:03 huangdh-9930 systemd[1]: Starting MySQL Server...
9月 03 09:41:07 huangdh-9930 systemd[1]: Started MySQL Server.
[root@huangdh-9930 ~]# service systemctl status mysqld
[root@~]# systemctl stop mysqld
[root@~]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: inactive (dead) since 日 2022-09-11 11:27:34 CST; 13s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 13115 ExecStart=/usr/sbin/mysqld $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Main PID: 13115 (code=exited, status=0/SUCCESS)
Status: "Server shutdown complete"
3.卸载MySQL安装包
查看代码
[root@~]# rpm -qa | grep -i mysql
mysql-community-libs-8.0.30-1.el7.x86_64
mysql-community-server-8.0.30-1.el7.x86_64
mysql80-community-release-el7-3.noarch
mysql-community-common-8.0.30-1.el7.x86_64
mysql-community-client-8.0.30-1.el7.x86_64
mysql-community-icu-data-files-8.0.30-1.el7.x86_64
mysql-community-libs-compat-8.0.30-1.el7.x86_64
mysql-community-client-plugins-8.0.30-1.el7.x86_64
# 通过:rpm -ev package-name 命令卸载
[root@~]# rpm -ev mysql-community-client-8.0.30-1.el7.x86_64
软件包准备中...
mysql-community-client-8.0.30-1.el7.x86_64
[root@huangdh-9930 ~]# rpm -ev mysql-community-icu-data-files-8.0.30-1.el7.x86_64
软件包准备中...
mysql-community-icu-data-files-8.0.30-1.el7.x86_64
[root@~]# rpm -ev mysql-community-libs-compat-8.0.30-1.el7.x86_64
错误:依赖检测失败:
libmysqlclient.so.18()(64bit) 被 (已安裝) postfix-2:2.10.1-9.el7.x86_64 需要
libmysqlclient.so.18(libmysqlclient_18)(64bit) 被 (已安裝) postfix-2:2.10.1-9.el7.x86_64 需要
[root@huangdh-9930 ~]# rpm -ev --nodeps mysql-community-libs-compat-8.0.30-1.el7.x86_64
软件包准备中...
mysql-community-libs-compat-8.0.30-1.el7.x86_64
# 如果卸载过程中提示:错误:依赖检测失败,在 -ev 后面加上:--nodeps 参数跳过依赖检查
[root@~]# rpm -ev --nodeps mysql-community-client-plugins-8.0.30-1.el7.x86_64
软件包准备中...
mysql-community-client-plugins-8.0.30-1.el7.x86_64
[root@~]# rpm -qa | grep -i mysql
[root@~]#
4.查看MySQL安装路径,并删除对应的文件夹
查看代码
[root@ ~]# find / -name mysql
/var/lib/mysql
/var/lib/mysql/mysql
/root/software/mysql
/usr/lib64/mysql
/etc/selinux/targeted/active/modules/100/mysql
[root@ ~]# rm -rf /var/lib/mysql
[root@ ~]# rm -rf /var/lib/mysql/mysql
[root@ ~]# rm -rf /usr/lib64/mysql
[root@ ~]# rm -rf /etc/selinux/targeted/active/modules/100/mysql
5.查看MySQL是否卸载成功
查看代码
[root@~]# mysql -version
-bash: mysql: 未找到命令
[root@~]# rpm -qa | grep -i mysql
[root@~]#