Deepin 或 Ubuntu 安装 MySQL 以及常见问题

Deepin 和 Ubuntu 都是 Linux 系统的代表,其安装 MySQL 的方式也基本相同,本文以 Deepin 系统为例简述了 MySQL 的安装方式以及常见问题

mysql 安装

$ sudo apt-get install mysql-server mysql-client

安装完成后默认会开启 mysql 服务,此时可以使用下面的命令查看是否安装成功

$ sudo lsof -i tcp | grep mysql
// mysqld  9819 mysql   28u  IPv4 189603    0t0  TCP localhost:mysql (LISTEN)

或者

$ sudo netstat -tap | grep mysql
// tcp    0    0 localhost:mysql    0.0.0.0:*     LISTEN     9819/mysqld 

账号

从某个版本开始,mysql 安装好后会自动新建一个账户,新建的账户可以使用下面的命令查看(deepin)

$ sudo cat /etc/mysql/debian.cnf

# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host     = localhost
user     = debian-sys-maint
password = TPxBnH4OkLZ58JvU
socket   = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host     = localhost
user     = debian-sys-maint
password = TPxBnH4OkLZ58JvU
socket   = /var/run/mysqld/mysqld.sock

当然也可以使用 root 账号不用密码访问数据库

$ sudo mysql -u root -p

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.7.21-1 (Debian)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

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.

数据库安装完毕建议立即修改 root 密码,方法参考另一篇文章 http://blog.johnhan.cn/blog_1017.html

数据库、数据表查看

查看当前所有数据库

mysql> show databases;

+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

查看 user 表(user 表位于 mysql 数据库中)的 host、user、authentication_string、plugin 字段

* 从某个版本开始,密码不在使用 password 字段,而变成了 authentication_string

mysql> select host,user,authentication_string,plugin from user;

+-----------+------------------+-------------------------------------------+-----------------------+
| host      | user             | authentication_string                     | plugin                |
+-----------+------------------+-------------------------------------------+-----------------------+
| localhost | root             |                                           | auth_socket           |
| localhost | mysql.session    | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password |
| localhost | mysql.sys        | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password |
| localhost | debian-sys-maint | *6393E0AA56EC75832112DFBA4CD6A60A659AC14E | mysql_native_password |
+-----------+------------------+-------------------------------------------+-----------------------+
4 rows in set (0.00 sec)

user 表存储的就是 mysql 的用户信息,上面的打印结果就可以看到 root 用户和安装 mysql 生成的 debian-sys-maint 用户

退出 mysql

mysql> quit;

提示

除特殊说明外本人博客均属原创,转载请注明出处:http://blog.johnhan.cn/blog_1016.html
鄂ICP备17018604号-1  鄂公网安备42060702000030号