美文网首页
Linux 安装 Mysql Server

Linux 安装 Mysql Server

作者: 一杉风雨 | 来源:发表于2019-06-17 20:54 被阅读0次

本篇记录了Ubuntu系统通过APT安装MySQL Server的具体配置过程。

环境

系统环境 版本
操作系统 Ubuntu 18.04
Mysql 5.7

APT安装MySQL Server

sudo apt install -y mysql-server

添加root用户

  1. 获取初始用户和密码
➜  ~ sudo cat /etc/mysql/debian.cnf 
# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host     = localhost
user     = debian-sys-maint
password = wIKGi0O8NfBjZsF6
socket   = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host     = localhost
user     = debian-sys-maint
password = wIKGi0O8NfBjZsF6
socket   = /var/run/mysqld/mysqld.sock
  1. 使用初始用户连接MySQL Server
mysql -u debian-sys-maint -p
  1. 检测root用户
    使用下面命令可以看到,root用户密码为空。这是由于此时root用户使用了auth_socket插件,该插件不关心密码,它只检查用户是否使用UNIX套接字进行连接,然后比较用户名。
mysql> SELECT User, Host, HEX(authentication_string) FROM mysql.user;
+------------------+-----------+------------------------------------------------------------------------------------+
| User             | Host      | HEX(authentication_string)                                                         |
+------------------+-----------+------------------------------------------------------------------------------------+
| root             | localhost | |
| mysql.session    | localhost | 2A5448495349534E4F544156414C494450415353574F52445448415443414E42455553454448455245 |
| mysql.sys        | localhost | 2A5448495349534E4F544156414C494450415353574F52445448415443414E42455553454448455245 |
| debian-sys-maint | localhost | 2A42424642344139433038464535433736463846423033334133344131463631423431383332354243 |
+------------------+-----------+------------------------------------------------------------------------------------+
5 rows in set (0.00 sec)
mysql> SELECT User, Plugin FROM mysql.user;
+------------------+-----------------------+
| User             | Plugin                |
+------------------+-----------------------+
| root             | auth_socket           |
| mysql.session    | mysql_native_password |
| mysql.sys        | mysql_native_password |
| debian-sys-maint | mysql_native_password |
+------------------+-----------------------+
5 rows in set (0.00 sec)
  1. 配置root用户
    如果要配置密码,需要在同一命令中同时更改插件并设置密码。
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';

设置root用户的任意地址访问

此时的root用户仅仅支持localhost的访问方式,想支持任意地址的访问还需执行以下操作

  1. 新增允许任何主机连接的root用户
grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
flush privileges;
  1. 修改/etc/mysql/mysql.conf.d/mysqld.cnf,找到以下语句并注释掉
# bind-address      = 127.0.0.1
  1. 重启MySQL服务
sudo service mysql restart

相关文章

网友评论

      本文标题:Linux 安装 Mysql Server

      本文链接:https://www.haomeiwen.com/subject/wznzfctx.html