Ubuntu 安装 MySQL

Ubuntu 下安装 MySQL 服务,推荐使用 apt install 官方的 APT 仓库安装方式,这种方式最安全、最稳定、能自动处理依赖关系,也支持后续升级。不推荐在官网手动下载 .deb 包的方式。

配置 Ubuntu 服务器

1. 确认 Ubuntu 系统版本

使用如下命令:

lsb_release -a
或者
cat /etc/os-release
或者
hostnamectl

显示如下:

No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 24.04.2 LTS
Release:        24.04
Codename:       noble

2. 更新国内镜像源

不然 sudo apt update 可能会失败。

备份原来的镜像文件:

sudo cp /etc/apt/sources.list.d/ubuntu.sources /etc/apt/sources.list.d/ubuntu.sources.backup

编辑 /etc/apt/sources.list.d/ubuntu.sources 文件

vim /etc/apt/sources.list.d/ubuntu.sources

原内容:

Types: deb
URIs: http://cn.archive.ubuntu.com/ubuntu/
Suites: noble noble-updates noble-backports
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

Types: deb
URIs: http://security.ubuntu.com/ubuntu/
Suites: noble-security
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

替换镜像源后内容:

阿里云

Types: deb
URIs: http://mirrors.aliyun.com/ubuntu/
Suites: noble noble-updates noble-backports
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

Types: deb
URIs: http://mirrors.aliyun.com/ubuntu/
Suites: noble-security
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

腾讯云

Types: deb
#URIs: http://archive.ubuntu.com/ubuntu/
URIs: http://mirrors.tencentyun.com/ubuntu/
Suites: noble noble-updates noble-backports
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

#Types: deb
#URIs: http://security.ubuntu.com/ubuntu/
#Suites: noble-security
#Components: main restricted universe multiverse
#Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

3. 更新软件包

# 更新本地的“软件包索引缓存”,获取镜像源地址中最新的软件版本
sudo apt update

# 更新升级 apt update 得到的新版本,更新安装这些新版本到我们的服务器上
sudo apt upgrade -y

手动添加官方 MySQL APT 源

在 Ubuntu 中 如果想安装 mysql 可以使用 sudo apt install mysql-server 默认会安装 APT 源中提供的最新版本,如果APT源中不存在 mysql 官网中最新的版本,那就需要我们手动添加官方的 mysql apt 源。

查询本地可以用的MySQL版本

apt-cache madison mysql-server

显示内容如下(没有最新版本):

mysql-server | 8.0.41-0ubuntu0.24.04.1 | http://mirrors.aliyun.com/ubuntu noble-updates/main amd64 Packages
mysql-server | 8.0.41-0ubuntu0.24.04.1 | http://mirrors.aliyun.com/ubuntu noble-security/main amd64 Packages
mysql-server | 8.0.36-2ubuntu3 | http://mirrors.aliyun.com/ubuntu noble/main amd64 Packages

开始手动安装镜像源

官网地址:https://dev.mysql.com/doc/refman/8.4/en/linux-installation-apt-repo.html

1. 下载最新的 MySQL APT 配置包:

wget https://dev.mysql.com/get/mysql-apt-config_0.8.34-1_all.deb

命令中的 mysql-apt-config_0.8.34-1_all.deb 最新的文件版本号在 https://dev.mysql.com/downloads/repo/apt/ 网址中查看。

2. 安装 MySQL APT 配置包

sudo dpkg -i mysql-apt-config_0.8.34-1_all.deb

显示如上页面,因为我要安装的就是 8.4 的版本,这里直接 敲回车,显示如下页面

显示如上页面,如果你没有特定要使用的版本,就直接使用它默认选中的,默认选中的是最适合你当前服务器的版本,按回车确认,然后回跳回第一个页面,如下图:

显示如上页面,按上下键把光标移动到 OK,按回车保存退出。

3. 更新 APT 缓存

sudo apt update

4. 重新查询本地可以用的MySQL版本

apt-cache madison mysql-server

显示内容如下(可以发现和之前上面查询的显示不一样了,多出来了 8.4.5-1ubuntu24.04 的版本):

mysql-server | 8.4.5-1ubuntu24.04 | http://repo.mysql.com/apt/ubuntu noble/mysql-8.4-lts amd64 Packages
mysql-server | 8.0.41-0ubuntu0.24.04.1 | http://mirrors.aliyun.com/ubuntu noble-updates/main amd64 Packages
mysql-server | 8.0.41-0ubuntu0.24.04.1 | http://mirrors.aliyun.com/ubuntu noble-security/main amd64 Packages
mysql-server | 8.0.36-2ubuntu3 | http://mirrors.aliyun.com/ubuntu noble/main amd64 Packages
mysql-community | 8.4.5-1ubuntu24.04 | http://repo.mysql.com/apt/ubuntu noble/mysql-8.4-lts Sources

安装 MySQL Server

安装指定版本的 MySQL Server 服务器

sudo apt install -y mysql-server=8.4.5-1ubuntu24.04

显示如上页面,直接输入你的密码,按回车确认密码。

安装完成。


卸载 MySQL

1. 先查询mysql所有已安装的包

dpkg -l | grep mysql

2. 使用 sudo apt-get remove 命令,卸载掉上面查询到的所有安装的包

sudo apt-get remove --purge -y mysql-apt-config mysql-server mysql-client mysql-common mysql-community-server mysql-community-client mysql-community-client-core mysql-community-server-core mysql-community-client-plugins

3. 删除配置文件和数据文件

sudo rm -rf /etc/mysql /var/lib/mysql /var/log/mysql /var/log/mysql.*

4. 清理无用依赖和缓存

sudo apt-get -y autoremove
sudo apt-get -y autoclean

5. 查询是否还有mysql的包

dpkg -l | grep mysql

MySQL 相关命令

查看 MySQL 服务状态:sudo systemctl status mysql

停止 MySQL 服务:sudo systemctl stop mysql

启动 MySQL 服务:sudo systemctl start mysql

重启 MySQL 服务:sudo systemctl restart mysql

查看 MySQL 的开机自启状态:systemctl is-enabled mysql  ,enable:启用,disable:禁用

启动 MySQL 开机自启动:sudo systemctl enable mysql

禁用 MySQL 开机自启动:sudo systemctl disable mysql


登录 MySQL

sudo mysql -u root -p
# 登录指定的 ip 和 端口
# mysql -u root -p -h 127.0.0.1 -P 3306

按回车,然后输入你的密码,再按回车确认。

登录进去后,查看 MySQL 版本

select version();


远程连接 MySQL

1. 修改 mysqld.cnf 配置文件:

sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf

修改 bind-address 属性,属性不存在就添加在[mysqld]块中(如果只需要指定的 ip 访问,使用防火墙控制)

bind-address = 0.0.0.0
# 端口,如果想修改端口的话,就修改 port 的值
# port = 3306

# 最大连接数量,默认是151,超过这个数量会报 Too many connections 错误
# max_connections = 500    # 使用 SHOW PROCESSLIST 命令查看连接了多少个数量了

# 空闲连接最长 sleep 时间为 300秒(5分钟),通过 SHOW PROCESSLIST 查看 sleep
# wait_timeout = 300    # 默认是28800秒(8小时),wait_timeout 主要针对非交互客户端的连接,连接池的连接就是非交互客户端

# interactive_timeout = 300     # 默认是28800秒(8小时),interactive_timeout 针对的是交互客户端,比如命令行连接的方式

使用如下命令,查询mysql使用的端口

sudo netstat -tulnp | grep mysql

2. 重启 MySQL 服务

sudo systemctl restart mysql

3. 创建远程登录账号

登录 MySQL 后,执行以下命令:

CREATE USER 'username'@'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'username'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;

设置完成后,即可使用刚刚创建的用户远程连接到MySQL数据库了。


创建只有操作指定数据库权限的用户

登录 MySQL 后,执行以下命令:

CREATE USER 'myuser'@'%' IDENTIFIED BY 'mypassword';
GRANT ALL PRIVILEGES ON mydb.* TO 'myuser'@'%';
FLUSH PRIVILEGES;
  • 'myuser'@'%':表示用户myuser允许从任意主机连接,如果需要myuser只能从 192.168.1.1 主机连接,那就改为 'myuser'@'192.168.1.1',第二行也要改成一样的
  • mydb.*:表示只对 mydb 数据库下的所有表拥有权限。
  • ALL PRIVILEGES:表示这个用户拥有对这个数据库的所有权限,如果只需要有 查询 权限,可以写成这样:GRANT SELECT ON mydb.* TO 'myuser'@'%';

修改用户密码

1. 先查询用户对应的Host

SELECT User, Host FROM mysql.user;

2. 修改指定用户的密码

ALTER USER 'test'@'%' IDENTIFIED BY 'new_password';
FLUSH PRIVILEGES;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

7 号

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值