CentOS下安装数据库
在CentOS下安装MySQL与MongoDB
安装MySQL
CentOS下有更好的MariaDB(MySQL的子项目)代替MySQL,因此我们安装通过yum安装MariaDB
# yum install mariadb-server
# mysql_secure_installation # 设置MySQL默认用户密码
# systemctl start mariadb.service # 启动mariadb服务
# mysql -uroot -proot # 登录MySQL
开启MySQL远程连接:
# mysql -uroot -proot
# grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option; # root是用户名,%代表任意主机,'123456'指定的登录密码(这个和本地的root密码可以设置不同的,互不影响)
# flush privileges; # 重载系统权限
# 允许3306端口
# iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT
# iptables -L -n
# iptables -D INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT # # 此时生产环境是不安全的,远程管理之后应该关闭端口,删除之前添加的规则
安装MongoDB
1.添加MongoDB到yum repo
# vim /etc/yum.repos.d/mongodb.repo
[mongodb]
name=MongoDB repo
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64/
gpgcheck=0
enabled=1
2.安装MongoDB
# yum install mongodb-org
3.检查并启动MongoDB
# chkconfig mongod on
# service mongod start
# 或者
# systemctl restart mongo.service
4.检查是否启动
# ps -ef | grep mongo
mongod 2587 1 1 05:24 ? 00:00:08 /usr/bin/mongod -f /etc/mongod.conf
root 2984 2270 0 05:34 pts/1 00:00:00 grep --color=auto mongo
# netstat -an | grep 27017
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN
unix 2 [ ACC ] STREAM LISTENING 20600 /tmp/mongodb-27017.sock
5.开启防火墙远程访问端口
# firewall-cmd --zone=public --add-port=27017/tcp --permanent
success
# firewall-cmd --reload
success
如果关闭了firewall,则在iptables开放远端访问端口:
# iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 27017 -j ACCEPT
修改MongoDB配置
# vi /etc/mongod.conf
# 添加或修改如下配置
bind_ip = 0.0.0.0
port = 27017
6.iptables和firewalld
# yum install iptables-services
# systemctl mask firewalld.service
# systemctl enable iptables.service
# systemctl enable ip6tables.service
# systemctl stop firewalld.service
# systemctl start iptables.service
# systemctl start ip6tables.service