netstat -an|grep 3306
这样代表端口未打开
>>> netstat -an|grep 3306tcp 0 0 0.0.0.0:3306 0.0.0.0:* ListEN二、修改访问权限
进入目录/etc/MysqL,或者是(/etc/MysqL/MysqL.conf.d)如下所示
$ cd /etc/MysqL/
在该目录下,有一个配置文件my.cnf(MysqLd.cnf),如下所示
>>> lsconf.d debian.cnf debian-start my1.cnf my.cnf
找到my.cnf文件,并打开配置文件
$ sudo vim my.cnf
文件打开后有一大段注释说明,不用去管它,直接看到下中的部分:
[MysqLd]## * Basic Settings#user = MysqLpID-file = /var/run/MysqLd/MysqLd.pIDsocket = /var/run/MysqLd/MysqLd.sockport = 3306basedir = /usrdatadir = /var/lib/MysqLtmpdir = /tmplc-messages-dir = /usr/share/MysqLskip-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 = 2048Mmax_allowed_packet = 500M
找到bind-addresss上一行,注释意思是在默认情况下只允许本地服务访问MysqL,所以我们需要注释掉bind-address这条配置信息。注释方法,如下图所示:
# bind-address = 127.0.0.1
三、 修改端口号:
在当前这个配置文件,找到
[MysqLd]## * Basic Settings#user = MysqLpID-file = /var/run/MysqLd/MysqLd.pIDsocket = /var/run/MysqLd/MysqLd.sockport = 3306basedir = /usrdatadir = /var/lib/MysqLtmpdir = /tmplc-messages-dir = /usr/share/MysqLskip-external-locking
如果这里没有prot =3306这条配置,那么需要在其中增加一条端口配置,如下所示:
port = 3306
修改文件后保存。
四、开放root账户访问权限
在第三步中,我们仅仅只是取消了本地访问限制,但是我们还是没有对账户权限进行设置。
重启MysqL服务,并进入MysqL控制台:
# 重启MysqLservice MysqL stop service MysqL start# 登陆MysqL -h 127.0.0.1 -u root -p
登陆成功界面
Your MysqL connection ID is 2441Server version: 5.5.58-0ubuntu0.14.04.1 (Ubuntu)copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.Oracle is a registered Trademark of Oracle Corporation and/or itsaffiliates. Other names may be Trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.MysqL>
切换到系统数据库“MysqL”中:
$ use MysqL;
查看一下该数据库中的所有的表
$ show tables;
界面如下:
MysqL> show tables;+---------------------------+| tables_in_MysqL |+---------------------------+| columns_priv || db || event || func || general_log || help_category || help_keyword || help_relation || help_topic || host || ndb_binlog_index || plugin || proc || procs_priv || proxIEs_priv || servers || slow_log || tables_priv || time_zone || time_zone_leap_second || time_zone_name || time_zone_Transition || time_zone_Transition_type || user |+---------------------------+24 rows in set (0.00 sec)
我们要修改上图中的最后一张表“user”,看一下这张表有哪些字段:
$ desc user;
界面如下:
MysqL> desc user;+------------------------+-----------------------------------+------+-----+---------+-------+| FIEld | Type | Null | Key | Default | Extra |+------------------------+-----------------------------------+------+-----+---------+-------+| Host | char(60) | NO | PRI | | || User | char(16) | NO | PRI | | || Password | char(41) | NO | | | || Select_priv | enum('N','Y') | NO | | N | || Insert_priv | enum('N','Y') | NO | | N | || Update_priv | enum('N','Y') | NO | | N | || Delete_priv | enum('N','Y') | NO | | N | || Create_priv | enum('N','Y') | NO | | N | || Drop_priv | enum('N','Y') | NO | | N | || Reload_priv | enum('N','Y') | NO | | N | || Shutdown_priv | enum('N','Y') | NO | | N | || Process_priv | enum('N','Y') | NO | | N | || file_priv | enum('N','Y') | NO | | N | || Grant_priv | enum('N','Y') | NO | | N | || References_priv | enum('N','Y') | NO | | N | || Index_priv | enum('N','Y') | NO | | N | || Alter_priv | enum('N','Y') | NO | | N | || Show_db_priv | enum('N','Y') | NO | | N | || Super_priv | enum('N','Y') | NO | | N | || Create_tmp_table_priv | enum('N','Y') | NO | | N | || Lock_tables_priv | enum('N','Y') | NO | | N | || Execute_priv | enum('N','Y') | NO | | N | || Repl_slave_priv | enum('N','Y') | NO | | N | || Repl_clIEnt_priv | enum('N','Y') | NO | | N | || Create_vIEw_priv | enum('N','Y') | NO | | N | || Show_vIEw_priv | enum('N','Y') | NO | | N | || Create_routine_priv | enum('N','Y') | NO | | N | || Alter_routine_priv | enum('N','Y') | NO | | N | || Create_user_priv | enum('N','Y') | NO | | N | || Event_priv | enum('N','Y') | NO | | N | || Trigger_priv | enum('N','Y') | NO | | N | || Create_tablespace_priv | enum('N','Y') | NO | | N | || ssl_type | enum('','ANY','X509','SPECIFIED') | NO | | | || ssl_cipher | blob | NO | | NulL | || x509_issuer | blob | NO | | NulL | || x509_subject | blob | NO | | NulL | || max_questions | int(11) unsigned | NO | | 0 | || max_updates | int(11) unsigned | NO | | 0 | || max_connections | int(11) unsigned | NO | | 0 | || max_user_connections | int(11) unsigned | NO | | 0 | || plugin | char(64) | YES | | | || authentication_string | text | YES | | NulL | |+------------------------+-----------------------------------+------+-----+---------+-------+42 rows in set (0.00 sec)
字段非常多,就不一一罗列了。我们要用到的只是“Host”和“User”两个字段而已:
$ select host,user from user;
在这张表里,我们看到root用户仅仅只能在本地访问MysqL服务,所以我们要把它修改为“%”,意思是无论在哪里root账户都能够访问数据库服务:
$ update user set host='%' where user='root';
注意,在真实的生产环境中,并不建议这么修改,因为安全风险太大。我建议根据实际情况将root用户的host项修改为某个指定的ip地址,或仍然保持localhost
最后一项设置,开放root账户所有权限:
$ grant all privileges on *.* to 'root'@'%' IDentifIEd by '你的root账户密码';
使各种权限设置立即生效:
$ flush privileges;
选择出host列和user列的数据:
$ select host,user from user;
五、再次确认3306端口状态
先退出MysqL,查看3306端口状态
$ quit$ netstat -an|grep 3306
如果看到下图这样,就可以了:
netstat -an|grep 3306tcp 0 0 0.0.0.0:3306 0.0.0.0:* ListEN总结
以上是内存溢出为你收集整理的在Ubuntu/Linux环境下使用MySQL:开放/修改3306端口、开放访问权限全部内容,希望文章能够帮你解决在Ubuntu/Linux环境下使用MySQL:开放/修改3306端口、开放访问权限所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)