ubuntu 安装mysql5.7 允许远程连接

MySQL配置与权限管理
本文详细介绍了MySQL数据库的默认用户和密码配置,包括如何查看和修改配置文件,使用debian-sys-maint用户登录,查看user表结构,更新root用户密码,配置远程连接,以及为root用户授权的过程。

使用默认的用户和密码

  1. 查看 /etc/mysql/debian.cnf
# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host     = localhost
user     = debian-sys-maint
password = yNcxf43lFP3PNPOG
socket   = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host     = localhost
user     = debian-sys-maint   ### 可以使用的用户
password = yNcxf43lFP3PNPOG  ### 可以使用的密码
socket   = /var/run/mysqld/mysqld.sock
  1. 使用 debian-sys-maint 登陆 mysql
    mysql -udebian-sys-maint -pyNcxf43lFP3PNPOG

  2. use mysql;

  3. 查看 user 表结构

mysql> show create table user\G
*************************** 1. row ***************************
       Table: user
Create Table: CREATE TABLE `user` (
  `Host` char(60) COLLATE utf8_bin NOT NULL DEFAULT '',
  `User` char(32) COLLATE utf8_bin NOT NULL DEFAULT '',
  `Select_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Insert_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Update_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Delete_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Create_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Drop_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Reload_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Shutdown_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Process_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `File_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Grant_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `References_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Index_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Alter_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Show_db_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Super_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Create_tmp_table_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Lock_tables_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Execute_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Repl_slave_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Repl_client_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Create_view_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Show_view_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Create_routine_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Alter_routine_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Create_user_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Event_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Trigger_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Create_tablespace_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `ssl_type` enum('','ANY','X509','SPECIFIED') CHARACTER SET utf8 NOT NULL DEFAULT '',
  `ssl_cipher` blob NOT NULL,
  `x509_issuer` blob NOT NULL,
  `x509_subject` blob NOT NULL,
  `max_questions` int(11) unsigned NOT NULL DEFAULT '0',
  `max_updates` int(11) unsigned NOT NULL DEFAULT '0',
  `max_connections` int(11) unsigned NOT NULL DEFAULT '0',
  `max_user_connections` int(11) unsigned NOT NULL DEFAULT '0',
  `plugin` char(64) COLLATE utf8_bin NOT NULL DEFAULT 'mysql_native_password',
  `authentication_string` text COLLATE utf8_bin,    ######
  `password_expired` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `password_last_changed` timestamp NULL DEFAULT NULL,
  `password_lifetime` smallint(5) unsigned DEFAULT NULL,
  `account_locked` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  PRIMARY KEY (`Host`,`User`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Users and global privileges'
1 row in set (0.00 sec)
  1. 修改 root 密码
    update user set authentication_string=PASSWORD ("your_password") where user='root';
  2. flush privileges;

允许远程连接

修改 /etc/mysql/mysql.conf.d/mysqld.conf

root@aliyun:/etc/mysql/mysql.conf.d# cat mysqld.cnf
#
# The MySQL database server configuration file.
#
# You can copy this to one of:
# - "/etc/mysql/my.cnf" to set global options,
# - "~/.my.cnf" to set user-specific options.
#
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

# This will be passed to all mysql clients
# It has been reported that passwords should be enclosed with ticks/quotes
# escpecially if they contain "#" chars...
# Remember to edit /etc/mysql/debian.cnf when changing the socket location.

# Here is entries for some specific programs
# The following values assume you have at least 32M ram

[mysqld_safe]
socket          = /var/run/mysqld/mysqld.sock
nice            = 0

[mysqld]
#
# * Basic Settings
#
user            = mysql
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
port            = 3306
basedir         = /usr
datadir         = /var/lib/mysql
tmpdir          = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#bind-address           = 127.0.0.1
#
# * Fine Tuning
#
key_buffer_size         = 16M
max_allowed_packet      = 16M
thread_stack            = 192K
thread_cache_size       = 8
# This replaces the startup script and checks MyISAM tables if needed
# the first time they are touched
myisam-recover-options  = BACKUP
#max_connections        = 100
#table_open_cache       = 64
#thread_concurrency     = 10
#
# * Query Cache Configuration
#
query_cache_limit       = 1M
query_cache_size        = 16M
#
# * Logging and Replication
#
# Both location gets rotated by the cronjob.
# Be aware that this log type is a performance killer.
# As of 5.1 you can enable the log at runtime!
#general_log_file        = /var/log/mysql/mysql.log
#general_log             = 1
#
# Error log - should be very few entries.
#
log_error = /var/log/mysql/error.log
#
# Here you can see queries with especially long duration
#slow_query_log         = 1
#slow_query_log_file    = /var/log/mysql/mysql-slow.log
#long_query_time = 2
#log-queries-not-using-indexes
#
# The following can be used as easy to replay backup logs or for replication.
# note: if you are setting up a replication slave, see README.Debian about
#       other settings you may need to change.
#server-id              = 1
#log_bin                        = /var/log/mysql/mysql-bin.log
expire_logs_days        = 10
max_binlog_size   = 100M
#binlog_do_db           = include_database_name
#binlog_ignore_db       = include_database_name
#
# * InnoDB
#
# InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/.
# Read the manual for more InnoDB related options. There are many!
#
# * Security Features
#
# Read the manual, too, if you want chroot!
# chroot = /var/lib/mysql/
#
# For generating SSL certificates I recommend the OpenSSL GUI "tinyca".
#
# ssl-ca=/etc/mysql/cacert.pem
# ssl-cert=/etc/mysql/server-cert.pem
# ssl-key=/etc/mysql/server-key.pem

注释掉 bind-address = 127.0.0.1

给 root 用户授权

  1. mysql -uroot -p your_password
  2. GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'your_password';
  3. flush privileges;
### 回答1: 要在 Ubuntu 18.04 的 MySQL 5.7 上开放远程连接,请执行以下步骤: 1. 登录 MySQL 控制台:打开终端并输入以下命令: ``` mysql -u root -p ``` 2. 修改 MySQL 配置文件:使用以下命令编辑 MySQL 配置文件: ``` sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf ``` 3. 修改 bind-address 参数:找到 bind-address 参数并将其值从 127.0.0.1 改为 0.0.0.0。这样,MySQL 服务器将允许从任何 IP 地址进行连接。 4. 重启 MySQL 服务:使用以下命令重启 MySQL 服务: ``` sudo service mysql restart ``` 5. 创建用户并授予远程访问权限:在 MySQL 控制台中执行以下命令: ``` GRANT ALL PRIVILEGES ON *.* TO 'your_user'@'%' IDENTIFIED BY 'your_password'; ``` 6. 刷新权限:执行以下命令: ``` FLUSH PRIVILEGES; ``` 7. 退出 MySQL 控制台:使用以下命令退出 MySQL 控制台: ``` exit ``` 接下来,您就可以使用任何远程工具(如 MySQL Workbench)从远程计算机连接MySQL 服务器了。 ### 回答2: 要在Ubuntu 18.04上开放MySQL 5.7远程连接,你需要按照以下步骤进行操作: 1. 确保你已经登录到Ubuntu的终端或远程终端,使用管理员权限(sudo)执行以下命令来安装MySQL服务器: ```shell sudo apt-get update sudo apt-get install mysql-server ``` 2. 安装完成后,执行以下命令来启动MySQL服务: ```shell sudo systemctl start mysql ``` 3. 执行以下命令来设置MySQL服务自动启动: ```shell sudo systemctl enable mysql ``` 4. 为了能够远程访问MySQL服务器,你需要修改MySQL的配置文件。执行以下命令来编辑该文件: ```shell sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf ``` 5. 在编辑器中,找到以下行并将其注释掉(在行前面加上`#`符号): ```shell bind-address = 127.0.0.1 ``` 6. 添加以下行来允许所有远程IP地址连接MySQL服务器: ```shell bind-address = 0.0.0.0 ``` 7. 保存并关闭编辑器(使用Ctrl + X,然后输入Y来保存修改)。 8. 执行以下命令来重启MySQL服务以使配置修改生效: ```shell sudo systemctl restart mysql ``` 现在,你的Ubuntu 18.04上的MySQL服务器已经开放了远程连接。你可以使用其他计算机上的MySQL客户端工具来连接它。请不要忘记在远程连接之前设置适当的安全性措施。 ### 回答3: 要在Ubuntu 18.04上开放远程连接MySQL 5.7,您需要按照以下步骤进行操作: 1. 首先,确保您已经在Ubuntu安装MySQL 5.7版本。如果尚未安装,请使用以下命令进行安装: ``` sudo apt-get update sudo apt-get install mysql-server-5.7 ``` 2. 访问MySQL配置文件以进行修改: ``` sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf ``` 3. 在该文件中找到“bind-address”行,并将其注释掉。注释的符号是“#”,将行变为: ``` # bind-address = 127.0.0.1 ``` 4. 保存并关闭文件。 5. 重启MySQL服务使更改生效: ``` sudo service mysql restart ``` 6. 接下来,您需要为远程访问的MySQL用户创建授权。打开MySQL命令行界面: ``` mysql -u root -p ``` 7.MySQL命令行中,使用以下命令创建一个允许远程连接的用户,并将其替换为您想要使用的用户名和密码: ``` GRANT ALL PRIVILEGES ON *.* TO 'username'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION; ``` 请确保在'username'和'password'之间使用您的用户名和密码。 8. 刷新MySQL权限: ``` FLUSH PRIVILEGES; ``` 9. 现在,您已经成功地开放了MySQL 5.7远程连接。您可以使用远程客户端连接MySQL服务器: ``` mysql -u username -p -h <服务器IP地址> ``` 请将<服务器IP地址>替换为您的MySQL服务器的实际IP地址。 通过这些步骤,您应该能够在Ubuntu 18.04上成功地开放MySQL 5.7远程连接。请记住,远程连接可能会带来安全风险,因此请确保在网络上采取适当的安全措施。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值