centos7安装的mysql怎么远程连接

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了centos7安装的mysql怎么远程连接相关的知识,希望对你有一定的参考价值。

1.安装mysql
CentOS7中默认安装了MySQL系列的数据库,需要先卸载掉。
查看当前安装的mariadb包:
[root@bogon desktop]# rpm -qa | grep mariadb
将它们统统强制性卸载掉:
[root@bogon 桌面]# rpm -e --nodeps mariadb-libs-5.5.35-3.el7.x86_64
在官网下载RPM安装包,http://dev.mysql.com/downloads/mysql/,下载完成后解压。
双击安装MySQL-server,MySQL-client和MySQL-devel三个安装包。

2. 启动MYSQL
[root@bogon desktop]#service mysql start
得到错误:ERROR!The server quit without updating PID file
我们这里主要是因为:selinux惹的祸,如果是centos系统,默认会开启selinux。解决方法是关闭它,打开/etc/selinux/config,把SELINUX=enforcing改为SELINUX=disabled后存盘退出重启机器。
然后再启动mysql就没问题了:
[root@bogon desktop]#service mysql start
查看MySQL运行状态:
[root@bogon desktop]# service mysql status
SUCCESS! MySQL running (2377)

3. 设置密码
安装之后root用户默认没有密码,通过命令设置密码。
[root@bogon desktop]# /usr/bin/mysqladmin -u root password \'passok\'
然后设置root用户的远程访问权限
在mysql控制台执行
GRANT ALL PRIVILEGES ON *.* TO \'root\'@\'%\' IDENTIFIED BY \'MyPassword\' WITH GRANT OPTION;
FLUSH PRIVILEGES;

4.开启远程连接
需要安装iptables,
yum install -y iptables
然后编辑系统的开放端口列表,增加3306端口,重启防火墙即可。
参考技术A 1)mysql -u root -p password ;use mysql ;
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION; 允许任何ip以root用户登录
flush privileges;立即生效
2)修改my.cnf文件 注释掉bindaddress这句话,如果有的话
3)检查防火墙
systemctl status iptables.service /firewalld.service 并关闭
另外 如果不想关闭防火墙,那么可以添加规则,下面是使用命令添加
1)先确保防火墙已启动
2)执行命令 firewall-cmd --permanent(永久) --add-port=3306(默认端口)/tcp(协议) 响应 success
3) 执行命令查看规则添加是否成功 firewall-cmd --permanent --query-port=3306/tcp
4)重启防火墙生效 systemctl restart mariadb.service本回答被提问者采纳

解决linux(debian10)中mysql无法远程连接

Centos8今年底停止维护,Centos7到2024年维护期限到达之后停止维护。作为还在使用 Centos的运维,还是有些小伤心呢。白嫖党怎么办?有人说,Debian才是王道。Debian是啥,没怎么听说啊。装个虚拟机玩玩,再装个基本的应用比如Mysql试试。安装教程(教程见引用)一步一步走,安装Debian、Mysql基本都很顺利,就是Mysql无法开启远程连接。怎么解决?


1. 网络通不通?


   ping下呢,通的!


   telnet呢,不通!要解决的就是这个问题。


2. Mysql是否正常运行?


# systemctl status mysql● mysql.service - MySQL Community Server Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled) Active: active (running) since Thu 2021-04-01 10:06:14 CST; 7s ago Process: 72107 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS) Process: 72142 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid (code=exited, status=0/SUCCESS)Main PID: 72144 (mysqld) Tasks: 27 (limit: 2330) Memory: 181.1M CGroup: /system.slice/mysql.service └─72144 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
40110:06:12 debian systemd[1]: Starting MySQL Community Server...40110:06:14 debian systemd[1]: Started MySQL Community Server.


   正常的啊


3. 端口在监听吗?

# netstat -ntlp | grep 3306tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 72144/mysqld

   在监听啊


4. 防火墙呢?

# systemctl status firewalld●firewalld.service - firewalld - dynamic firewall daemon Loaded: loaded (/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled) Active: active (running) since Thu 2021-04-01 11:35:14 CST; 19s ago Docs: man:firewalld(1)Main PID: 103561 (firewalld) Tasks: 2 (limit: 2330) Memory: 20.4M CGroup: /system.slice/firewalld.service └─103561 /usr/bin/python3 /usr/sbin/firewalld --nofork --nopid
4月01 11:35:14 debian systemd[1]: Starting firewalld - dynamic firewall daemon...4月01 11:35:14 debian systemd[1]: Started firewalld - dynamic firewall daemon.

   开启的,怎么办?两种处理方式:


   ①关掉防火墙;

# systemctl stop firewalld

   ②加策略;也有两种方式


   第一种:开放服务

# firewall-cmd --zone=public --add-service=mysql --permanent //开放mysql服务# firewall-cmd --reload //重载生效success# firewall-cmd --zone=public --list-services //查看添加效果dhcpv6-client mysql ssh

   第二种:开放端口


# firewall-cmd --zone=public --query-port=3306/tcp --permanent //开放3306端口# firewall-cmd --reload //重载生效success# firewall-cmd --zone=public --list-ports //查看添加效果3306/tcp

   再试下呢,还是不行!


5. Mysql允许远程登录不?

# mysql -uroot -p //登录mysqlEnter password:Welcome to the MySQL monitor. Commands endwith ; or \g.Your MySQL connection id is 4Server version:5.7.33 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/orits affiliates.
Oracle is a registered trademark of Oracle Corporation and/oritsaffiliates. Other names may be trademarks of their respectiveowners.
Type 'help;'or '\h' for help. Type '\c' to clear the current input statement.
mysql> use mysql; Reading table information forcompletion of table and column namesYou can turn off this feature to get a quicker startup with -A
Database changed
mysql> grant all privileges on *.* to root@'%'identified by 'xxxxxxxx' with grant option;Query OK, 0rows affected, 1 warning (0.00 sec)
mysql> flush privileges;Query OK, 0rows affected (0.00 sec)
mysql> select host,user from user;+-----------+---------------+| host |user |+-----------+---------------+|% | root || localhost |mysql.session ||localhost | mysql.sys || localhost |root |+-----------+---------------+4 rows inset (0.00 sec)

   再试下呢,还是不行!


6. Mysql配置不对吗?

# vim /etc/mysql/my.cnf

    并没有百度各种答案里配置的bind-address = 127.0.0.1啊,没有就加上bind-address = 0.0.0.0再试试,还是不行!


   注:修改配置文件之后记得

# systemctl restart firewalld

7. Mysql安装安全加固的问题?


   哦哦哦,想起Debian在线安装mysql的时候比Centos多执行了一个命令

# sudo mysql_secure_installationSecuring the MySQL server deployment.
Enter password for user root:The 'validate_password' plugin is installed on the server.The subsequent steps will run withthe existing configurationofthe plugin.Usingexisting password for root.
Estimated strength ofthe password: 50Changethe password for root ? ((Press y|Y for Yes, any other key for No) : n
... skipping.Bydefault, a MySQL installation has an anonymous user,allowing anyone tolog into MySQL without having to havea useraccount created for them. This is intended only fortesting, andto make the installation go a bit smoother.You should remove them beforemoving into a productionenvironment.
Remove anonymous users? (Press y|Y forYes, any other key for No) : ySuccess.

Normally, root should onlybe allowed to connect from'localhost'. This ensures that someone cannot guess atthe root passwordfrom the network.
Disallowroot login remotely? (Press y|Y for Yes, any other key for No) : n
... skipping.Bydefault, MySQL comes with a database named 'test' thatanyone can access. This isalso intended only for testing,andshould be removed before moving into a productionenvironment.

Remove testdatabase and access to it? (Press y|Y for Yes, any other key for No) : n
... skipping.Reloading the privilege tableswill ensure that all changesmade so farwill take effect immediately.
Reload privilege tablesnow? (Press y|Y for Yes, any other key for No) : n
... skipping.Alldone!

   其中有个问题“Disallow root login remotely? (Press y|Y for Yes, any other key for No) : n”我明明选择的是No啊,怎么还是不行。难道是我搞错了?再试几遍呢,还是不行!法克!


   注:重新配置之后记得重启


8. Mysql配置还是不对吗?


   经过上一步,发现Centos7安装和Debian10在线安装的Mysql还是有区别的。配置文件my.cnf位置不一样,Centos7的配置文件在/etc/下,而Debian10的配置文件/etc/mysql/下面。咦……,Debian10还专门给mysql建了配置文件目录啊,看看里面有啥

root@debian:/etc/mysql# ls -l总用量 16drwxr-xr-x 2root root 4096 331 15:32 conf.dlrwxrwxrwx 1root root 24 331 15:32 my.cnf -> /etc/alternatives/my.cnf-rw-r--r-- 1root root 839 83 2016 my.cnf.fallback-rw-r--r-- 1root root 1215 41 10:00 mysql.cnfdrwxr-xr-x 2root root 4096 41 10:05 mysql.conf.d

   这里面有两个目录conf.d、mysql.conf.d,两个cnf文件,一个.cnf.fallback文件。把所有文件全部打开看看

root@debian:/etc/mysql# cat ./conf.d/mysql.cnf[mysql]root@debian:/etc/mysql# cat ./conf.d/mysqldump.cnf[mysqldump]quickquote-namesmax_allowed_packet = 16Mroot@debian:/etc/mysql# cat my.cnf# Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.## This program is free software; you can redistribute it and/or modify# it under the terms of the GNU General Public License, version 2.0,# as published by the Free Software Foundation.## This program is also distributed with certain software (including# but not limited to OpenSSL) that is licensed under separate terms,# as designated in a particular file or component or in included license# documentation. The authors of MySQL hereby grant you an additional# permission to link the program and your derivative works with the# separately licensed software that they have included with MySQL.## This program is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU General Public License, version 2.0, for more details.## You should have received a copy of the GNU General Public License# along with this program; if not, write to the Free Software# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
!includedir /etc/mysql/conf.d/!includedir /etc/mysql/mysql.conf.d/root@debian:/etc/mysql# cat my.cnf.fallback## 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
!includedir /etc/mysql/conf.d/root@debian:/etc/mysql# cat mysql.cnf# Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.## This program is free software; you can redistribute it and/or modify# it under the terms of the GNU General Public License, version 2.0,# as published by the Free Software Foundation.## This program is also distributed with certain software (including# but not limited to OpenSSL) that is licensed under separate terms,# as designated in a particular file or component or in included license# documentation. The authors of MySQL hereby grant you an additional# permission to link the program and your derivative works with the# separately licensed software that they have included with MySQL.## This program is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU General Public License, version 2.0, for more details.## You should have received a copy of the GNU General Public License# along with this program; if not, write to the Free Software# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
!includedir /etc/mysql/conf.d/!includedir /etc/mysql/mysql.conf.d/root@debian:/etc/mysql# cat mysql.conf.d/mysqld.cnf# Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.## This program is free software; you can redistribute it and/or modify# it under the terms of the GNU General Public License, version 2.0,# as published by the Free Software Foundation.## This program is also distributed with certain software (including# but not limited to OpenSSL) that is licensed under separate terms,# as designated in a particular file or component or in included license# documentation. The authors of MySQL hereby grant you an additional# permission to link the program and your derivative works with the# separately licensed software that they have included with MySQL.## This program is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU General Public License, version 2.0, for more details.## You should have received a copy of the GNU General Public License# along with this program; if not, write to the Free Software# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
## The MySQL Server configuration file.## For explanations see# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
[mysqld]pid-file = /var/run/mysqld/mysqld.pidsocket = /var/run/mysqld/mysqld.sockdatadir = /var/lib/mysqllog-error = /var/log/mysql/error.log# By default we only accept connections from localhostbind-address = 127.0.0.1# Disabling symbolic-links is recommended to prevent assorted security riskssymbolic-links=0root@debian:/etc/mysql#

   ./my.cnf == ./mysql.cnf,内容完全一样。**里面配置了两个目录conf.d、mysql.conf.d,里面包含./conf.d/mysql.cnf、./conf.d/mysqldump.cnf、./mysql.conf.d/mysqld.cnf,这三个文件的内容才是Debian10下mysql的全部配置内容**。在./mysql.conf.d/mysqld.cnf中配置了bind-address = 127.0.0.1,而且这是默认配置。修改为bind-address = 0.0.0.0 或者直接注释掉该行。再试下呢,终于成功了!!!


注:修改配置文件之后永远要记得重启服务

以上是关于centos7安装的mysql怎么远程连接的主要内容,如果未能解决你的问题,请参考以下文章

CentOS7下安装MySQL并配置远程连接

ubuntu怎么连接centos远程桌面

centos怎么开启远程桌面连接

centos7 服务器安装mysql 并支持远程连接

Windows远程桌面连接Centos7

解决linux(debian10)中mysql无法远程连接