飞鱼

恭喜你发现了一个菜鸡博主。

如何安装或升级到最新稳定版的MySQL/MariaDB

飞鱼 2020年03月04日 05:51:28

MySQL/MariaDB 是我们最常用的数据库之一。默认安装到的版本往往太旧,甚至找不到安装源。这里做个填坑笔记。

使用一键安装包固然方便,但有时我们只需要数据库,而并不需要其它组件。

安装MySQL

先通过 SSH 连接到服务器

本地访问链接:MySQL Community Downloads

我的服务器操作系统是 Debian,因此我选择 apt 的安装源

进去之后点 “Download”,然后找到 “No thanks, just start my download.” 右击,将链接复制下来。

服务器上运行 shell 命令

cd ~

# wget 后面粘贴你刚刚复制的链接
wget https://dev.mysql.com/get/mysql-apt-config_0.8.15-1_all.deb

此时 deb 安装包已经保存到当前目录了

# 查看包的名称
ls

# 添加安装源    后面同样粘贴你所保存到的包名称
apt install ./mysql-apt-config_0.8.15-1_all.deb

然后根据提示设置好安装版本,选 OK 即可。


# 更新安装源
apt update

# 安装 MySQL
apt install mysql-server

设置 MySQL 的 root 密码,完成安装


登录 MySQL 进行管理

mysql -u root -p
# 输入 MySQL 的 root 密码

修改 root 密码为 newpassword (密码一定要设置复杂一点)

use mysql;
update user set password=password('newpassword') where user = 'root';
flush privileges;

开启 MySQL 远程登录

先查看账户列表

use mysql;
select user,host from user;

修改允许登录到 MySQL 的主机

update user set host='%' where user='root' and host='localhost';
flush privileges;
quit;

修改 MySQL 配置文件

nano /etc/mysql/mysql.conf.d/mysqld.cnf

找到并注释掉这一行

#bind-address = (服务器本地ip地址)

重启 MySQL

/etc/init.d/mysql restart

如果你的服务器配置了防火墙记得开放 3306 端口

# 查看端口配置
iptables -L -n --line-numbers

看看 3306 端口有没有被禁止

# 删除对应的设置   x 为序号
iptables -D INPUT x

安装 MariaDB

访问链接:Setting up MariaDB Repositories

依次选择操作系统,版本,及安装镜像,下方即可生成对应的安装命令

我的系统版本是 Debian 10

依次运行如下命令

apt-get install software-properties-common dirmngr

apt-key adv --fetch-keys 'https://mariadb.org/mariadb_release_signing_key.asc'

add-apt-repository 'deb [arch=amd64] http://mirrors.tuna.tsinghua.edu.cn/mariadb/repo/10.4/debian buster main'

如果在运行第 3 条命令时出现错误,则需要手动添加安装源

nano /etc/apt/sources.list

在最下方添加安装源

# MariaDB 10.4 repository list - created 2020-03-05 09:59 UTC
# http://downloads.mariadb.org/mariadb/repositories/
deb [arch=amd64] http://mirrors.tuna.tsinghua.edu.cn/mariadb/repo/10.4/debian buster main
deb-src http://mirrors.tuna.tsinghua.edu.cn/mariadb/repo/10.4/debian buster main

开始安装 MariaDB

apt update
apt install mariadb-server

MariaDB 远程登录的开启方法,与 MySQL 基本相同。区别就是,不同的数据库配置文件是分开存放的。

请注意:MariaDB 默认是没有密码的,请务必先设置一个比较复杂的登录密码。

ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
flush privileges;
quit;

我们先找找看,哪些配置文件里面包含 “skip-networking”

cd /etc/mysql
grep -rn "skip-networking" *

然后使用 nano 打开含有有该项的文件,找到 “bind-address” 并注释掉。

接下来,操作方法和 MySQL 一样,修改 mysql 数据库中 user 表的 host 为 “%” ,以允许远程主机登录。

修改完记得重新启动服务


© 2020 飞鱼的博客