Ubuntu 下 MySql 使用

发布于:2025-07-11 ⋅ 阅读:(15) ⋅ 点赞:(0)

1.开发背景

        开发项目需要使用到数据库,相对于轻量级的 SQLite,MySql 相对复杂一下,但是可以远程访问,还是比较舒服的。

2.开发需求

        Ubuntu 安装 MySql 服务端,Window 客户端访问 Ubuntu 数据库。

3.开发环境

        Ubuntu20.04 + Window10

4.实现步骤

4.1 安装 MySql

4.1.1 安装软件
# Ubuntu MySQl数据库服务端
sudo apt install mysql-server
4.1.2 查看状态
# 查看状态
sudo systemctl status mysql
4.1.3 启停 MySql
# 启动 mySql
sudo systemctl start mysql

# 停止 mySql
sudo systemctl stop mysql
4.1.4 设置自启动
# 开机自启动
sudo systemctl enable mysql

4.2 配置 MySql

sudo mysql_secure_installation
secure enough. Would you like to setup VALIDATE PASSWORD component?

Press y|Y for Yes, any other key for No: n

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done!

        截取关键配置,除了秘钥强保护不需要(测试,为了方便为主),其他的都是默认,根据自己需求配置即可。

4.3 登录 MySql

# root 用户进入数据库 测试机默认密码也是 root
sudo mysql -u root -p

4.4 远程访问 MySql

4.4.1 添加用户

        尽量不通过 root 访问,这里创建用户 yangjinghui

# 创建用户 yangjinghui 密码 root
CREATE USER 'yangjinghui'@'%' IDENTIFIED WITH mysql_native_password BY 'root';

# 查看用户信息
SELECT user, host FROM mysql.user;

# 用户授权 所有数据库
GRANT ALL PRIVILEGES ON *.* TO 'yangjinghui'@'%';

# 用户授权 个别数据库
#GRANT ALL PRIVILEGES ON mydb.* TO 'yangjinghui'@'%';

# 权限生效
FLUSH PRIVILEGES;
4.4.2 联网配置
# 修改配置 bind-address 修改 禁用 ssl
sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf
#bind-address = 127.0.0.1
bind-address = 0.0.0.0
ssl=0

# 防火墙通道
sudo ufw allow 3306

# 重启 mysql
sudo systemctl restart mysql
4.4.3 Window 访问 MySql


网站公告

今日签到

点亮在社区的每一天
去签到