在Ubuntu下搭建微服务开发环境

关于本文

  • 写作目标:作为在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

安装

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…
    原文作者:wangdandong
    原文地址: https://segmentfault.com/a/1190000012057130
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞