在Ubuntu下搭建微服務開發環境

wangdandong發表於2017-11-17

關於本文

  • 寫作目標:作為在linux下安裝JDK、MySQL、Zookeeper、Redis、MongoDB、RabbitMQ等開發元件的快速參考指南
  • Linux版本:Linux Mint 18.2 Sonya(基於Ubuntu 16.04 Xenial)

實戰記錄

  • JAVA OpenJDK 1
sudo apt install openjdk-8-jdk
  • MySQL 23

安裝

wget https://dev.mysql.com/get/mysql-apt-config_0.8.6-1_all.deb
dpkg -i mysql-apt-config_0.8.6-1_all.deb
apt update
apt install mysql-server

配置

vi /etc/mysql/conf.d/mysql.cnf
[mysql]
default-character-set=utf8

vi /etc/mysql/mysql.conf.d/mysqld.cnf
[mysqld]
# 註釋如下配置以允許遠端訪問
# bind-address  = 127.0.0.1
character-set-server=utf8
default-time-zone=`+8:00`

# 重啟服務
sudo service mysql restart

常用命令

# 登入
mysql -uroot -p

# 建立使用者、資料庫
create user `test`@`%` identified by `123456`;
create database test;
grant all on test.* to `test`@`%`;
flush privileges;

# 重置root密碼
sudo service mysql stop
sudo mkdir -p /var/run/mysqld
sudo chown mysql:adm /var/run/mysqld
sudo nohup mysqld_safe --skip-grant-tables --skip-networking &
#進入mysql shell後用下面修改密碼的方式更改root密碼
mysql
update mysql.user set authentication_string=password(`123456`) where user=`root` and host = `localhost`;
#重啟服務
mysqladmin shutdown
sudo service mysql start

# 修改密碼
#方式一 (https://dev.mysql.com/doc/refman/5.7/en/password-hashing.html)
set password for `root`@`localhost`=password(`123456`);
flush privileges;
#方式二
update mysql.user set authentication_string=password(`123456`) where user=`root` and host = `localhost`;
flush privileges;

# 修改資料庫字符集
alter database test character set utf8;

# 檢視mysql字符集預設配置
show variables like `%character%`;

# 複製資料庫:將資料庫db1複製到資料庫db2 (https://dev.mysql.com/doc/refman/5.7/en/mysqldump-copying-database.html)
mysqldump -uroot -p db1 > dump.sql
mysqladmin -uroot -p create db2
mysql -uroot -p db2 < dump.sql
  • Zookeeper (啟動前必須先安裝好JDK)

從Ubuntu軟體倉庫安裝 4

sudo apt install zookeeperd

從zookeeper官網下載(推薦此方式,可使用最新穩定版) 5

#切換到root使用者
su -
cd /usr/local/share
wget http://mirrors.aliyun.com/apache/zookeeper/zookeeper-3.4.10/zookeeper-3.4.10.tar.gz
tar xzf zookeeper-3.4.10.tar.gz
cd zookeeper-3.4.10
mv zookeeper-3.4.10 zookeeper
cp conf/zoo_sample.cfg conf/zoo.cfg
vi conf/zoo.cfg # 修改 dataDir=/var/lib/zookeeper

# 啟動服務
bin/zkServer.sh start

# 安裝為系統自啟服務 (https://issues.apache.org/jira/browse/ZOOKEEPER-2095)
vi /lib/systemd/system/zookeeper.service
---------------------------------------------------------
[Unit]
Description=Apache Zookeeper
After=network.target

[Service]
Type=forking
SyslogIdentifier=zookeeper
Restart=always
RestartSec=0s
ExecStart=/usr/local/share/zookeeper/bin/zkServer.sh start
ExecStop=/usr/local/share/zookeeper/bin/zkServer.sh stop
ExecReload=/usr/local/share/zookeeper/bin/zkServer.sh restart

[Install]
WantedBy=multi-user.target
---------------------------------------------------------

systemctl start zookeeper
systemctl enable zookeeper
  • Redis

從Ubuntu軟體倉庫安裝 6

sudo apt install redis-server

從redis官網下載(推薦此方式,可使用最新穩定版) 7

#切換到root使用者
su -
cd /usr/local/share
wget http://download.redis.io/releases/redis-3.2.9.tar.gz
tar xzf redis-3.2.9.tar.gz
cd redis-3.2.9
make

# 啟動服務
nohup src/redis-server redis.conf > redis-server.out &

# 或者安裝為系統自啟服務 (設定 redis executable path 時指定 src/redis-server 所在的完整路徑)
utils/install_server.sh
/etc/init.d/redis_6379 start
update-rc.d redis_6379 defaults

配置 8

# 使用場景舉例:允許內網環境中其他機器訪問時,假設redis安裝機器IP為172.18.1.10,可按如下配置
vi redis.conf
bind 127.0.0.1 172.18.1.10
# 使用redis-cli進行訪問驗證 (redis-cli與redis-server在同一個目錄)
redis-cli -h 172.18.1.10 -p 6379

# 使用場景舉例:開啟遠端訪問 (註釋掉bind配置項,修改requirepass、masterauth配置項)
# 務必設定非常複雜、長度很大的密碼
vi redis.conf
#bind 127.0.0.1
# 在master例項修改requirepass配置項
requirepass Fmg1$7!&bcNp@iY0tW6pEVPP!FZ9vHMd5a%TOJAGI8bO7qqT1%
# 在slave例項修改masterauth配置項
masterauth Fmg1$7!&bcNp@iY0tW6pEVPP!FZ9vHMd5a%TOJAGI8bO7qqT1%

# 驗證授權
# ./redis-cli -a `Fmg1$7!&bcNp@iY0tW6pEVPP!FZ9vHMd5a%TOJAGI8bO7qqT1%`

# 如果啟用密碼訪問,須要修改/etc/init.d/redis_6379
$CLIEXEC -p $REDISPORT -a `Fmg1$7!&bcNp@iY0tW6pEVPP!FZ9vHMd5a%TOJAGI8bO7qqT1%` shutdown

# 修改完配置後注意重啟服務
/etc/init.d/redis_6379 restart
  • MongoDB 9

安裝

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
# 使用阿里雲映象站,加快下載速度
sudo echo "deb http://mirrors.aliyun.com/mongodb/apt/ubuntu xenial/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list
sudo apt-get update
sudo apt-get install -y mongodb-org

配置 10111213

  • 預設配置檔案路徑是/etc/mongod.conf
  • 如果想為mongos或mongod繫結多個監聽的IP地址,可將net.bindIp設定為英文逗號分割的多個IP地址
# 使用場景舉例:允許內網環境中其他機器訪問時,假設mongod安裝機器IP為172.18.1.10,可按如下配置
vi /etc/mongod.conf
net:
  bindIp: 127.0.0.1,172.18.1.10

# 使用場景舉例:開啟遠端訪問 (註釋net.bindIp配置項,新增security.authorization配置項;建立授權使用者)
vi /etc/mongod.conf
net:
  # bindIp: 127.0.0.1
security:
  authorization: enabled

mongo
use admin
db.createUser(
  {
    user: "root",
    pwd: "123456",
    roles: [ { role: "root", db: "admin" } ]
  }
)
use test
db.createUser(
  {
    user: "test",
    pwd: "123456",
    roles: [ { role: "dbOwner", db: "test" } ]
  }
)

# 重啟服務以使配置生效
sudo service mongod restart

# 開啟授權後的登入舉例,特別注意authenticationDatabase引數的使用,它與使用者所授權角色對應的資料庫有關,root角色擁有所有庫的所有操作許可權,dbOwner角色只有授權庫的所有操作許可權
mongo localhost:27017/test -u "root" -p "123456" --authenticationDatabase "admin"
mongo localhost:27017/test -u "test" -p "123456" --authenticationDatabase "test"
  • RabbitMQ 14

使用PackageCloud APT Repository進行安裝 15

curl -s https://packagecloud.io/install/repositories/rabbitmq/rabbitmq-server/script.deb.sh | sudo bash

sudo apt-get install rabbitmq-server

使用RabbitMQ APT Repository進行安裝 16

echo `deb http://www.rabbitmq.com/debian/ testing main` | sudo tee /etc/apt/sources.list.d/rabbitmq.list

wget -O- https://www.rabbitmq.com/rabbitmq-release-signing-key.asc | sudo apt-key add -
或者
wget -O- https://dl.bintray.com/rabbitmq/Keys/rabbitmq-release-signing-key.asc | sudo apt-key add -

sudo apt-get update
sudo apt-get install rabbitmq-server

配置 17181920

  • 配置所在路徑:在/var/log/rabbitmq/rabbit@{計算機名}.log檢視;

預設情況rabbitmq.config、rabbitmq-env.conf在目錄/etc/rabbitmq/中,如果檔案不存在可自行建立,
rabbitmq.config配置示例:/usr/share/doc/rabbitmq-server/rabbitmq.config.example.gz或者訪問rabbitmq.config.example
也可通過設定系統環境變數RABBITMQ_CONFIG_FILE、RABBITMQ_CONF_ENV_FILE指定具體路徑;
修改完成後須要重啟rabbimq-server服務;
預設配置如下,注意RABBITMQ_CONFIG_FILE配置路徑不用新增.config字尾,Erlang執行時會自動追加:

RABBITMQ_CONFIG_FILE=/etc/rabbitmq/rabbitmq
RABBITMQ_CONF_ENV_FILE=/etc/rabbitmq/rabbitmq-env.conf
  • 預設賬號: 使用者名稱密碼都是guest

預設只能通過loopback地址(例如localhost)連線guest賬號,如果想允許遠端訪問,須要修改rabbitmq.config(將loopback_users配置項設為[]):[{rabbit, [{loopback_users, []}]}].

# 建立新使用者,增加許可權
sudo rabbitmqctl add_user test 123456
sudo rabbitmqctl set_permissions -p / test ".*" ".*" ".*"
sudo rabbitmqctl set_user_tags test administrator
# 驗證賬號
sudo rabbitmqctl authenticate_user test 123456

# 修改密碼 
sudo rabbitmqctl change_password test 123456

(本文首發我的個人網站 http://www.wangdandong.com )


  1. http://openjdk.java.net/insta…
  2. https://dev.mysql.com/doc/mys…
  3. https://dev.mysql.com/downloa…
  4. https://launchpad.net/ubuntu/…
  5. http://zookeeper.apache.org/d…
  6. https://launchpad.net/ubuntu/…
  7. https://redis.io/download
  8. https://redis.io/commands/auth
  9. https://docs.mongodb.com/v3.2…
  10. https://docs.mongodb.com/v3.2…
  11. https://docs.mongodb.com/v3.2…
  12. https://docs.mongodb.com/v3.2…
  13. https://docs.mongodb.com/v3.2…
  14. http://www.rabbitmq.com/downl…
  15. https://packagecloud.io/rabbi…
  16. http://www.rabbitmq.com/insta…
  17. http://www.rabbitmq.com/confi…
  18. http://www.rabbitmq.com/reloc…
  19. http://www.rabbitmq.com/acces…
  20. http://www.rabbitmq.com/man/r…

相關文章