{"id":164484,"date":"2019-12-20T10:11:57","date_gmt":"2019-12-20T02:11:57","guid":{"rendered":"https:\/\/gulass.cn\/?p=164484"},"modified":"2019-11-29T09:13:48","modified_gmt":"2019-11-29T01:13:48","slug":"mysql-service","status":"publish","type":"post","link":"https:\/\/gulass.cn\/mysql-service.html","title":{"rendered":"\u5141\u8bb8\u8fdc\u7a0b\u8fde\u63a5\u5230MySQL\u6570\u636e\u5e93\u670d\u52a1\u5668\u7684\u6b65\u9aa4"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u4e0b\u9762\u4ecb\u7ecd\u5141\u8bb8\u8fdc\u7a0b\u8fde\u63a5\u5230MySQL\u670d\u52a1\u5668\u6240\u9700\u7684\u6b65\u9aa4\u3002\u540c\u6837\u9002\u7528\u4e8eMariaDB\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cMySQL\u670d\u52a1\u5668\u4ec5\u4fa6\u542c\u6765\u81ealocalhost\u7684\u8fde\u63a5\uff0c\u8fd9\u610f\u5473\u7740\u53ea\u80fd\u7531\u8fd0\u884c\u5728\u540c\u4e00\u4e3b\u673a\u4e0a\u7684\u5e94\u7528\u7a0b\u5e8f\u8bbf\u95ee\u5b83\u3002
\n\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6709\u5fc5\u8981\u5141\u8bb8\u8fdc\u7a0b\u8fde\u63a5\u3002\u6bd4\u5982\u4f60\u60f3\u8981\u4ece\u672c\u5730\u7cfb\u7edf\u8fde\u63a5\u5230\u8fdc\u7a0bMySQL\u670d\u52a1\u5668\uff0c\u6216\u8005\u4f7f\u7528\u591a\u670d\u52a1\u5668\u90e8\u7f72\u3001\u5e94\u7528\u7a0b\u5e8f\u4e0e\u6570\u636e\u5e93\u670d\u52a1\u5668\u4e0d\u5728\u540c\u4e00\u53f0\u673a\u5668\u4e0a\u8fd0\u884c\u65f6\u3002<\/p>\n

\"\"<\/p>\n

\u914d\u7f6eMySQL\u670d\u52a1\u5668<\/strong><\/div>\n

\u7b2c\u4e00\u6b65\u662f\u8bbe\u7f6eMySQL\u670d\u52a1\u5668\uff0c\u4ee5\u4fa6\u542c\u673a\u5668\u4e0a\u7684\u67d0\u4e2a\u7279\u5b9aIP\u5730\u5740\u6216\u6240\u6709IP\u5730\u5740\u3002
\n\u5982\u679cMySQL\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u53ef\u4ee5\u901a\u8fc7\u4e13\u7528\u7f51\u7edc\u76f8\u4e92\u901a\u4fe1\uff0c\u90a3\u4e48\u6700\u597d\u7684\u9009\u62e9\u662f\u8bbe\u7f6eMySQL\u670d\u52a1\u5668\uff0c\u4ec5\u4fa6\u542c\u4e13\u7528IP\u3002\u5426\u5219\uff0c\u5982\u679c\u4f60\u60f3\u901a\u8fc7\u516c\u5171\u7f51\u7edc\u8fde\u63a5\u5230\u670d\u52a1\u5668\uff0c\u5c31\u8bbe\u7f6eMySQL\u670d\u52a1\u5668\uff0c\u4fa6\u542c\u673a\u5668\u4e0a\u7684\u6240\u6709IP\u5730\u5740\u3002
\n\u4e3a\u6b64\uff0c\u4f60\u9700\u8981\u7f16\u8f91MySQL\u914d\u7f6e\u6587\u4ef6\uff0c\u6dfb\u52a0\u6216\u66f4\u6539bind-address\u9009\u9879\u7684\u503c\u3002\u4f60\u53ef\u4ee5\u8bbe\u7f6e\u5355\u4e2aIP\u5730\u5740\u548cIP\u8303\u56f4\u3002\u5982\u679c\u5730\u5740\u662f0.0.0.0\uff0cMySQL\u670d\u52a1\u5668\u63a5\u53d7\u6240\u6709\u4e3b\u673aIPv4\u63a5\u53e3\u4e0a\u7684\u8fde\u63a5\u3002\u5982\u679c\u4f60\u5728\u7cfb\u7edf\u4e0a\u914d\u7f6e\u4e86IPv6\uff0c\u6539\u800c\u4f7f\u7528::\uff0c\u800c\u4e0d\u662f0.0.0.0\u3002
\nMySQL\u914d\u7f6e\u6587\u4ef6\u7684\u4f4d\u7f6e\u56e0\u53d1\u884c\u7248\u800c\u5f02\u3002\u5728Ubuntu\u548cDebian\u4e2d\uff0c\u8be5\u6587\u4ef6\u4f4d\u4e8e\/etc\/mysql\/mysql.conf.d\/mysqld.cnf\uff0c\u800c\u5728\u57fa\u4e8eRed Hat\u7684\u53d1\u884c\u7248(\u6bd4\u5982CentOS)\u4e2d\uff0c\u8be5\u6587\u4ef6\u4f4d\u4e8e\/etc\/my.cnf\u3002
\n\u4f7f\u7528\u6587\u672c\u7f16\u8f91\u5668\u6253\u5f00\u6587\u4ef6\uff1a<\/p>\n

sudo\u00a0nano\u00a0\/etc\/mysql\/mysql.conf.d\/mysqld.cnf<\/pre>\n

\u641c\u7d22\u4ee5bind-address\u5f00\u5934\u7684\u884c\uff0c\u5c06\u5176\u503c\u8bbe\u7f6e\u4e3aMySQL\u670d\u52a1\u5668\u5e94\u4fa6\u542c\u7684IP\u5730\u5740\u3002
\n\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u8be5\u503c\u88ab\u8bbe\u7f6e\u4e3a127.0.0.1(\u4ec5\u5728localhost\u4e2d\u4fa6\u542c)\u3002
\n\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u503c\u6539\u4e3a0.0.0.0\u6765\u8bbe\u7f6eMySQL\u670d\u52a1\u5668\uff0c\u4fa6\u542c\u6240\u6709IPv4\u63a5\u53e3\uff1a<\/p>\n

mysqld.cnf\r\nbind-address\u00a0=\u00a00.0.0.0\r\n#\u00a0skip-networking<\/pre>\n

\u5982\u679c\u6709\u4e00\u884c\u542b\u6709skip-networking\uff0c\u5220\u9664\u8be5\u884c\u6216\u901a\u8fc7\u5728\u884c\u5f00\u5934\u6dfb\u52a0#\u6765\u6ce8\u91ca\u6389\u5b83\u3002
\n\u5728MySQL 8.0\u53ca\u66f4\u9ad8\u7248\u672c\u4e2d\uff0cbind-address\u6307\u4ee4\u53ef\u80fd\u4e0d\u5b58\u5728\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5c06\u5176\u6dfb\u52a0\u5230[mysqld]\u90e8\u5206\u4e0b\u3002
\n\u5b8c\u6210\u540e\uff0c\u91cd\u65b0\u542f\u52a8MySQL\u670d\u52a1\u4ee5\u4f7f\u66f4\u6539\u751f\u6548\u3002\u53ea\u6709root\u7528\u6237\u6216\u62e5\u6709sudo\u6743\u9650\u7684\u7528\u6237\u624d\u80fd\u91cd\u65b0\u542f\u52a8\u670d\u52a1\u3002
\n\u60f3\u5728Debian\u6216Ubuntu\u4e0a\u91cd\u65b0\u542f\u52a8MySQL\u670d\u52a1\uff0c\u8bf7\u8f93\u5165\uff1a<\/p>\n

\u00a0sudo\u00a0systemctl\u00a0restart\u00a0mysqld<\/pre>\n

\u5728\u57fa\u4e8eRedHat\u7684\u53d1\u884c\u7248(\u6bd4\u5982CentOS)\u4e0a\uff0c\u60f3\u91cd\u65b0\u542f\u52a8\u670d\u52a1\uff0c\u8fd0\u884c\uff1a<\/p>\n

sudo\u00a0systemctl\u00a0restart\u00a0mysql<\/pre>\n
\u6388\u6743\u7528\u6237\u4ece\u8fdc\u7a0b\u673a\u5668\u6765\u8bbf\u95ee<\/strong><\/div>\n

\u4e0b\u4e00\u6b65\u662f\u5141\u8bb8\u8fdc\u7a0b\u7528\u6237\u8bbf\u95ee\u6570\u636e\u5e93\u3002
\n\u8f93\u5165\u4ee5\u4e0b\u5185\u5bb9\uff0c\u4ee5root\u7528\u6237\u8eab\u4efd\u767b\u5f55MySQL\u670d\u52a1\u5668\uff1a<\/p>\n

$\u00a0sudo\u00a0mysql<\/pre>\n

\u5982\u679c\u4f60\u4f7f\u7528\u65e7\u7684\u539f\u751fMySQL\u9a8c\u8bc1\u63d2\u4ef6\u4ee5root\u7528\u6237\u8eab\u4efd\u767b\u5f55\uff0c\u8bf7\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff0c\u51fa\u73b0\u63d0\u793a\u65f6\u8f93\u5165\u5bc6\u7801\uff1a<\/p>\n

$\u00a0mysql\u00a0-uroot\u00a0-p<\/pre>\n

\u4eceMySQL shell\u91cc\u9762\uff0c\u4f7f\u7528GRANT\u8bed\u53e5\u4e3a\u8fdc\u7a0b\u7528\u6237\u6388\u4e88\u8bbf\u95ee\u6743\u9650\u3002<\/p>\n

mysql> GRANT ALL ON database_name.* TO user_name@'ip_address' IDENTIFIED BY 'user_password'; <\/pre>\n

\u5176\u4e2d\uff1a<\/p>\n

    \n
  • database_name\u662f\u7528\u6237\u5c06\u8fde\u63a5\u5230\u7684\u6570\u636e\u5e93\u7684\u540d\u79f0\u3002<\/li>\n
  • user_name\u662fMySQL\u7528\u6237\u7684\u540d\u79f0\u3002<\/li>\n
  • ip_address\u662f\u7528\u6237\u5c06\u8fde\u63a5\u7684IP\u5730\u5740\u3002\u4f7f\u7528%\u5141\u8bb8\u7528\u6237\u4ece\u4efb\u4f55IP\u5730\u5740\u8fdb\u884c\u8fde\u63a5\u3002<\/li>\n
  • user_password\u662f\u7528\u6237\u5bc6\u7801\u3002<\/li>\n<\/ul>\n

    \u6bd4\u5982\u8bf4\uff0c\u8981\u6388\u4e88\u540d\u4e3afoo\u3001\u4f7f\u7528\u5bc6\u7801my_password\u7684\u7528\u6237\u4eceIP\u662f10.8.0.5\u7684\u5ba2\u6237\u7aef\u673a\u5668\u8bbf\u95ee\u6570\u636e\u5e93dbname\uff0c\u4f60\u8981\u8fd0\u884c\uff1a<\/p>\n

    mysql> GRANT ALL ON dbname.* TO foo@'10.8.0.5' BY 'my_passwd'; <\/pre>\n

    \u914d\u7f6e\u9632\u706b\u5899<\/strong>
    \n\u6700\u540e\u4e00\u6b65\u662f\u914d\u7f6e\u9632\u706b\u5899\u914d\u7f6e\uff0c\u5141\u8bb8\u6765\u81ea\u8fdc\u7a0b\u673a\u5668\u7684\u901a\u8fc7\u7aef\u53e33306(MySQL\u9ed8\u8ba4\u7aef\u53e3)\u5165\u7ad9\u7684\u6d41\u91cf\u3002
    \nIptables<\/strong><\/p>\n

    \u5982\u679c\u4f60\u4f7f\u7528iptables\u4f5c\u4e3a\u9632\u706b\u5899\uff0c\u4ee5\u4e0b\u547d\u4ee4\u5c06\u5141\u8bb8\u4ece\u4e92\u8054\u7f51\u4e0a\u7684\u4efb\u4f55IP\u5730\u5740\u8bbf\u95eeMySQL\u7aef\u53e3\u3002\u8fd9\u5f88\u4e0d\u5b89\u5168\u3002<\/p>\n

    $ sudo iptables -A INPUT -p tcp --destination-port 3306 -j ACCEPT <\/pre>\n

    \u5141\u8bb8\u4ece\u7279\u5b9aIP\u5730\u5740\u8bbf\u95ee\uff1a<\/p>\n

    sudo iptables -A INPUT -s 10.8.0.5 -p tcp --destination-port 3306 -j ACCEPT <\/pre>\n
    UFW<\/strong><\/div>\n

    UFW\u662fUbuntu\u4e2d\u7684\u9ed8\u8ba4\u9632\u706b\u5899\u5de5\u5177\u3002\u8981\u5141\u8bb8\u4ece\u4e92\u8054\u7f51\u4e0a\u7684\u4efb\u4f55IP\u5730\u5740\u8fdb\u884c\u8bbf\u95ee(\u5f88\u4e0d\u5b89\u5168)\uff0c\u8bf7\u8fd0\u884c\uff1a<\/p>\n

    $ sudo ufw allow 3306\/ tcp <\/pre>\n

    \u5141\u8bb8\u4ece\u7279\u5b9aIP\u5730\u5740\u8bbf\u95ee\uff1a<\/p>\n

    sudo ufw allow from 10.8.0.5 to any port 3306 <\/pre>\n
    FirewallD<\/strong><\/div>\n

    FirewallD\u662fCentOS\u4e2d\u7684\u9ed8\u8ba4\u9632\u706b\u5899\u7ba1\u7406\u5de5\u5177\u3002\u8981\u5141\u8bb8\u4ece\u4e92\u8054\u7f51\u4e0a\u7684\u4efb\u4f55IP\u5730\u5740\u8fdb\u884c\u8bbf\u95ee(\u5f88\u4e0d\u5b89\u5168)\uff0c\u8bf7\u8f93\u5165\uff1a<\/p>\n

    $ sudo firewall-cmd --permanent --zone = public --add-port = 3306\/ tcp  \r\n$ sudo firewall-cmd --reload <\/pre>\n

    \u8981\u5141\u8bb8\u4ece\u7279\u5b9a\u7aef\u53e3\u4e0a\u7684\u7279\u5b9aIP\u5730\u5740\u8fdb\u884c\u8bbf\u95ee\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u65b0\u7684FirewallD\u533a\u57df\u6216\u4f7f\u7528\u4e30\u5bcc\u7684\u89c4\u5219\u3002\u4e0d\u59a8\u521b\u5efa\u4e00\u4e2a\u540d\u4e3amysqlzone\u7684\u65b0\u533a\u57df\uff1a<\/p>\n

    \r\n$ sudo firewall-cmd --new-zone=mysqlzone --permanent  \r\n$ sudo firewall-cmd --reload  \r\n$ sudo firewall-cmd --permanent --zone=mysqlzone --add-source=10.8.0.5\/32  \r\n$ sudo firewall-cmd --permanent --zone=mysqlzone --add-port=3306\/tcp  \r\n$ sudo firewall-cmd --reload <\/pre>\n
    \u9a8c\u8bc1\u66f4\u6539<\/strong><\/div>\n

    \u8981\u9a8c\u8bc1\u8fdc\u7a0b\u7528\u6237\u53ef\u4ee5\u8fde\u63a5\u5230MySQL\u670d\u52a1\u5668\uff0c\u8bf7\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n

    mysql -u user_name -h mysql_server_ip -p <\/pre>\n

    \u5176\u4e2duser_name\u662f\u4f60\u6388\u4e88\u8bbf\u95ee\u6743\u9650\u7684\u7528\u6237\u7684\u540d\u79f0\uff0cmysql_server_ip\u662f\u8fd0\u884cMySQL\u670d\u52a1\u5668\u7684\u4e3b\u673a\u7684IP\u5730\u5740\u3002
    \n\u5982\u679c\u4e00\u5207\u8bbe\u7f6e\u6b63\u786e\uff0c\u4f60\u5c31\u80fd\u591f\u767b\u5f55\u5230\u8fdc\u7a0bMySQL\u670d\u52a1\u5668\u3002
    \n\u5982\u679c\u4f60\u6536\u5230\u5982\u4e0b\u9519\u8bef\uff0c\u7aef\u53e33306\u672a\u6253\u5f00\uff0c\u6216\u8005MySQL\u670d\u52a1\u5668\u672a\u4fa6\u542cIP\u5730\u5740\u3002<\/p>\n

    ERROR 2003 (HY000): Can't connect to MySQL server on '10.8.0.5' (111)\" <\/pre>\n

    \u4ee5\u4e0b\u9519\u8bef\u8868\u660e\u4f60\u5c1d\u8bd5\u767b\u5f55\u7684\u7528\u6237\u6ca1\u6709\u8bbf\u95ee\u8fdc\u7a0bMySQL\u670d\u52a1\u5668\u7684\u6743\u9650\u3002<\/p>\n

    \r\n\"ERROR 1130 (HY000): Host \u201810.8.0.5\u2019 is not allowed to connect to this MySQL server\" <\/pre>\n

    MySQL\u662f\u9ed8\u8ba4\u6d41\u884c\u7684\u5f00\u6e90\u6570\u636e\u5e93\u670d\u52a1\u5668\uff0c\u5b83\u4ec5\u4fa6\u542c\u6765\u81ealocalhost\u7684\u5165\u7ad9\u8fde\u63a5\u3002
    \n\u8981\u5141\u8bb8\u8fdc\u7a0b\u8fde\u63a5\u5230MySQL\u670d\u52a1\u5668\uff0c\u4f60\u9700\u8981\u6267\u884c\u4ee5\u4e0b\u6b65\u9aa4\uff1a
    \n1. \u914d\u7f6eMySQL\u670d\u52a1\u5668\uff0c\u4fa6\u542c\u6240\u6709\u63a5\u53e3\u6216\u7279\u5b9a\u63a5\u53e3\u3002
    \n2. \u6388\u4e88\u8fdc\u7a0b\u7528\u6237\u8bbf\u95ee\u6743\u9650\u3002
    \n3. \u6253\u5f00\u9632\u706b\u5899\u4e2d\u7684MySQL\u7aef\u53e3\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

    \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cMySQL\u670d\u52a1\u5668\u4ec5\u4fa6\u542c\u6765\u81ealocalhost\u7684\u8fde\u63a5\uff0c\u8fd9\u610f\u5473\u7740\u53ea\u80fd\u7531\u8fd0\u884c\u5728\u540c\u4e00\u4e3b\u673a\u4e0a\u7684\u5e94\u7528\u7a0b\u5e8f\u8bbf\u95ee\u5b83 […]<\/p>\n","protected":false},"author":1469,"featured_media":164485,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-164484","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/164484","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/1469"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=164484"}],"version-history":[{"count":12,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/164484\/revisions"}],"predecessor-version":[{"id":165574,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/164484\/revisions\/165574"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/164485"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=164484"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=164484"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=164484"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}