Linux环境安装MySQL详细步骤
一、Linux环境安装MySQL详细步骤
一、安装之前检测系统是否有自带的MySQL
rpm -qa | grep mysql
#检查是否安装过MySQL
rpm -qa | grep mariadb
#检查是否存在 mariadb 数据库(内置的MySQL数据库),有则强制删除
rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64
#强制删除
rpm -e --nodeps mariadb-5.5.68-1.el7.x86_64
#强制删除
[root@localhost ~]# rpm -qa | grep mysql
[root@localhost ~]# rpm -qa | grep mariadb
mariadb-libs-5.5.68-1.el7.x86_64
[root@localhost ~]# rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64
[root@localhost ~]#
二、安装步骤
1、下载Mysql源
MySQL源
[root@localhost ~]# rpm -qa|grep mysql
[root@localhost ~]# curl -O https://repo.mysql.com//mysql57-community-release-el7-11.noarch.rpm
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 25680 100 25680 0 0 2783 0 0:00:09 0:00:09 --:--:-- 7276
[root@localhost ~]# ll
总用量 36
-rw-------. 1 root root 1571 3月 6 17:22 anaconda-ks.cfg
-rw-r--r--. 1 root root 1619 3月 6 17:23 initial-setup-ks.cfg
-rw-r--r--. 1 root root 25680 3月 10 23:56 mysql57-community-release-el7-11.noarch.rpm
2、安装MySQL源
yum localinstall mysql57-community-release-el7-11.noarch.rpm
[root@localhost ~]# yum localinstall -y mysql57-community-release-el7-11.noarch.rpm
已安装:
mysql57-community-release.noarch 0:el7-11
完毕!
3、检查MySQL源是否安装成功
yum repolist enabled | grep mysql.*
[root@localhost ~]# yum repolist enabled | grep mysql.*
mysql-connectors-community/x86_64 MySQL Connectors Community 213
mysql-tools-community/x86_64 MySQL Tools Community 96
mysql57-community/x86_64 MySQL 5.7 Community Server 642
4、安装MySQL
yum install -y mysql-community-server
#首次安装会失败,公钥尚未安装。
[root@localhost ~]# yum install -y mysql-community-server
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.nju.edu.cn
* extras: mirrors.nju.edu.cn
* updates: mirrors.nju.edu.cn
mysql-community-server-5.7.41-1.el7.x86_64.rpm 的公钥尚未安装
`失败的软件包是:mysql-community-server-5.7.41-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
# 导入之后,重新安装
[root@localhost ~]# rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
[root@localhost ~]# yum install -y mysql-community-server
已安装:
mysql-community-server.x86_64 0:5.7.41-1.el7
作为依赖被安装:
mysql-community-client.x86_64 0:5.7.41-1.el7 mysql-community-common.x86_64 0:5.7.41-1.el7
mysql-community-libs.x86_64 0:5.7.41-1.el7
5、 查看是否安装成功
[root@localhost ~]# yum list installed mysql-*
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.nju.edu.cn
* extras: mirrors.nju.edu.cn
* updates: mirrors.nju.edu.cn
已安装的软件包
mysql-community-client.x86_64 5.7.41-1.el7 @mysql57-community
mysql-community-common.x86_64 5.7.41-1.el7 @mysql57-community
mysql-community-libs.x86_64 5.7.41-1.el7 @mysql57-community
mysql-community-server.x86_64 5.7.41-1.el7 @mysql57-community
6、启动MySQL并检查MySQL运行状态
[root@localhost ~]# systemctl start mysqld
[root@localhost ~]#
[root@localhost ~]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since 二 2023-03-14 11:32:35 CST; 9s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 32863 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 32810 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 32866 (mysqld)
Tasks: 27
CGroup: /system.slice/mysqld.service
└─32866 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
3月 14 11:32:28 localhost.localdomain systemd[1]: Starting MySQL Server...
3月 14 11:32:35 localhost.localdomain systemd[1]: Started MySQL Server.
7、获取MySQL默认登录密码,并修改默认密码
grep 'temporary password' /var/log/mysqld.log
[root@localhost ~]# grep 'temporary password' /var/log/mysqld.log
2023-03-14T03:32:32.459538Z 1 [Note] A temporary password is generated for root@localhost: xoRtju)bm8e+
#默认密码:xoRtju)bm8e+
[root@localhost ~]# mysql -uroot -p #登入MySQL
Enter password: #输入默认密码
# 先修改密码 ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Yunweiku@123';
Query OK, 0 rows affected (0.00 sec)
# MySQL默认的密码复杂度为 MEDIUM,所以新密码至少为8位,并且必须包含大、小写字母、数字和特殊字符
8、授予root用户远程访问权限,并刷新权限使生效
# 授予root用户远程访问权限 grant all privileges on *.* to 'root' @'%' identified by 'Mysql@123';
# 1、第一个*是数据库,可以改成允许访问的数据库名称
# 2、第二个*是数据库的表名称,代表允许访问任意的表
# 3、root代表远程登录使用的用户名,可以自定义
# 4、%代表允许任意ip登录,如果你想指定特定的IP,可以把%替换掉就可以了
# 5、password代表远程登录时使用的密码,可以自定义
# 6、让权限立即生效:flush privileges;
mysql> grant all privileges on *.* to 'root' @'%' identified by 'Yunweiku@123';
Query OK, 0 rows affected, 1 warning (0.02 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye
三、卸载MySQL
1、查看MySQL安装情况:rpm -qa | grep mysql
[root@localhost ~]# rpm -qa | grep mysql
mysql-community-libs-5.7.41-1.el7.x86_64
mysql-community-client-5.7.41-1.el7.x86_64
mysql-community-server-5.7.41-1.el7.x86_64
mysql-community-common-5.7.41-1.el7.x86_64
mysql57-community-release-el7-11.noarch
2、强制删除已安装MySQL并删除文件
#强制删除已安装MySQL
[root@localhost ~]# rpm -e --nodeps mysql-community-libs
[root@localhost ~]# rpm -e --nodeps mysql-community-client
[root@localhost ~]# rpm -e --nodeps mysql-community-server
[root@localhost ~]# rpm -e --nodeps mysql57-community-release
[root@localhost ~]# rpm -e --nodeps mysql-community-common
[root@localhost ~]# rpm -qa|grep mysql
[root@localhost ~]#
#查找所有MySQL目录并删除:find / -name mysql
[root@localhost ~]# find / -name mysql
find: ‘/proc/35432’: 没有那个文件或目录
/var/lib/mysql
/var/lib/mysql/mysql
/usr/lib64/mysql
/usr/share/mysql
[root@localhost ~]# rm -rf /var/lib/mysql
[root@localhost ~]# rm -rf /usr/share/mysql
[root@localhost ~]# rm -rf /usr/lib64/mysql
[root@localhost ~]#
3、 删除my.cnf:rm -f /etc/my.cnf
[root@localhost ~]# rm -f /etc/my.cnf
[root@localhost ~]#
运维库