这篇文章以我在学校进修的计算与信息学设计课程项目为基础,简单介绍一下对于linux系统如何通过命令行安装、配置、操作Mysql。一方面记录一下我们课程小组在项目开发中遇到的服务器搭建的问题及解决方法,另一方面总结一下我在MySQL搭载过程中翻阅各种技术社区后找到的有效、可实施的解决方法。
现在网上很多关于MySQL数据库安装教程新旧不一,并且由于centos系统版本不同,所用镜像源不同,很多解决方法已经不适用。这篇文章主要用于组内项目服务器开发过程展示总结,内容均为原创,转载请注明来源,文章中有纰漏之处还望斧正。当然如果能够帮助大家解决一些服务器搭建问题那就再好不过。
云服务器规格
这里我以我所使用的华为云服务器(操作系统:CentOS7.8 64位,镜像类型:ECS系统盘镜像x86)为例。
规格图如下:
这里我所用的centos版本比较高,默认的镜像源经常出现不稳定的情况,所以建议大家尽量选择7.4左右的centos版本。
MySQL安装过程中的常见问题
- MySQL依赖问题
- 默认的rmp源不稳定
下面我先给出MySQL安装的步骤及命令行代码,在遇到以上问题的时候我会给出解决方案。
MySQL安装步骤
依次执行下面三行代码
wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
yum -y install mysql57-community-release-el7-10.noarch.rpm
yum -y install mysql-community-server --nogpgcheck
centos7中默认安装有MariaDB,这个是MySQL的分支,但在安装完MySQL之后可以直接覆盖掉MariaDB。
下面进行mysql的配置,执行以下命令,启动MySQL服务:
systemctl start mysqld
systemctl enable mysqld
查看MySQL运行状态:
systemctl status mysqld.service
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2022-05-17 17:19:25 CST; 1min 1s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 656 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 589 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 837 (mysqld)
CGroup: /system.slice/mysqld.service
└─837 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
May 17 17:19:22 hecs-340553 systemd[1]: Starting MySQL Server...
May 17 17:19:25 hecs-340553 systemd[1]: Started MySQL Server.
执行以下命令,获取安装MySQL时自动设置的root用户密码:
grep 'temporary password' /var/log/mysqld.log
如果回显信息中密码为空,则说明没有自动设置密码,如果有自动设置密码,需要复制在下一步中使用。
执行以下命令,并按照回显提示信息进行操作,加固MySQL:
mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root: #输入上一步骤中获取的安装MySQL时自动设置的root用户密码
The existing password for the user account root has expired. Please set a new password.
New password: #设置新的root用户密码
Re-enter new password: #再次输入密码
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration of the plugin.
Using existing password for root.
Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) : N #是否更改root用户密码,输入N
... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y #是否删除匿名用户,输入Y
Success.
Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y #禁止root远程登录,输入Y
Success.
By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y #是否删除test库和对它的访问权限,输入Y
- Dropping test database...
Success.
- Removing privileges on test database...
Success.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y #是否重新加载授权表,输入Y
Success.
All done!
执行以下命令,再根据提示输入数据库管理员root账号的密码进入数据库:
mysql -u root -p
执行以下命令,使用MySQL数据库
use mysql;
执行以下命令,查看用户列表:
select host,user from user;
执行以下命令,mysql默认不允许远程主机,%表示允许所有主机连接。刷新用户列表并允许所有IP对数据库进行访问,方面后续使用数据库软件进行管理:
update user set host='%' where user='root' LIMIT 1;
执行以下命令,强制刷新权限。允许同一子网中设置为允许访问的云服务器通过私有IP对MySQL数据库进行访问:
flush privileges;
执行以下命令,退出数据库:
quit
执行以下命令,重启MySQL服务
systemctl start mysqld
执行以下命令,设置开机自动启动MySQL服务:
systemctl enable mysqld
执行以下命令,关闭防火墙:
systemctl stop firewalld.service
重新查看防火墙状态是否为关闭:
systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:firewalld(1)
以上就是mysql在linux centos7系统上的安装过程,下面主要介绍一下在安装过程中可能出现的问题以及解决方案,这些问题和解决方案都是我们小组在项目开发过程中遇到的一些实际问题,当然无法包括大家可能遇到的所有问题。
MySQL依赖问题
出现依赖问题或版本冲突建议先将mysql相关文件全部删除,再重新进行mysql安装。
yum remove mysql mysql-server mysql-libs mysql-server
查找残留文件:
rpm -qa | grep -i mysql
将查询出来的文件逐个删除,这里需要用到删除命令,比如:
yum remove mysql-community-common-5.7.29-1.el6.x86_64
查找残留目录,如果有残留文件,再逐一删除(这样能将mysql文件删除干净,方面重新安装):
whereis mysql
rm –rf /usr/lib64/mysql
检测系统是否存在mysql:
yum list installed|grep mysql
删除完毕后重新按照以上流程按照即可。
默认的rmp源不稳定,如何进行rmp源更新
给CentOS添加rpm源,并且选择较新的源:
wget dev.mysql.com/get/mysql-community-release-el6-5.noarch.rpm --no-check-certificate
yum localinstall mysql-community-release-el6-5.noarch.rpm
yum repolist all | grep mysql
yum repolist enabled | grep mysql
查看可获得的mysql版本,进行下载:
yum list | grep mysql
yum -y install mysql-community-server
然后在根据流程进一步操作即可。