阿里云centos服务器安装mysql教程

一、前言

自己在阿里云安装mysql过程中,运用网上的yum方法总会出现这样那样的问题,所以结合网上搜索到的资料,写一篇用rpm包进行下载mysql服务的博客

二、卸载系统中的mysql

首先运行:

rpm -qa|grep -i mysql 

会显示安装的mysql各软件包

mysql57-community-release-el7-8.noarch
mysql-community-server-5.7.25-1.el7.x86_64
mysql-community-common-5.7.25-1.el7.x86_64
mysql-community-client-5.7.25-1.el7.x86_64
mysql-community-libs-5.7.25-1.el7.x86_64

再利用 rpm -e --nodeps 命令一个一个删除

rpm -e --nodeps mysql57-community-release-el7-8.noarch
rpm -e --nodeps mysql-community-server-5.7.25-1.el7.x86_64
rpm -e --nodeps mysql-community-common-5.7.25-1.el7.x86_64
rpm -e --nodeps mysql-community-client-5.7.25-1.el7.x86_64
rpm -e --nodeps mysql-community-libs-5.7.25-1.el7.x86_64

删除干净后再用以下命令确认已经删除干净

rpm -qa | grep -i mysql

三、在windows中先下载好linux的mysql安装包

官网5.7版本:https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.29-1.el7.x86_64.rpm-bundle.tar

官网下载很慢,也可以选择其他版本的只要是rpm-bundle.tar的包就好。

下载所需的tar包

img

img

之后选择下面的需要下载的版本,因为要直接解压安装,所以我下载的是第一个

大家可以根据自己的版本选择下载

点击下载

img

用winscp或其他软件将安装包传到阿里云服务器上(一般服务器解压的地址在/usr/local/下,可以新建mysql文件夹,将压缩包放入,然后解压)

cd /usr/local/

mkdir mysql

tar -xvf xxx

使用tar命令解压

tar -xvf mysql-5.7.29-1.el7.x86_64.rpm-bundle.tar

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-O7aeyJlI-1603611057880)(C:\Users\ASUS\AppData\Roaming\Typora\typora-user-images\image-20201025151804465.png)]

安装新版mysql前,需将系统自带的mariadb-lib卸载

rpm -qa|grep mariadb

mariadb-libs-5.5.60-1.el7_5.x86_64

rpm -e --nodeps mariadb-libs-5.5.60-1.el7_5.x86_64

为了避免出现权限问题,给mysql解压文件所在目录赋予最大权限

chmod -R 777  ./mysql

严格按照顺序安装:

mysql-community-common-5.7.29-1.el7.x86_64.rpm

mysql-community-libs-5.7.29-1.el7.x86_64.rpm

mysql-community-client-5.7.29-1.el7.x86_64.rpm

mysql-community-server-5.7.29-1.el7.x86_64.rpm这四个包

其实你不按这个顺序安装也可以,只是你在安装这个包时系统会提醒你首先需要安装这个包的依赖包(另一个rpm包,依赖包就在这几个rpm中),就这样递归的找下去,也可以安装好。

rpm -ivh mysql-community-common-5.7.29-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-5.7.29-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-5.7.29-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-5.7.29-1.el7.x86_64.rpm

如果安装过程中出现这个错误就在后面添加 --force --nodeps,这可能是由于yum安装了旧版本的GPG keys造成的

img

四、配置数据库

vim /etc/my.cnf

添加这三行

skip-grant-tables
character_set_server=utf8
init_connect='SET NAMES utf8'

img

skip-grant-tables:跳过登录验证

character_set_server=utf8:设置默认字符集UTF-8

init_connect=‘SET NAMES utf8’:设置默认字符集UTF-8

启动MySQL服务

设置开机启动

systemctl start mysqld.service

启动mysql

mysql

进入mysql的命令行界面

先设置一个简单的密码

update mysql.user set authentication_string=password('123456') where user='root';

img

立即生效

flush privileges;

退出mysql并停止mysql服务

systemctl stop  mysqld.service

编辑my.cnf配置文件将:skip-grant-tables这一行注释掉

重启mysql服务

systemctl start mysqld.service

再次登录mysql

mysql -uroot -p123456

如果输入其他命令出错,再重设密码

set password=password('123456');
开启远程登陆
grant all privileges on *.* to 'root'@'%' identified by '123123' with grant option;

img

by后面的就是远程登录密码,远程登录密码可以和用户密码不一样

开放3306端口
firewall-cmd --zone=public --add-port=3306/tcp --permanent

–zone #作用域
–add-port=80/tcp #添加端口,格式为:端口/通讯协议
–permanent #永久生效,没有此参数重启后失效

重启防火墙

firewall-cmd --reload

如果这一步执行完毕后,在你的电脑上依然无法连接到阿里云的mysql服务器,你可以试一试:

1.mysql的服务是否处于运行状态

systemctl status mysql

2.关闭防火墙

五、阿里云服务器配置安全组

a、打开阿里云后台

img

b、点击配置规则配置

img

c、配置具体规则

img

重启服务器

然后测试

img

六、一些centos下mysql基本操作命令

1、登陆mysql

本地:mysql -u root -p, 回车后输入密码; 也可以p后不加空格,直接加密码。回车就登录了
远程:mysql -h ip -u root -p -P 3306例如:mysql -h 127.0.0.1 -u root -p -P 3306

附:连接mysql出现错误:Can’t connect to local MySQL server through socket '/var/lib/mysql/mysql.soc

1、先通过:service mysqld status 查看mysql是否启动

(没装mysql service 也可通过 /etc/rc.d/init.d/mysqld status查看)

若没启动,则启动mysql:service mysqld start

2、方法: 修改/etc/my.conf:
[mysqld]
datadir=/usr/local/mysql/data

socket=/var/lib/mysql/mysql.sock

[mysql.server]
user=mysql
basedir=/usr/local/mysql

If there is not currently a section called [client], add one at the bottom of the file and copy the socket= line under the [mysqld] section such as:

[client]
socket=/var/lib/mysql/mysql.sock
(注意:红色标记部分文件路径必须相同)

发现依旧如此,运行/etc/init.d/mysql start报错:Starting MySQLCouldn’t find MySQL manager or server
e is not currently a section called [client], add one at the bottom of the file and copy the socket= line under the [mysqld] section such as:

[client]
socket=/var/lib/mysql/mysql.sock
(注意:红色标记部分文件路径必须相同)

发现依旧如此,运行/etc/init.d/mysql start报错:Starting MySQLCouldn’t find MySQL manager or server
是mysqld服务没启,运行/usr/local/mysql/bin/mysqld_safe &

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值