已复制
全屏展示
复制代码

Ubuntu 22.04 安装 MySQL


· 3 min read

一. 使用apt-get安装MySQL

使用 Ubuntu 22.04 系统的默认仓库源安装MySQL,不需要配置任何仓库,Ubuntu 22.04 默认安装的MySQL版本是 8.0 版本。

# 安装 MySQL
$ sudo apt-get install mysql-server

# 查看 MySQL 是否正常运行(安装完成后会自动启动)
# 如果没启动手动启动即可:sudo systemctl start mysql.service
# 如果没启动手动启动即可:sudo systemctl enable mysql.service
$ systemctl status mysql.service

● mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2023-10-31 03:54:34 UTC; 20s ago
    Process: 8597 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
   Main PID: 8605 (mysqld)
     Status: "Server is operational"
      Tasks: 38 (limit: 1006)
     Memory: 356.5M
        CPU: 849ms
     CGroup: /system.slice/mysql.service
             └─8605 /usr/sbin/mysqld
Oct 31 03:54:34 macossoftware systemd[1]: Starting MySQL Community Server...
Oct 31 03:54:34 macossoftware systemd[1]: Started MySQL Community Server.

二. MySQL本地root登录

大部分情况下,我们的应用程序都在本机,所以只需要配置一个本地登录的密码就行了,看下面步骤:

# 安装后需要使用 sudo 免密码进入MySQL控制台,然后配置root用户名密码
$ sudo mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.35-0ubuntu0.22.04.1 (Ubuntu)
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>


# 注意:这里只能配置root用户从localhost登录
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'passwd123456';
Query OK, 0 rows affected (0.00 sec)


mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)


# 然后退出控制台重新使用密码登录就行了
$ mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.35-0ubuntu0.22.04.1 (Ubuntu)
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>

三. MySQL远程登录配置

如果MySQL需要远程登录,需要有除了 root 用户外的其他用户,可以按照下面步骤配置远程登录、创建新用户、给新用户授权来操作:

# 首先修改监听所有网卡地址
$ sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf


# 在 [mysqld] 下面修改或者添加 bind-address 配置
bind-address            = 0.0.0.0


# 重启MySQL
$ sudo systemctl restart mysql.service


# 我们创建测试数据库,测试数据表,定义访问这个数据库的用户名和密码
mysql> create database test_database;
mysql> create table test_database.test_table(id int);


# 创建 user1 用户可以从本机的任意ip地址连接,并设置密码
# 设置 user1 可以访问 test_database.*(可根据实际情况调整)
# 也可以设置 *.* 表示所有数据库的数据表
mysql> create user 'user1'@'%' identified by 'User_password123';
mysql> grant all privileges on test_database.* to 'user1'@'%';
mysql> FLUSH PRIVILEGES;


# 最后:记得关闭系统的防火墙
$ sudo ufw disable


# 查看防火墙状态
$ sudo ufw status
Status: inactive


# 开启防火墙命令
# sudo ufw enable

最后用ip地址登录:


$ mysql -uuser1 -hx.x.x.x -pUser_password123 -P3306
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 8.0.35-0ubuntu0.22.04.1 (Ubuntu)
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>

🔗

文章推荐