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 vi /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
- 禁用MySQL默认的AppStream存储库:
- 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.44
Release : 1.el7
Architecture: x86_64
Install Date: 2023年12月03日 星期日 14时54分27秒
Group : Applications/Databases
Size : 834403500
License : Copyright (c) 2000, 2023, Oracle and/or its affiliates. All rights reserved. Under GPLv2 license as shown in the Description field.
Signature : RSA/SHA256, 2023年10月12日 星期四 08时15分38秒, Key ID 467b942d3a79bd29
Source RPM : mysql-community-5.7.44-1.el7.src.rpm
Build Date : 2023年10月11日 星期三 08时14分29秒
Build Host : pb2-el7-35.regionaliad02.mysql2iad.oraclevcn.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
Description :
The MySQL(TM) software delivers a very fast, multi-threaded, multi-user,
and robust SQL (Structured Query Language) database server. MySQL Server
is intended for mission-critical, heavy-load production systems as well
as for embedding into mass-deployed software. MySQL is a trademark of
Oracle and/or its affiliates
The MySQL software has Dual Licensing, which means you can use the MySQL
software free of charge under the GNU General Public License
(http://www.gnu.org/licenses/). You can also purchase commercial MySQL
licenses from Oracle and/or its affiliates if you do not wish to be bound by the terms of
the GPL. See the chapter "Licensing and Support" in the manual for
further info.
The MySQL web site (http://www.mysql.com/) provides the latest news and
information about the MySQL software. Also please see the documentation
and the manual for more information.
This package includes the MySQL server binary as well as related utilities
to run and administer a MySQL server.
出现以上信息说明安装成功
- 2.4 检查 mysql 源是否安装成功
yum repolist enabled | grep "mysql.*-community.*"
出现以下信息说明安装成功:
mysql-connectors-community MySQL Connectors Community
mysql-tools-community MySQL Tools Community
mysql57-community MySQL 5.7 Community Server
- 2.5 启动MySQL
systemctl start mysqld
- 2.6 查看启动状态
[root@192 ~]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Sun 2023-12-03 14:56:32 EST; 8s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 16231 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 16178 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 16234 (mysqld)
Tasks: 27 (limit: 49300)
Memory: 306.8M
CGroup: /system.slice/mysqld.service
└─16234 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
12月 03 14:56:25 localhost.localdomain systemd[1]: Starting MySQL Server...
12月 03 14:56:32 localhost.localdomain systemd[1]: Started MySQL Server.
出现以上信息,则启动成功
- 2.7 设置开机启动
systemctl enable mysqld
- 2.8 刷新所有修改过的配置文件
systemctl daemon-reload
- 2.9 获取安装mysql后生成的临时密码,用于登录
[root@192 ~]# grep 'temporary password' /var/log/mysqld.log
2023-12-03T19:56:29.623704Z 1 [Note] A temporary password is generated for root@localhost: h/pl*,emh7CA
如果出现如上信息,密码为:h/pl*,emh7CA
- 2.10 登录MySQL
mysql -uroot -p
# 再输入上面查找得到的临时密码: h/pl*,emh7CA,即可进入mysql
- 2.11 修改登录密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Root@123456789(修改后的密码,注意必须包含大小写字母数字以及特殊字符并且长度不能少于8位,否则会报错)';
或者通过:mysql> set password for 'root'@'localhost'=password('Root@123456789');
或者通过:mysql> use mysql;
mysql> update user set password=PASSWORD('Root@123456789') 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安装后默认不支持中文)
vi /etc/my.cnf
# 进入文件后添加下面的配置即可
[mysqld]
character-set-server=utf8
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
- 2.14 重启MySQL服务并进入MySQL
[root@192 ~]# systemctl restart mysqld
[root@192 ~]# 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
3、防火墙放行数据库端口
- 3.1 查看
3306端口有没有被防火墙放行
[root@192 ~]# firewall-cmd --query-port=3306/tcp
no
显示no,表示防火墙未放行。
- 3.2 开放防火墙端口
3306
[root@192 ~]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
success
显示success,表示放行成功。
- 3.3 重启防火墙,使放行生效
[root@192 ~]# systemctl restart firewalld
[root@192 ~]# firewall-cmd --query-port=3306/tcp
yes
出现如上信息,表示防火墙已放行3306端口,这时可以使用本机的navicat访问。
可以看到连接成功。