docker部署[centos/微服务], 安装mysql/mongodb/redis/elasticsearch


基本环境: centos
基本要求: mysql数据库, mongodb数据库, elasticsearch数据库, redis
基本设计: 其他在centos装, elasticsearch采用官方镜像6.5.4的docker

一 安装mysql

rpm -qa | grep mysql
yum install mysql

删除原来mysql:
yum remove mysql

下载mysql的repo源
wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
安装mysql-community-release-el7-5.noarch.rpm包
sudo rpm -ivh mysql-community-release-el7-5.noarch.rpm

yum install mysql-server
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
没有可用软件包 mysql-server。
错误:无须任何处理

再次检查是否已经安装:
rpm -qa | grep mysql
mysql-community-common-5.6.43-2.el7.x86_64
mysql-community-client-5.6.43-2.el7.x86_64
mysql-community-server-5.6.43-2.el7.x86_64
mysql-community-release-el7-5.noarch
mysql-community-libs-5.6.43-2.el7.x86_64

如果报错:
Requires: libc.so.6(GLIBC_2.17)(64bit)
yum install glibc.i686
yum list libstdc++*

mysql -u root
登录时有可能报这样的错:ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2),原因是/var/lib/mysql的访问权限问题。下面的命令把/var/lib/mysql的拥有者改为当前用户:
sudo chown -R openscanner:openscanner /var/lib/mysql
sudo chown -R root:root /var/lib/mysql

如果不是:
service mysql start
mysql -u root
mysql> set password for root@localhost = password('apptestxwk');
mysql -u root -p
apptestxwk

 

使用msqlworkbench手动导出原来表数据结构,因为报错,采用命令行导出
server---export data--Dump Structre Only
Unknown table 'column_statistics' in information_schema (1109)
跳板机器登录:
mysql -h rm-xxxxxxxxxx.mysql.rds.aliyuncs.com:3306 -u dba -p
ERROR 2005 (HY000): Unknown MySQL server host in rm-xxxxxxxx.mysql.rds.aliyuncs.com:3306

mysql -h rm-xxxxxxxxxx.mysql.rds.aliyuncs.com -u dba -p

 

明密码登录不允许:

mysqldump -h rm-xxxxxxxxxxx.mysql.rds.aliyuncs.com -uroot -p123456 app > app_dump.sql
mysqldump: Got error: 1045: Access denied for user 'root'@'192.168.11.187' (using password: YES) when trying to connect

mysqldump -h rm-xxxxxxxxxxx.mysql.rds.aliyuncs.com -uroot -p app > app_dump.sql
123456

To make a complete dump, pass --all-databases --triggers --routines --events.

基本导出:
mysqldump --opt -h rm-xxxxxxxxx.mysql.rds.aliyuncs.com app -u dba -p > app_dump.sql  

将触发器也要导出
mysqldump --opt --triggers --routines --events -h rm-xxxxxxxxs.mysql.rds.aliyuncs.com app -u dba -p > appdb_dump.sql 

导入数据:
mysql -h rm-xxxxxxx.mysql.rds.aliyuncs.com -u dba -p
lyp82nLF
create database apptest;
进入数据库use apptest
source /root/appdb_dump.sql

mongodb:
service mongod start
创建管理员,root root
> use admin
switched to db admin
> db.createUser({user:"root",pwd:"root",roles:["userAdminAnyDatabase"]})
Successfully added user: { "user" : "root", "roles" : [ "userAdminAnyDatabase" ] }
db.auth("root","root")

use apptest

 

MySQL导出的问题:

MySQL导出的SQL语句在导入时有可能会非常非常慢,经历过导入仅45万条记录,竟用了近3个小时。在导出时合理使用几个参数,可以大大加快导入的速度。 
注意:max_allowed_packet和net_buffer_length不能比目标数据库的配置数值大,否则可能出错。
-e 使用包括几个VALUES列表的多行INSERT语法; 
--max_allowed_packet=XXX 客户端/服务器之间通信的缓存区的最大大小; 
--net_buffer_length=XXX TCP/IP和套接字通信缓冲区大小,创建长度达net_buffer_length的行

首先确定目标库的参数值
mysql>show variables like 'max_allowed_packet'; 4194304
mysql>show variables like 'net_buffer_length'; 16384

根据参数值书写mysqldump命令,如: 
mysql>mysqldump -uroot -p apptest -e --max_allowed_packet=1048576 --net_buffer_length=16384 > SQL文件

如: 
mysql>mysqldump -uroot -p apptest -e --max_allowed_packet=1048576 --net_buffer_length=16384 > apptest.sql

ssh:47.93.7.185:22
username:root
password:
hostname: localhost
port:3306
username:dba
pwd:xxx


导出语句大概耗时4-5分钟
mysqldump --opt --triggers --routines --events -h rm-2ze95784ru46d502s.mysql.rds.aliyuncs.com app -u dba -p -e --max_allowed_packet=4194304 --net_buffer_length=16384 > apptestneed_dump.sql

导入数据200多万:4:51-5:13 差不多23分
mysql -h localhost -u root -p
apptestxwk
create database apptest;
进入数据库use apptest;
source /root/apptestneed_dump.sql

 

 

二 安装ElasticSearch

es默认用户名没有,连接只需要localhost:9200
docker-es:
yum install docker
service docker start
docker search elasticsearch
docker pull docker.io/nshou/elasticsearch-kibana
docker run -d -p 9200:9200 -p 9300:9300 -p 5601:5601 --name eskibana docker.io/nshou/elasticsearch-kibana
curl localhost:9200出现返回数据就正常

修改docker-elasticsearch密码:
docker exec -it 08ccecabe456 sh
cat /etc/issue
Welcome to Alpine Linux 3.8
Kernel \r on an \m (\l)
进入docker的elasticsearch安装目录可以看到好多bat,
其中有elasticsearch-plugin.bat
~/elasticsearch/bin $ pwd
/home/elasticsearch/elasticsearch/bin
elasticsearch-plugin install x-pack


暂时没有修改密码成功:
默认是elastic和changme

curl -XPUT -u elastic 'http://localhost:9200/_xpack/security/user/elastic/_password' -d '{
"password" : "apptestxwk"
}'
curl -XPUT -u elastic 'http://localhost:9200/_xpack/security/user/kibana/_password' -d '{
"password" : "apptestxwk"
}'

 

response: {'error': {'root_cause': [{'type': 'mapper_parsing_exception', 'reason': 'analyzer [ik_smart] not found for field [star]'}], 'type': 'mapper_parsing_exception', 'reason': 'Failed to parse mapping [post]: analyzer [ik_smart] not found for field [star]', 'caused_by': {'type': 'mapper_parsing_exception', 'reason': 'analyzer [ik_smart] not found for field [star]'}}, 'status': 400}

analyzer [ik_smart] not found for field
curl http://localhost:9200/_analyze?analyzer=ik_max_word&text="helloword"

ES2.0版本与ES5.0以后的版本对比以及注意事项
1、5.0以后移除名为 ik 的analyzer和tokenizer,请分别使用 ik_smart 和 ik_max_word(也就是5.0之前进行测试的时候需要将上面中的ik_max_word换成ik关键字)
2、ES6.0与ik6.0进行集成安装之后,只能通过head工具来进行测试,通过url进行测试的时候会报错,这是一个bug已经提交到gethub上了,希望社区管理能及时更正(亲测5.5.1版本没问题)。
3、ES5.0之后的ik_smart和ik_max_word说明
ik_max_word: 会将文本做最细粒度的拆分,比如会将“我是中国人”拆分为“我,是,中国人,中国,国人”,会穷尽各种可能的组合。
ik_smart: 会做最粗粒度的拆分,比如会将“我是中国人”拆分为“我,是,中国人”。


docker pull docker.io/elasticsearch
manifest for docker.io/elasticsearch:latest not found
docker pull docker.io/elasticsearch:6.5.4

启动容器放置到前台运行,看报错调整:
docker run -p 9200:9200 -p 9300:9300 --name es_test docker.io/elasticsearch:6.5.4

启动容器放置到后台运行:
docker run -d -p 9200:9200 -p 9300:9300 -p 5601:5601 --name es_test docker.io/elasticsearch:6.5.4

提示端口号被占用,其实没有占用; 尝试重启docker服务
netstat -ap | grep 9300
service docker stop
service docker start

RROR: [1] bootstrap checks failed
[1]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
[2019-03-30T12:27:15,148][INFO ][o.e.n.Node ] [kuGGo0b] stopping ...

执行以下配置来尝试:
cat /proc/sys/vm/max_map_count
sysctl -w vm.max_map_count=262144

指定虚拟内存300兆(在服务器如果是测试服,尽量小一点,4g, 内存默认占据很多,会占据到30-40%):
docker run --name es_test01 -p 9200:9200 -p 9300:9300 -p 5601:5601 -e ES_JAVA_OPTS="-Xms300m -Xmx300m" docker.io/elasticsearch:6.5.4

指定虚拟内存1g:
docker run --name es_test01 -p 9200:9200 -p 9300:9300 -p 5601:5601 -e ES_JAVA_OPTS="-Xms1g -Xmx1g" docker.io/elasticsearch:6.5.4

 

执行命令查看是否成功
curl localhost:9200

which elasticsearch
/usr/share/elasticsearch/bin/elasticsearch

docker内部安装分词器;正确没错
./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.5.4/elasticsearch-analysis-ik-6.5.4.zip

/usr/share/elasticsearch/bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.5.4/elasticsearch-analysis-ik-6.5.4.zip

再次执行查看是否分词正常
/usr/bin/python3.6 /root/backend/script/create_test_esmapping.py

 

 

三 Redis

yum install redis
service redis start
vi /etc/redis.conf
vi-i
:/requirepass
原来带的:requirepass foobared
改掉:requirepass apptestxwk
service redis restart
redis-cli -h localhost -p 6379
localhost:6379> keys *
(error) NOAUTH Authentication required.
redis-cli -h localhost -p 6379 -a apptestxwk
# 登录后不重启修改密码config set requirepass apptestxwk

四 mongodb

service mongod start
创建管理员,root root
> use admin
switched to db admin
> db.createUser({user:"root",pwd:"root",roles:["userAdminAnyDatabase"]})
Successfully added user: { "user" : "root", "roles" : [ "userAdminAnyDatabase" ] }
db.auth("root","root")

 

或者

use admin
db.dropUser("root");

db.createUser({user:"root",pwd:"root",roles:[{"role":"userAdminAnyDatabase","db":"admin"}]})

db.createUser({user:"root",pwd:"root",roles:[{"role":"userAdminAnyDatabase","db":"admin"},{"role":"readWrite","db":"test"}]})

db.auth("root","root")
db.system.users.find()
service mongod restart

 

支持信息:

https://pypi.org/project/pymongo/
PyMongo supports MongoDB 2.6, 3.0, 3.2, 3.4, 3.6 and 4.0.
pymongo 3.7 支持mongodb4.0.5, 应该是没问题的

> use admin
switched to db admin
> db.system.users.find()
{ "_id" : "admin.root", "user" : "root", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "InGAMBfDRWMOCF/X9wCICw==", "storedKey" : "4pWSPvq71BjlqTY5Hjr69u9ajXo=", "serverKey" : "ceQlLGm7MCyBC4+RMQs7elvT1/o=" }, "SCRAM-SHA-256" : { "iterationCount" : 15000, "salt" : "swcDiA7bTaBq1sQt2gMNdrf2buefv+StyKAAHg==", "storedKey" : "4E7Z9CuoH4Gv8+u2jtl2aRslcGISMqlgh+PIQ3aIu1A=", "serverKey" : "XtqdmRkEAIKjb2hlYFCKMxOiOpv4RojE6j/eTkk6XxM=" } }, "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" }, { "role" : "readWrite", "db" : "test" } ] }

 

posted @ 2019-04-01 22:01  Adamanter  阅读(356)  评论(0编辑  收藏  举报