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

最近需要用下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

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 213,047评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,807评论 3 386
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 158,501评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,839评论 1 285
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,951评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,117评论 1 291
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,188评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,929评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,372评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,679评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,837评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,536评论 4 335
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,168评论 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,886评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,129评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,665评论 2 362
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,739评论 2 351