适合:智能合约开发、DApp 本地调试、新手入门
1. 安装依赖
安装 Node.js (v18+):https://nodejs.org/
安装 Ganache
bash运行
npm install -g ganache
如果太慢 切换源
npm config set registry https://registry.npmmirror.com/
2. 启动本地链
bash运行
# 命令行版
ganache --chain.chainId 1337 --wallet.totalAccounts 10
# 或启动GUI版(推荐)
ganache ui
RPC 地址:http://127.0.0.1:8545 (或 7545)
Chain ID:1337
自动生成 10 个账户,每个 1000 ETH 测试币
Ganache界面
3. 连接 MetaMask 钱包
打开 MetaMask → 网络 → 添加网络
填写:
网络名称:Localhost 8545
新 RPC URL:http://127.0.0.1:8545
链 ID:1337
符号:ETH
浏览器:(留空)
导入 Ganache 显示的私钥到 MetaMask,即可使用测试币。
方法二:GUI 桌面版(完全不用命令行,零等待)
如果你只是用来做区块链测试,直接下载桌面版,跳过命令行安装,最省心:
Ganache 桌面版下载地址:
https://trufflesuite.com/ganache/
下载安装后双击打开就能用,自带图形界面,比命令行版更好用。
MetaMask 导入账户(找不到入口看这里)
打开 MetaMask 小狐狸
点右上角头像(圆形图标)MetaMask Help Center
下拉到底 → 点 Add account or hardware wallet(添加账户 / 硬件钱包)MetaMask Help Center
在下一屏 → 点 Import account(导入账户)MetaMask Help Center
粘贴你从 Ganache 复制的 私钥 → 点 ImportMetaMask Help Center
成功后会显示 Imported 标签,余额 100 ETHMetaMask Help Center
进阶
方案 3:多节点私有链(Geth,真实网络模拟)
适合:测试共识机制(PoA/PoS)、节点发现、P2P 网络、跨节点交易
1. 安装 Geth
- 下载:https://geth.ethereum.org/downloads/
- 验证:geth version
2. 编写创世区块(genesis.json)
json
{ "config": { "chainId": 12345, "homesteadBlock": 0, "eip155Block": 0, "eip158Block": 0, "clique": { "period": 2, // 出块间隔(秒) "epoch": 30000 } }, "difficulty": "0x1", "gasLimit": "0x8000000", "alloc": { "你的钱包地址": { "balance": "1000000000000000000000" } } }
3. 初始化节点
bash
运行
# 创建数据目录 mkdir -p node1 node2 # 初始化节点1 geth --datadir ./node1 init genesis.json # 初始化节点2 geth --datadir ./node2 init genesis.json
4. 启动节点(PoA 共识)
bash
运行
# 节点1(矿工) geth --datadir ./node1 --networkid 12345 --port 30303 --http --http.addr 0.0.0.0 --http.port 8545 --http.api eth,net,web3,personal --mine --miner.threads 1 --allow-insecure-unlock # 节点2(加入网络) geth --datadir ./node2 --networkid 12345 --port 30304 --http.port 8546 --bootnodes "节点1的enode地址"
5. 节点互联
- 在节点 1 控制台执行:admin.nodeInfo.enode
-
复制 enode 地址,在节点 2 执行:
javascript运行
admin.addPeer("enode://xxx@127.0.0.1:30303") admin.peers // 查看连接