Ubuntu20.04安装MySQL5.7与远程连接

一、安装MySQL5.7

1.更换镜像源

sudo cp /etc/apt/sources.list /etc/apt/sources.list.old     #备份原来的文件
sudo vim /etc/apt/sources.list      #修改sources.list文件

配置文件内容如下所示:

# 清华镜像源
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted universe multiverse

2.更新

sudo apt update    # 更新镜像源

3.安装MySQL5.7

sudo apt install -y mysql-server-5.7

(需要输入两次mysql的root用户的密码

软件包设置
   ┌──────────────────────────┤ 正在设定 mysql-server-5.7 ├──────────────────────────┐
   │ While not mandatory, it is highly recommended that you set a password for the   │
   │ MySQL administrative "root" user.                                               │
   │                                                                                 │
   │ If this field is left blank, the password will not be changed.                  │
   │                                                                                 │
   │ New password for the MySQL "root" user:                                         │
   │                                                                                 │
   │ *********______________________________________________________________________ │
   │                                                                                 │
   │                                     <确定>                                      │
   │                                                                                 │
   └─────────────────────────────────────────────────────────────────────────────────┘
软件包设置
                     ┌────────┤ 正在设定 mysql-server-5.7 ├────────┐
                     │                                             │
                     │                                             │
                     │ Repeat password for the MySQL "root" user:  │
                     │                                             │
                     │ *********__________________________________ │
                     │                                             │
                     │                   <确定>                    │
                     │                                             │
                     └─────────────────────────────────────────────┘

4、安装完成,查看MySQL的版本

mysql -V    # 查看mysql版本

5、连接MySQL(使用root和刚才设置的root的密码)

➜  ~ mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.33-0ubuntu0.16.04.1 (Ubuntu)

Copyright (c) 2000, 2021, 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> 

二、配置远程连接

1、修改配置文件

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

找到 bind-address 修改值为 0.0.0.0
在这里插入图片描述
重启MySQL

sudo /etc/init.d/mysql restart 

2、为用户授权远程连接服务

使用 root 用户登录 MySQL 数据库

mysql -u root -p

使用 MySQL 命令为 root 用户授权 MySQL 远程连接服务

grant all privileges on *.* to 'root'@'%' identified by 'root' with grant option;

将配置写入 MySQL 授权表中

flush privileges;

然后即可连接远程连接

三、启动、停止服务

#启动
sudo service mysql start
#停止
sudo service mysql stop

猜你喜欢

转载自blog.csdn.net/m0_68744965/article/details/129017095