Mysql数据库的安装对于开发者来说,是我们必然会面对的问题,它的安装过程其实并不复杂,并且网络上的安装教程也非常多,但是对于新手来说,各种不同形式的安装教程,又给新手们带来了要选择哪种方式进行安装的难题,而且很多时候按照教程也没有能够安装成功,安装过程出现各种各样的错误。
下面记录了我在Linux(Centos 7)环境下安装Mysql的完整过程,实操记录,绝非水文,如有错误或遗漏,欢迎指正。
一、Mysql安装
1.下载 MySQL 所需要的安装包
网址:https://dev.mysql.com/downloads/mysql/
2.Select Operating System: 选择 Red Hat ,CentOS 是基于红帽的,Select OS Version: 选择 linux 7
3.选择 RPM Bundle 点击 Download
4.点击 No thanks, just start my download. 进行下载
5.下载好后,使用xshell连接到我们的虚拟机。
6.通过 rpm -qa | grep mariadb 命令查看 mariadb 的安装包
7.通过 rpm -e mariadb-libs-5.5.60-1.el7_5.x86_64 —nodeps 命令装卸 mariadb
注意:mariadb-libs-5.5.60-1.el7_5.x86_64名称根据自己实际修改
8.通过 rpm -qa | grep mariadb 命令再次查看 mariadb 的安装包
9.通过 cd /usr/local/ 命令进入根目录下的usr目录下的local目录,这个目录是放一些本地的共享资源的
10.通过 ll 命令查看一下当前目录下的目录结构
11.通过 mkdir mysql 命令 在当前目录下创建一个名为 mysql 的目录
12.通过 ll 命令查看一下当前目录下的目录结构,刚创建的 mysql 目录有了
13.通过 cd mysql 命令进入 mysql 目录
14.通过 ll 命令查看一下当前目录下的目录结构
15.通过xftp把刚才下载好的mysql安装包上传到mysql目录
16.通过 ll 命令查看一下当前目录下的目录结构
17.通过 tar -xvf mysql-8.0.23-1.el7.x86_64.rpm-bundle.tar 命令解压 tar 包
18.通过rpm -ivh mysql-community-common-8.0.23-1.el7.x86_64.rpm —nodeps —force 命令安装 common
19.通过 rpm -ivh mysql-community-libs-8.0.23-1.el7.x86_64.rpm —nodeps —force 命令安装 libs
20.通过 rpm -ivh mysql-community-client-8.0.23-1.el7.x86_64.rpm —nodeps —force 命令安装 client
21.通过 rpm -ivh mysql-community-server-8.0.23-1.el7.x86_64.rpm —nodeps —force 命令安装 server
22.通过 rpm -qa | grep mysql 命令查看 mysql 的安装包
23.通过以下命令,完成对 mysql 数据库的初始化和相关配置
这儿特别注意:我们知道在 Linux 环境下默认是区分大小写的,如要改变这种默认方式,需要在初始化前修改 lower_case_table_names,mysql8.0 要求我们不能在initialize之后再更改 lower_case_table_names 的值,也就是说,再通过更改 my.cnf 文件是不管用的。
初始化前,linux 下修改文件 my.cnf ,在 [mysqld] 配置节点下添加 lower-case-table-names=1,然后再执行如下命令。
<span class="pln">mysqld </span><span class="pun">--</span><span class="pln">initialize</span><span class="pun">;</span>
<span class="pln">chown mysql</span><span class="pun">:</span><span class="pln">mysql </span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">lib</span><span class="pun">/</span><span class="pln">mysql </span><span class="pun">-</span><span class="pln">R</span><span class="pun">;</span>
<span class="pln">systemctl start mysqld</span><span class="pun">.</span><span class="pln">service</span><span class="pun">;</span>
<span class="pln">systemctl enable mysqld</span><span class="pun">;</span>
查看MySQL官方文档:
- 5.7 官方文档:Identifier Case Sensitivity:https://dev.mysql.com/doc/refman/5.7/en/identifier-case-sensitivity.html
- 8.0 官方文档:Identifier Case Sensitivity:https://dev.mysql.com/doc/refman/8.0/en/identifier-case-sensitivity.html
MySQL 8.0 Release Notes - Changes in MySQL 8.0.17 (2019-07-22, General Availability) - Functionality Added or Changed有记录:
<span class="typ">In</span> <span class="typ">MySQL</span> <span class="lit">8.0</span><span class="pun">,</span><span class="pln"> the lower_case_table_names variable can only be configured </span><span class="kwd">when</span><span class="pln"> the </span><span class="typ">MySQL</span><span class="pln"> server </span><span class="kwd">is</span><span class="pln"> initializ</span>
24.通过 cat /var/log/mysqld.log | grep password 命令查看数据库的密码
25.通过 mysql -uroot -p 敲回车键进入数据库登陆界面
26.输入刚刚查到的密码,进行数据库的登陆,复制粘贴就行,MySQL 的登陆密码也是不显示的
27.通过 ALTER USER ‘root’@’localhost’ IDENTIFIED WITH mysql_native_password BY ‘123456’; 命令来修改密码
28.通过 exit; 命令退出 MySQL,然后通过新密码再次登陆
29.通过以下命令,进行远程访问的授权
<span class="pln">create user </span><span class="str">'root'</span><span class="pun">@</span><span class="str">'%'</span><span class="pln"> identified </span><span class="kwd">with</span><span class="pln"> mysql_native_password </span><span class="kwd">by</span> <span class="str">'123456'</span><span class="pun">;</span>
<span class="pln">grant all privileges on </span><span class="pun">*.*</span><span class="pln"> to </span><span class="str">'root'</span><span class="pun">@</span><span class="str">'%'</span> <span class="kwd">with</span><span class="pln"> grant option</span><span class="pun">;</span>
<span class="pln">flush privileges</span><span class="pun">;</span>
30.通过 ALTER USER ‘root’@’localhost’ IDENTIFIED BY ‘123456’ PASSWORD EXPIRE NEVER; 命令修改加密规则,MySql8.0 版本 和 5.0 的加密规则不一样,而现在的可视化工具只支持旧的加密方式。
31.通过 flush privileges; 命令刷新修该后的权限
32.通过 exit; 命令退出 MySQL。
33.通过以下命令,关闭 firewall
<span class="pln">systemctl stop firewalld</span><span class="pun">.</span><span class="pln">service</span><span class="pun">;</span>
<span class="pln">systemctl disable firewalld</span><span class="pun">.</span><span class="pln">service</span><span class="pun">;</span>
<span class="pln">systemctl mask firewalld</span><span class="pun">.</span><span class="pln">service</span><span class="pun">;</span>
34.通过 yum -y install iptables-services 命令安装 iptables 防火墙
35.通过以下命令启动设置防火墙
<span class="pln">systemctl enable iptables</span><span class="pun">;</span>
<span class="pln">systemctl start iptables</span><span class="pun">;</span>
36.通过 vim /etc/sysconfig/iptables 命令编辑防火墙,添加端口
37.点击 i 键进入插入模式
38.在相关位置,写入以下内容
-A INPUT -m state —state NEW -m tcp -p tcp —dport 80 -j ACCEPT
-A INPUT -m state —state NEW -m tcp -p tcp —dport 3306 -j ACCEPT
-A INPUT -m state —state NEW -m tcp -p tcp —dport 443 -j ACCEPT
-A INPUT -m state —state NEW -m tcp -p tcp —dport 8080 -j ACCEPT
-A INPUT -m state —state NEW -m tcp -p tcp —dport 8090 -j ACCEPT
-A INPUT -m state —state NEW -m tcp -p tcp —dport 5888 -j ACCEPT
点击 ESC 键退出插入模式,点击 : 键,输入 wq 敲回车键保存退出,: 为英文状态下的
39.通过 systemctl restart iptables.service 命令重启防火墙使配置生效
40.通过 systemctl enable iptables.service 命令设置防火墙开机启动
41.通过 ifconfig 命令查看 ip,得到ip后我们就可以通过mysql工具连接到我们安装好的mysql了。
二、Mysql卸载
1、关闭mysql
<span class="pln">service mysqld stop</span>
2、查看安装的mysql
<span class="pln">rpm </span><span class="pun">-</span><span class="pln">qa</span><span class="pun">|</span><span class="pln">grep </span><span class="pun">-</span><span class="pln">i mysql</span>
3、卸载安装的mysql
<span class="pln">rpm </span><span class="pun">-</span><span class="pln">ev mysql</span><span class="pun">-</span><span class="pln">community</span><span class="pun">-</span><span class="pln">client</span><span class="pun">-</span><span class="lit">8.0</span><span class="pun">.</span><span class="lit">11</span><span class="pun">-</span><span class="lit">1.el7.x86</span><span class="pln">_64 </span><span class="pun">--</span><span class="pln">nodeps</span>
<span class="pln">rpm </span><span class="pun">-</span><span class="pln">ev mysql</span><span class="pun">-</span><span class="pln">community</span><span class="pun">-</span><span class="pln">common</span><span class="pun">-</span><span class="lit">8.0</span><span class="pun">.</span><span class="lit">11</span><span class="pun">-</span><span class="lit">1.el7.x86</span><span class="pln">_64 </span><span class="pun">--</span><span class="pln">nodeps</span>
<span class="pln">rpm </span><span class="pun">-</span><span class="pln">ev mysql</span><span class="pun">-</span><span class="pln">community</span><span class="pun">-</span><span class="pln">server</span><span class="pun">-</span><span class="lit">8.0</span><span class="pun">.</span><span class="lit">11</span><span class="pun">-</span><span class="lit">1.el7.x86</span><span class="pln">_64 </span><span class="pun">--</span><span class="pln">nodeps</span>
<span class="pln">rpm </span><span class="pun">-</span><span class="pln">ev mysql</span><span class="pun">-</span><span class="pln">community</span><span class="pun">-</span><span class="pln">libs</span><span class="pun">-</span><span class="lit">8.0</span><span class="pun">.</span><span class="lit">11</span><span class="pun">-</span><span class="lit">1.el7.x86</span><span class="pln">_64 </span><span class="pun">--</span><span class="pln">nodeps</span>
4、删除mysql相关目录
<span class="pln">find </span><span class="pun">/</span> <span class="pun">-</span><span class="pln">name mysql</span>
<span class="pun">[</span><span class="pln">root@niceyoohw conf</span><span class="pun">]#</span><span class="pln"> rm </span><span class="pun">-</span><span class="pln">rf </span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">lib</span><span class="pun">/</span><span class="pln">mysql</span><span class="pun">/</span>
<span class="pun">[</span><span class="pln">root@niceyoohw conf</span><span class="pun">]#</span><span class="pln"> rm </span><span class="pun">-</span><span class="pln">rf </span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="pln">lib64</span><span class="pun">/</span><span class="pln">mysql</span>
<span class="pun">[</span><span class="pln">root@niceyoohw conf</span><span class="pun">]#</span><span class="pln"> rm </span><span class="pun">-</span><span class="pln">rf </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">selinux</span><span class="pun">/</span><span class="pln">targeted</span><span class="pun">/</span><span class="pln">active</span><span class="pun">/</span><span class="pln">modules</span><span class="pun">/</span><span class="lit">100</span><span class="pun">/</span><span class="pln">mysql</span>
<span class="pun">[</span><span class="pln">root@niceyoohw conf</span><span class="pun">]#</span><span class="pln"> rm </span><span class="pun">-</span><span class="pln">rf </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">selinux</span><span class="pun">/</span><span class="pln">targeted</span><span class="pun">/</span><span class="pln">tmp</span><span class="pun">/</span><span class="pln">modules</span><span class="pun">/</span><span class="lit">100</span><span class="pun">/</span><span class="pln">mysql</span>
5、删除my.cnf
<span class="pln">rm </span><span class="pun">-</span><span class="pln">rf </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="kwd">my</span><span class="pun">.</span><span class="pln">cnf</span>
6、检查卸载情况
<span class="pln">rpm </span><span class="pun">-</span><span class="pln">qa</span><span class="pun">|</span><span class="pln">grep </span><span class="pun">-</span><span class="pln">i mysql</span>
显示为空,卸载完毕。
作者|国思软件
文章评论