登录
首页 >  数据库 >  MySQL

Ubuntu 安装MySQL8.0

来源:SegmentFault

时间:2023-01-24 19:58:27 248浏览 收藏

本篇文章主要是结合我之前面试的各种经历和实战开发中遇到的问题解决经验整理的,希望这篇《Ubuntu 安装MySQL8.0》对你有很大帮助!欢迎收藏,分享给更多的需要的朋友学习~

环境

  • Linux/Ubuntu20.04LTS
  • mysql-client-core-8.0
  • mysql-server-8.0 (8.0.23-0ubuntu0.20.04.1)

流程

  1. 打开终端,查看是否安装了MySQL

lauiji@lauiji-IdeaPad-15sIML-2020:~$ mysql

Command 'mysql' not found, but can be installed with:

sudo apt install mysql-client-core-8.0     # version 8.0.23-0ubuntu0.20.04.1, or
sudo apt install mariadb-client-core-10.3  # version 1:10.3.25-0ubuntu0.20.04.1
  1. 按提示安装一下

lauiji@lauiji-IdeaPad-15sIML-2020:~$ sudo apt install mysql-client-core-8.0
获取:1 http://cn.archive.ubuntu.com/ubuntu/ focal-updates/main amd64 mysql-client-core-8.0 amd64 8.0.23-0ubuntu0.20.04.1 [4,215 kB]
正在设置 mysql-client-core-8.0 (8.0.23-0ubuntu0.20.04.1) ...
正在处理用于 man-db (2.9.1-1) 的触发器 ...
  1. 查看运行服务 $ systemctl status mysql

lauiji@lauiji-IdeaPad-15sIML-2020:~$ systemctl status mysql
Unit mysql.service could not be found.
  1. 安装服务

sudo apt-get update  #更新源
sudo apt-get install mysql-server #安装

lauiji@lauiji-IdeaPad-15sIML-2020:~$ sudo apt-get install mysql-server
update-alternatives: 使用 /var/lib/mecab/dic/ipadic-utf8 来在自动模式中提供 /var/lib/mecab/dic/debian (mecab-dictionary)
正在设置 mysql-server-8.0 (8.0.23-0ubuntu0.20.04.1) ...
update-alternatives: 使用 /etc/mysql/mysql.cnf 来在自动模式中提供 /etc/mysql/my.cnf (my.cnf)
Renaming removed key_buffer and myisam-recover options (if present)
mysqld will log errors to /var/log/mysql/error.log
mysqld is running as pid 19429
Created symlink /etc/systemd/system/multi-user.target.wants/mysql.service → /lib/systemd/system/mysql.service.

这时就安装完成了!

  1. 查看运行服务

lauiji@lauiji-IdeaPad-15sIML-2020:~$ systemctl status mysql
● mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset:>
     Active: active (running) since Sat 2021-07-03 10:48:52 CST; 24h ago
    Process: 947 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=ex>
   Main PID: 991 (mysqld)
     Status: "Server is operational"
      Tasks: 38 (limit: 13995)
     Memory: 402.8M
     CGroup: /system.slice/mysql.service
             └─991 /usr/sbin/mysqld
  1. 安装完成后如何登录到数据库呢?密码啥?
刚安装完的数据库还没有密码,密码为空,直接回车即可,登录到数据库修改密码

lauiji@lauiji-IdeaPad-15sIML-2020:/etc/mysql$ sudo mysql -u root -p 
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.23-0ubuntu0.20.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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)

这里root就是我们刚才连接的账户,

debian-sys-maint
账号是在安装MySQL自动产生的,可以通过下面的命令查看到它,$ sudo cat /etc/mysql/debian.cnf

mysql> select User, Host from mysql.user;
+------------------+-----------+
| User             | Host      |
+------------------+-----------+
| debian-sys-maint | localhost |
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |
+------------------+-----------+
5 rows in set (0.00 sec)
  1. 设置密码
设置你的root登录密码,这里举例为 123456

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
Query OK, 0 rows affected (0.00 sec)
  1. 刷新权限

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
  1. 使用新密码重新登录

mysql> quit;
Bye
lauiji@lauiji-IdeaPad-15sIML-2020:/etc/mysql$ mysql -u root -p
Enter password: 

提示:MySQL8.0的版本更改了root账户的授权方式,默认是auth_socket。也就是说需要通过 Unix socket 文件来验证所有连接到localhost的用户,不能使用提供密码的方式了。

  1. linux环境安装mysql5.7 和 mysql8.0初始密码的区别
linux安装完mysql5.7和8.0初始密码是不一样的。

一、mysql5.7初始密码

   linux安装MySQL5.7,mysql为root用户随机生成了一个密码在error log中,
   error log的位置默认是/var/log/mysqld.log,启动过一次才可以查看临时密码,
   $ grep 'temporary password' /var/log/mysqld.log

二、mysql8.0初始密码

    Mysql8.0安装root账号的初始密码是没有的,直接回车就可登录,不用输密码
    输入登录mysql命令 $ mysql -u root -p

【遇上雨季,吃定彩虹】 --Layuji

文中关于mysql的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《Ubuntu 安装MySQL8.0》文章吧,也可关注golang学习网公众号了解相关技术文章。

声明:本文转载于:SegmentFault 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>
评论列表