MySQL 在 Centos 7环境安装
说明:
• 安装与卸载中,用户全部切换成为root,⼀旦安装,普通用户能使用的
• 初期练习,mysql不进行用户管理,全部使用root进行,尽快适应mysql语句,后⾯学习用户管理后再考虑新建普通用户
1. 卸载不要的环境
[root@iZ0jl69kyvg0h181cozuf5Z ~]# ps ajx |grep mariadb # 先检查是否有mariadb存在
13529 13551 13550 13529 pts/0 13550 S+ 0 0:00 grep --color=auto mariadb
这里博主是新建的云服务器,所以这些东西都没有(如果有的话,如下列代码所示:)
[whb@VM-0-3-centos ~]$ ps ajx |grep mariadb # 先检查是否有mariadb存在
13134 14844 14843 13134 pts/0 14843 S+ 1005 0:00 grep --color=auto mariadb
19010 19187 19010 19010 ? -1 Sl 27 16:55 /usr/libexec/mysqld --
basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin
--log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid
--socket=/var/lib/mysql/mysql.sock
如果存在mariadb 要先将mariadb服务先停止
[whb@VM-0-3-centos ~]$ systemctl stop mariadb.service # 停⽌mariadb 服务
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to manage system services or units.
Authenticating as: root
Password:
==== AUTHENTICATION COMPLETE ===
停止完服务如下所式:
[whb@VM-0-3-centos ~]$ ps axj |grep mariadb # 停⽌完成
13134 14976 14975 13134 pts/0 14975 S+ 1005 0:00 grep --color=auto mariadb
2. 检查系统安装包
[whb@VM-0-3-centos ~]$ rpm -qa | grep mariadb
or
[whb@VM-0-3-centos ~]$ rpm -qa | grep mysql
mysql-community-common-5.7.41-1.el7.x86_64
mysql-community-server-5.7.41-1.el7.x86_64
mysql57-community-release-el7-11.noarch
mysql-community-client-5.7.41-1.el7.x86_64
mysql-community-libs-5.7.41-1.el7.x86_64
3. 卸载这些默认安装包
#卸载显⽰出来的mariadb/mysql安装包
[whb@VM-0-3-centos ~]$ sudo yum remove mariadb # 还是试着将上⾯的包都⼀个⼀个卸载吧
# 备份/etc/my.cnf,备份/var/lib/mysql数据(如果不需要再使用之前MySQL当中的数据可以不用进行备份)
4. 获取mysql官方yum源
获取mysql官⽅yum源 点击该链接获取yum源!
⼀定要看下⾯的⽂字!如果后⾯⾃⼰安装不成功,就看看这个
注意:最好安装和自己系统⼀致的mysql版本,否则可能会存在软件兼容性问题
# 查看⾃⼰的系统版本
[root@iZ0jl69kyvg0h181cozuf5Z ~]# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)
可以看到这里博主的系统是CentOS 7.9 版本的
如果是7.1 / 7.2 … 没找到后缀相同的版本,选择下载el7.rpm即可
下载完成后放到桌面上
执行rz命令,将安装包导入到Linux系统
如果出现如下报错:
请下载lrzsz安装包
执行rz命令找到自己下载的安装包
到这里rpm的安装包就安装完成啦!
5. 安装mysql yum 源,对比前后yum源
查看你本地的yum源 [yum源各不相同,我们可以发现没有mysql相关的yum源]
[root@iZ0jl69kyvg0h181cozuf5Z ~]# ls /etc/yum.repos.d/ -l
total 8
-rw-r--r-- 1 root root 675 Mar 10 13:16 CentOS-Base.repo
-rw-r--r-- 1 root root 230 Mar 10 13:16 epel.repo
安装mysql的yum源
[root@iZ0jl69kyvg0h181cozuf5Z ~]# rpm -ivh mysql57-community-release-el7-9.noarch.rpm
warning: mysql57-community-release-el7-9.noarch.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ################################# [100%]
Updating / installing...1:mysql57-community-release-el7-9 ################################# [100%]
[root@iZ0jl69kyvg0h181cozuf5Z ~]# ls /etc/yum.repos.d/ -l
total 16
-rw-r--r-- 1 root root 675 Mar 10 13:16 CentOS-Base.repo
-rw-r--r-- 1 root root 230 Mar 10 13:16 epel.repo
-rw-r--r-- 1 root root 1416 Sep 12 2016 mysql-community.repo
-rw-r--r-- 1 root root 1440 Sep 12 2016 mysql-community-source.repo
前后进行对比发现MySQL 的 yum 源安装上了
# 安装完成后,这个rpm的安装包就没有作用了 可以进行删除命令
[root@iZ0jl69kyvg0h181cozuf5Z ~]# ll
total 12
-rw-r--r-- 1 root root 9224 Mar 11 08:20 mysql57-community-release-el7-9.noarch.rpm
[root@iZ0jl69kyvg0h181cozuf5Z ~]# rm mysql57-community-release-el7-9.noarch.rpm
rm: remove regular file ‘mysql57-community-release-el7-9.noarch.rpm’? y
6. 看看能不能正常工作
[root@iZ0jl69kyvg0h181cozuf5Z ~]# yum list |grep mysql
mysql57-community-release.noarch el7-9 installed
akonadi-mysql.x86_64 1.9.2-4.el7 base
anope-mysql.x86_64 2.1.2-1.el7 epel
apr-util-mysql.x86_64 1.5.2-6.el7_9.1 updates
calligra-kexi-driver-mysql.x86_64 2.9.10-2.el7 epel
collectd-mysql.x86_64 5.8.1-1.el7 epel
dmlite-plugins-mysql.x86_64 1.15.2-15.el7 epel
dovecot-mysql.x86_64 1:2.2.36-8.el7 base
dpm-copy-server-mysql.x86_64 1.13.0-1.el7 epel
dpm-name-server-mysql.x86_64 1.13.0-1.el7 epel
dpm-server-mysql.x86_64 1.13.0-1.el7 epel
dpm-srm-server-mysql.x86_64 1.13.0-1.el7 epel
dspam-mysql.x86_64 3.10.2-12.el7 epel
exim-mysql.x86_64 4.97.1-1.el7 epel
freeradius-mysql.x86_64 3.0.13-15.el7 base
fts-mysql.x86_64 3.12.0-1.el7 epel
gnokii-smsd-mysql.x86_64 0.6.31-17.el7 epel
holland-mysql.noarch 1.1.22-1.el7 epel
holland-mysqldump.noarch 1.1.22-1.el7 epel
7. 安装mysql服务
[root@iZ0jl69kyvg0h181cozuf5Z ~]# yum install -y mysql-community-server
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package mysql-community-server.x86_64 0:5.7.44-1.el7 will be installed
--> Processing Dependency: mysql-community-common(x86-64) = 5.7.44-1.el7 for package: mysql-community-server-5.7.44-1.el7.x86_64
--> Processing Dependency: mysql-community-client(x86-64) >= 5.7.9 for package: mysql-community-server-5.7.44-1.el7.x86_64
...
Installing : mysql-community-common-5.7.33-1.el7.x86_64 #公共模块
Installing : mysql-community-libs-5.7.33-1.el7.x86_64 #库
Installing : mysql-community-client-5.7.33-1.el7.x86_64 #客⼾端
Installing : mysql-community-server-5.7.33-1.el7.x86_64 #服务器
... #安装了服务和客⼾端
安装遇到秘钥过期的问题:
Failing package is: mysql-community-client-5.7.39-1.el7.x86_64 GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
解决⽅案:
# 执行该命令再重新进行安装 [root@iZ0jl69kyvg0h181cozuf5Z ~]# rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
8. 查看配置文件和数据存储位置
[root@iZ0jl69kyvg0h181cozuf5Z ~]# ls /etc/my.cnf
/etc/my.cnf
[root@iZ0jl69kyvg0h181cozuf5Z ~]# which mysqld
/usr/sbin/mysqld
[root@iZ0jl69kyvg0h181cozuf5Z ~]# which mysql
/usr/bin/mysql
9. 启动服务并查看
[root@iZ0jl69kyvg0h181cozuf5Z ~]# systemctl start mysqld
[root@iZ0jl69kyvg0h181cozuf5Z ~]# ps axj | grep mysqld1 13984 13983 13983 ? -1 Sl 27 0:00 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
13529 14013 14012 13529 pts/0 14012 S+ 0 0:00 grep --color=auto mysqld
我们可以看到现在就有了mysqld的进程
10. 登陆MySQL账号
因为登录需要密码,而刚开始是由电脑自动生成的随机密码(也不好找) 所以我们第一次登录选择跳过验证!
[root@iZ0jl69kyvg0h181cozuf5Z ~]# vim /etc/my.cnf
在[mysqld]最后⼀栏配置(不知道是什么,就放在配置⽂件最后) 加⼊: skip-grant-tables 选项,
并保存退出
不会进行文档保存可以看看博主之前关于Linux系统的命令博客
保存完之后重启mysql服务
[root@iZ0jl69kyvg0h181cozuf5Z ~]# systemctl restart mysqld # 重启mysql服务
# 登录成功如下所示:
[root@iZ0jl69kyvg0h181cozuf5Z ~]# 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.44 MySQL Community Server (GPL)Copyright (c) 2000, 2023, 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>
那么接下来就可以执行mysql命令了
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
11. 设置开机启动[可以不设]
[root@iZ0jl69kyvg0h181cozuf5Z ~]# systemctl enable mysqld
[root@iZ0jl69kyvg0h181cozuf5Z ~]# systemctl daemon-reload
因为云服务器一般都是保持启动状态的,所以设备设置都无所谓╮(╯_╰)╭
12. 配置my.cnf
[root@iZ0jl69kyvg0h181cozuf5Z ~]# vim /etc/my.cnf
# ....
# 在/etc/my.cnf 文件当中添加以下三行即可!
port=3306
character-set-server=utf8
default-storage-engine=innodb
当然,完整的配置文件(/etc/my.cnf)放在下面啦!大家 Ctrl +C/V 即可!
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
port=3306
character-set-server=utf8
default-storage-engine=innodbdatadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
skip-grant-tables
因为之前我们启动了mysql服务,修改配置文件后要进行重新启动
[root@iZ0jl69kyvg0h181cozuf5Z ~]# ps axj | grep mysql1 14057 14056 14056 ? -1 Sl 27 0:00 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
13529 14209 14208 13529 pts/0 14208 S+ 0 0:00 grep --color=auto mysql
[root@iZ0jl69kyvg0h181cozuf5Z ~]# systemctl restart mysqld
那么到这里,整个MySQL安装的流程就结束啦!!! 可以进行MySQL知识的学习了。
感觉有帮助的朋友的兄弟给个三连支持一下,跪谢!!!