前言 :最近自己的服务器重装了系统,上面的软件都没了,重装了MySQL5.7,但是遇到了许多问题,要么就是下载的是MySQL8.0要么就是mysql-community-server下载失败,经过几个小时的google终于安装上了,并且能够在本机的navicat(sqlyog)能够访问到服务器上的数据库。于是写了这篇详细的文档,给以后的自己以及遇到相同问题的朋友一个好的参考方案。
1. 卸载MySQL
安装之前先把服务器上关于MySQL的资源全部卸载,防止遇到不知名的错误(此方法同样适用于想卸载MySQL的)
-
1.1 yum方式查询是否安装了MySQL
yum list installed mysql*
如果出现:
Dependencies resolved. =============================================================================================================================================================================================== Package Architecture Version Repository Size =============================================================================================================================================================================================== Installing: mysql-community-server x86_64 5.7.29-1.el7 mysql57-community 175 M Installing dependencies: mysql-community-client x86_64 5.7.29-1.el7 mysql57-community 26 M mysql-community-common x86_64 5.7.29-1.el7 mysql57-community 311 k mysql-community-libs x86_64 5.7.29-1.el7 mysql57-community 2.5 M Transaction Summary
说明有,需要卸载。
-
1.2 yum方式卸载(如果有,则执行这一步)
1. yum remove mysql-community-server mysql-community-client mysql-community-common mysql-community-libs 2. rm -rf /var/lib/mysql 3. rm /etc/my.cnf
-
1.3 rpm查看是否安装mysql
rpm -qa | grep -i mysql
如果出现
mysql-community-client-5.7.27-1.el6.x86_64 mysql-community-common-5.7.29-1.el7.x86_64 mysql-community-server-5.7.29-1.el7.x86_64 mysql-community-libs-5.7.27-1.el6.x86_64
则说明存在
-
1.4 rpm卸载MySQL
rpm -e mysql-community-client-5.7.27-1.el6.x86_64 rpm -e mysql-community-common-5.7.29-1.el7.x86_64 rpm -emysql-community-server-5.7.29-1.el7.x86_64 rpm -e mysql-community-libs-5.7.27-1.el6.x86_64 cd /var/lib/ rm -rf mysql/
-
1.5 清除剩余资源文件:
whereis mysql 出现:mysql: /usr/bin/mysql /usr/lib64/mysql /usr/share/mysql /usr/share/man/man1/mysql.1.gz 删除:rm -rf "上面的资源文件(如:/usr/bin/mysql)"
-
1.6 删除MySQL残留的配置文件
rm –rf /usr/my.cnf rm -rf /root/.mysql_sercret
-
1.7 再次检查是否完全删除
chkconfig --list | grep -i mysql chkconfig --del mysqld
如果还有资源没有删除干净,重复以上方法
2. 安装MySQL
-
2.1 添加MySQL存储库
-
禁用MySQL默认的AppStream存储库:
sudo dnf remove @mysql sudo dnf module reset mysql && sudo dnf module disable mysql
-
centos8没有MySQL存储库,因此我们将使用centos 7存储库。创建一个新的存储库文件。
sudo vim /etc/yum.repos.d/mysql-community.repo
-
将以下数据插入上面的存储库中
[mysql57-community] name=MySQL 5.7 Community Server baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/ enabled=1 gpgcheck=0 [mysql-connectors-community] name=MySQL Connectors Community baseurl=http://repo.mysql.com/yum/mysql-connectors-community/el/7/$basearch/ enabled=1 gpgcheck=0 [mysql-tools-community] name=MySQL Tools Community baseurl=http://repo.mysql.com/yum/mysql-tools-community/el/7/$basearch/ enabled=1 gpgcheck=0
-
-
2.2 安装MySQL(这里我选择MySQL5.7)
sudo dnf --enablerepo=mysql57-community install mysql-community-server
-
2.2.1 如果安装失败,使用以下方法进行安装
- 先下载rpm包
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-community-common-5.7.27-1.el6.x86_64.rpm wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-community-libs-5.7.27-1.el6.x86_64.rpm wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-community-client-5.7.27-1.el6.x86_64.rpm wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-community-server-5.7.27-1.el6.x86_64.rpm
- 再安装
yum install -y mysql-community-common-5.7.27-1.el6.x86_64.rpm yum install -y mysql-community-libs-5.7.27-1.el6.x86_64.rpm yum install -y mysql-community-client-5.7.27-1.el6.x86_64.rpm yum install -y mysql-community-server-5.7.27-1.el6.x86_64.rpm
2.3 下载完成后检查版本
[root@aliyun-zuojun ~]# rpm -qi mysql-community-server
Name : mysql-community-server
Version : 5.7.29
Release : 1.el7
Architecture: x86_64
Install Date: Sat 22 Feb 2020 11:04:07 AM CST
Group : Applications/Databases
Size : 801919839
License : Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. Under GPLv2 license as shown in the Description field.
Signature : DSA/SHA1, Thu 19 Dec 2019 04:12:40 PM CST, Key ID 8c718d3b5072e1f5
Source RPM : mysql-community-5.7.29-1.el7.src.rpm
Build Date : Wed 18 Dec 2019 09:31:48 PM CST
Build Host : loki02.no.oracle.com
Relocations : (not relocatable)
Packager : MySQL Release Engineering <mysql-build@oss.oracle.com>
Vendor : Oracle and/or its affiliates
URL : http://www.mysql.com/
Summary : A very fast and reliable SQL database server
出现以上信息说明安装成功
-
2.4 检查 mysql 源是否安装成功
yum repolist enabled | grep "mysql.*-community.*" 出现以下信息说明安装成功: mysql-connectors-community MySQL Connectors Community 141 mysql-tools-community MySQL Tools Community 105 mysql57-community MySQL 5.7 Community Server
-
2.5 启动MySQL
systemctl start mysqld
-
2.6 查看启动状态
systemctl status mysqld // 出现以下信息,则启动成功 Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled) Active: active (running) since Sat 2020-02-22 11:14:47 CST; 2h 19min ago Docs: man:mysqld(8) http://dev.mysql.com/doc/refman/en/using-systemd.html Process: 21345 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS) Process: 21323 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS) Main PID: 21349 (mysqld) Tasks: 30 (limit: 11516) Memory: 209.1M CGroup: /system.slice/mysqld.service ?..21349 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
-
2.7 设置开机启动
systemctl enable mysqld
-
2.8 刷新所有修改过的配置文件
systemctl daemon-reload
-
2.9 获取安装mysql后生成的临时密码,用于登录
grep 'temporary password' /var/log/mysqld.log # 如果出现如下列信息,密码为: BL=azx(1u;Br 2020-02-22T03:05:17.741049Z 1 [Note] A temporary password is generated for root@localhost: BL=azx(1u;Br
-
2.10 登录MySQL
mysql -uroot -p # 再输入上面查找得到的临时密码即可进入mysql
-
2.11 修改登录密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!(修改后的密码,注意必须包含大小写字母数字以及特殊字符并且长度不能少于8位,否则会报错)'; 或者通过:mysql> set password for 'root'@'localhost'=password('MyNewPass4!'); 或者通过:mysql> use mysql; mysql> update user set password=PASSWORD('MyNewPass5!') where user='root'; mysql> flush privileges;
-
2.12 添加远程登录用户(即本机访问服务器上的MySQL)
mysql> GRANT ALL PRIVILEGES ON *.* TO 'zhangsan(用户名)'@'%' IDENTIFIED BY 'Zhangsan2018!(密码)' WITH GRANT OPTION; # 或者直接将root权限修改为可以通过远程访问(但不推荐) mysql> use mysql; mysql> UPDATE user SET Host='%' WHERE User='root'; mysql> flush privileges;
-
2.13 设置默认编码为utf-8(mysql安装后默认不支持中文)
vim /etc/my.cnf # 进入文件后添加下面的配置即可 [mysqld] character-set-server=utf8 [client] default-character-set=utf8 [mysql] default-character-set=utf8
-
2.14 重启MySQL服务并进入MySQL
shell> systemctl restart mysqld shell> mysql -uroot -p mysql> show variables like 'character%';
- 出现如下则说明编码修改完成
+--------------------------+----------------------------+ | Variable_name | Value | +--------------------------+----------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | utf8 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | utf8 | | character_set_system | utf8 | | character_sets_dir | /usr/share/mysql/charsets/ | +--------------------------+----------------------------+
- 退出MySQL
mysql> quit
使用本机电脑navicat/sqlyog等一系列客户端工具连接服务器上的mysql,用户名和密码为远程用户的用户名和密码,如果是将root权限修改为可以远程访问,就用root访问。
以上就是全部对mysql的安装与卸载的教程,希望对你有帮助。
可以参考mysql官网文档:https://dev.mysql.com/doc/refman/5.7/en/linux-installation-yum-repo.html