在 Debian 11 Bullseye 上安装 MySQL
步骤 1. 在我们安装任何软件之前,通过apt在终端中运行以下命令来确保您的系统是最新的很重要:
apt -y update && apt -y screen wget curl unzip apt -y upgrade
步骤 2. 在 Debian 11 上安装 MySQL。
MySQL 在默认的 Debian 存储库中不可用。您需要在 Debian 11 上安装 MySQL APT 存储库:
wget https://repo.mysql.com//mysql-apt-config_0.8.22-1_all.deb dpkg -i mysql-apt-config_0.8.22-1_all.deb
在 MySQL 存储库安装过程中,如果出现提示,请选择 Debian buster 的存储库,然后按 TAB 键选择 Ok。按 ENTER 继续。
接下来,通过运行以下命令更新包列表并安装 MySQL 服务器包:
apt -y update && apt -y install mysql-server
在安装过程中,会出现一个新的弹出窗口,提示您输入数据库 root 密码。
安装完成后,MySQL服务将自动启动,您可以通过键入以下内容进行验证:
systemctl status mysql
步骤 3. 保护 MySQL。
MySQL 安装完成后,您可能需要保护新 MySQL。默认情况下,MySQL 未加固。您可以使用mysql_secure_installation脚本保护 MySQL 。您应该仔细阅读以下每个步骤,这些步骤将设置 root 密码、删除匿名用户、禁止远程 root 登录以及删除测试数据库和访问安全 MySQL:
mysql_secure_installation
像这样配置它:
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 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 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 - 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 Success. All done!
要登录 MySQL,请使用以下命令(请注意,它与登录 MySQL 数据库所用的命令相同):
mysql -u root -p
感谢您使用本教程在 Debian 11 Bullseye 上安装最新版本的 MySQL 8。如需其他帮助或有用信息,我们建议您查看MySQL 官方网站。
配置mysql允许远程连接的方法
默认情况下,mysql只允许本地登录,如果要开启远程连接,则需要修改/etc/mysql/my.conf文件。
一、修改/etc/mysql/my.conf
找到bind-address = 127.0.0.1这一行
改为bind-address = 0.0.0.0即可
二、为需要远程登录的用户赋予权限
1、新建用户远程连接mysql数据库
grant all on *.* to admin@'%' identified by '123456' with grant option;
flush privileges;
允许任何ip地址(%表示允许任何ip地址)的电脑用admin帐户和密码(123456)来访问这个mysql server。
注意admin账户不一定要存在。
2、支持root用户允许远程连接mysql数据库
grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
flush privileges;
三、查看系统用户