Mac下VMWare网络配置和Centos8下Mysql8远程访问

in #vmware4 years ago

最近需要用下mysql8,本机上有个mysql5,因为有一堆乱七八糟数据不想升级数据库,弄不好又得调整半天,想着在VMWare虚拟机的Centos8中装个mysql8,再提供外网访问权限,本来是挺简单的一个事情,由于VMware的一些设置,Centos8、mysql8的一些命令和语法不一样,还是花了一些时间才处理好。

主要几个知识点:

  1. 修改VMWare虚拟网卡缺省IP

  2. Centos8连接互联网

  3. mysql8远程访问

修改VMWare缺省IP

为什么要修改VMWare的IP呢?是因为Mac使用了192.168.1.x相关ip,与VMWare有冲突,当启动VMWare后即无法访问相关内网,也无法ping通。

VMWare有3个已经默认分配给网桥(VMnet0),宿主机虚拟网络适配器(VMnet1)和网络地址转换NAT设备(VMnet8)。

未启动VMWare时能ping通内网DNS

Chaim:Library Chaim$ ping 192.168.1.217
PING 192.168.1.217 (192.168.1.217): 56 data bytes
64 bytes from 192.168.1.217: icmp_seq=0 ttl=61 time=3.604 ms
64 bytes from 192.168.1.217: icmp_seq=1 ttl=61 time=3.558 ms

启动VMWare后,无法ping通DNS

Chaim:Library Chaim$ ping 192.168.1.217
PING 192.168.1.217 (192.168.1.217): 56 data bytes
Request timeout for icmp_seq 0

ifconfig显示如下:

Chaim:Library Chaim$ ifconfig
vmnet1: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
    ether 00:50:56:c0:00:01 
    inet 192.168.54.1 netmask 0xffffff00 broadcast 192.168.54.255
vmnet8: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
    ether 00:50:56:c0:00:08 
    inet 192.168.1.1 netmask 0xffffff00 broadcast 192.168.1.255

主要多了vmnet相关网卡,并且占用了192.168.1.x段

修改networking

sudo vim /Library/Preferences/VMware\ Fusion/networking
VERSION=1,0
answer VNET_1_DHCP yes
answer VNET_1_DHCP_CFG_HASH 60CB29F5143068977275725F80691DF76093C7CF
answer VNET_1_HOSTONLY_NETMASK 255.255.255.0
answer VNET_1_HOSTONLY_SUBNET 192.168.54.0
answer VNET_1_VIRTUAL_ADAPTER yes
answer VNET_3_DHCP no
answer VNET_3_DISPLAY_NAME vmnet
answer VNET_3_VIRTUAL_ADAPTER no
answer VNET_8_DHCP yes
answer VNET_8_DHCP_CFG_HASH BD1509643AE2072D0EBFCD07774927670FAD89BC
answer VNET_8_HOSTONLY_NETMASK 255.255.255.0
answer VNET_8_HOSTONLY_SUBNET 192.168.1.0
answer VNET_8_NAT yes
answer VNET_8_VIRTUAL_ADAPTER yes
add_bridge_mapping en0 2

修改192.168.x.x为172.28.x.x

修改vmnet1

sudo vim /Library/Preferences/VMware\ Fusion/vmnet1/dhcpd.conf
# Configuration file for ISC 2.0 vmnet-dhcpd operating on vmnet1.
# 
# This file was automatically generated by the VMware configuration program.
# See Instructions below if you want to modify it.
#
# We set domain-name-servers to make some DHCP clients happy
# (dhclient as configured in SuSE, TurboLinux, etc.).
# We also supply a domain name to make pump (Red Hat 6.x) happy.
#


###### VMNET DHCP Configuration. Start of "DO NOT MODIFY SECTION" #####
# Modification Instructions: This section of the configuration file contains
# information generated by the configuration program. Do not modify this
# section.
# You are free to modify everything else. Also, this section must start
# on a new line
# This file will get backed up with a different name in the same directory
# if this section is edited and you try to configure DHCP again.

# Written at: 10/30/2020 17:30:07
allow unknown-clients;
default-lease-time 1800;                # default is 30 minutes
max-lease-time 7200;                    # default is 2 hours

subnet 192.168.54.0 netmask 255.255.255.0 {
        range 192.168.54.128 192.168.54.254;
        option broadcast-address 192.168.54.255;
        option domain-name-servers 192.168.54.1;
        option domain-name localdomain;
        default-lease-time 1800;                # default is 30 minutes
        max-lease-time 7200;                    # default is 2 hours
}
host vmnet1 {
        hardware ethernet 00:50:56:C0:00:01;
        fixed-address 192.168.54.1;
        option domain-name-servers 0.0.0.0;
        option domain-name "";
}
####### VMNET DHCP Configuration. End of "DO NOT MODIFY SECTION" #######

将192.168.x.x修改成172.28.x.x

修改vmnet8

sudo vim /Library/Preferences/VMware\ Fusion/vmnet8/dhcpd.conf
# Configuration file for ISC 2.0 vmnet-dhcpd operating on vmnet8.
#
# This file was automatically generated by the VMware configuration program.
# See Instructions below if you want to modify it.
#
# We set domain-name-servers to make some DHCP clients happy
# (dhclient as configured in SuSE, TurboLinux, etc.).
# We also supply a domain name to make pump (Red Hat 6.x) happy.
#


###### VMNET DHCP Configuration. Start of "DO NOT MODIFY SECTION" #####
# Modification Instructions: This section of the configuration file contains
# information generated by the configuration program. Do not modify this
# section.
# You are free to modify everything else. Also, this section must start 
# on a new line 
# This file will get backed up with a different name in the same directory 
# if this section is edited and you try to configure DHCP again.

# Written at: 10/30/2020 17:30:07
allow unknown-clients;
default-lease-time 1800;                # default is 30 minutes
max-lease-time 7200;                    # default is 2 hours

subnet 192.168.1.0 netmask 255.255.255.0 {
    range 192.168.1.128 192.168.1.254;
    option broadcast-address 192.168.1.255;
    option domain-name-servers 192.168.1.2;
    option domain-name localdomain;
    default-lease-time 1800;                # default is 30 minutes
    max-lease-time 7200;                    # default is 2 hours
    option netbios-name-servers 192.168.1.2;
    option routers 192.168.1.2;
}
host vmnet8 {
    hardware ethernet 00:50:56:C0:00:08;
    fixed-address 192.168.1.1;
    option domain-name-servers 0.0.0.0;
    option domain-name "";
    option routers 0.0.0.0;
}
####### VMNET DHCP Configuration. End of "DO NOT MODIFY SECTION" #######

将192.168.x.x修改成172.28.x.x

修改vmnet8/nat

sudo vim /Library/Preferences/VMware\ Fusion/vmnet8/nat.conf
# VMware NAT configuration file
# Manual editing of this file is not recommended. Using UI is preferred.

[host]

# NAT gateway address
ip = 192.168.1.2
netmask = 255.255.255.0

# VMnet device if not specified on command line
device = vmnet8

# Allow PORT/EPRT FTP commands (they need incoming TCP stream ...)
activeFTP = 1

# Allows the source to have any OUI.  Turn this on if you change the OUI
# in the MAC address of your virtual machines.
allowAnyOUI = 1

# Controls if (TCP) connections should be reset when the adapter they are
# bound to goes down
resetConnectionOnLinkDown = 1

# Controls if (TCP) connection should be reset when guest packet's destination
# is NAT's IP address
resetConnectionOnDestLocalHost = 1

# Controls if enable nat ipv6
natIp6Enable = 0

# Controls if enable nat ipv6
natIp6Prefix = fd15:4ba5:5a2b:1008::/64

[tcp]

# Value of timeout in TCP TIME_WAIT state, in seconds
timeWaitTimeout = 30

[udp]

# Timeout in seconds. Dynamically-created UDP mappings will purged if
# idle for this duration of time 0 = no timeout, default = 60; real
# value might be up to 100% longer
timeout = 60

[netbios]
# Timeout for NBNS queries.
nbnsTimeout = 2

# Number of retries for each NBNS query.
nbnsRetries = 3

# Timeout for NBDS queries.
nbdsTimeout = 3

[incomingtcp]

# Use these with care - anyone can enter into your VM through these...
# The format and example are as follows:
#<external port number> = <VM's IP address>:<VM's port number>
#8080 = 172.16.3.128:80

[incomingudp]

# UDP port forwarding example
#6000 = 172.16.3.0:6001

同样把网关改为172.28.x.x

ip = 172.28.1.2

nat模式的网关,请务必保持其值与同目录下的dhcpd.conf文件内的网关保持一致。

重启VMWare相关网络

sudo /Applications/VMware\ Fusion.app/Contents/Library/vmnet-cli --stop
sudo /Applications/VMware\ Fusion.app/Contents/Library/vmnet-cli --start

查看ifconfig

vmnet1: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
    ether 00:50:56:c0:00:01 
    inet 172.28.54.1 netmask 0xffffff00 broadcast 172.28.54.255
vmnet8: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
    ether 00:50:56:c0:00:08 
    inet 172.28.1.1 netmask 0xffffff00 broadcast 172.28.1.255

确认虚拟网卡地址已改变,这样Mac的192.168段就不会被占用了,内网也能正常访问了!

Chaim:Library Chaim$ ping 192.168.1.217
PING 192.168.1.217 (192.168.1.217): 56 data bytes
64 bytes from 192.168.1.217: icmp_seq=0 ttl=61 time=3.041 ms

Centos8连网

装上Centos8后无法上网,用哪种模式都不行。

修改网卡IP

cd /etc/sysconfig/network-scriipts
vi ifcfg-ens33

增加IP相关段

TYPE=Ethernet
BOOTPROTO=none  这个地方改为static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
NAME=eno16777736    这个名字如果是其他的重复修改为不重复的
UUID=ed15885f-d297-4791-a238-6894bcfd1c13
ONBOOT=yes     这个地方改为yes
IPADDR0=172.28.1.188  这个地方按照DHCP里面的起始IP范围改相应的IP
NETMASK=255.255.255.0
GATEWAY0=172.28.1.1  改为主机的IP,这里已改过
DNS1=192.168.171.1 
DNS2=191.168.171.1
HWADDR=00:0C:29:26:D8:A1  这个地方是物理网卡地址,后面如果是是克隆的虚拟机再讲
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes

重启网卡

nmcli c reload

使用NAT模式、桥接模式都能上网了。


开启防火墙

开启mysql端口3306

firewall-cmd --zone=public --add-port=3306/tcp --permanent

重新载入一下防火墙设置,使设置生效

firewall-cmd --reload

Mysql8远程访问

建立用户

create user 'web'@'%' identified by '123457';
flush privileges;

mysql8中已经不支持grant all privileges on . to 'root'@'%' identified by '密码' with grant option这种写法。

应该使用

grant all privileges on *.* to 'web'@'%' ;

结论

通过以上几步操作,解决了以前虚拟机启动就不能访问内网192.168.1.x的问题,从Mac上可以访问虚拟机Centos8下的Mysql8,根据VMWare网络不同,也可以从局域网内访问。


参考

VMware for mac 如何更改为固定ip实现上网

https://www.cnblogs.com/qiang-qiang/p/10411100.html

VMware虚拟机在局域网联网的设置方法

https://www.cnblogs.com/xuedexin/articles/5377401.html

https://www.cnblogs.com/leozhanggg/p/11772251.html

添加网卡

https://blog.csdn.net/az44yao/article/details/86519243

Linux系统通过firewall限制或开放IP及端口

https://blog.csdn.net/ywd1992/article/details/80401630