在Linux上常用的数据库服务包括: - 关系型数据库:MySQL/MariaDB, PostgreSQL - NoSQL数据库:MongoDB, Redis - 时序数据库:InfluxDB - 键值存储:Redis, Memcached
# Ubuntu/Debian
sudo apt update
sudo apt install mariadb-server
# CentOS/RHEL
sudo yum install mariadb-server
sudo systemctl start mariadb
sudo systemctl enable mariadb
sudo mysql_secure_installation
sudo mysql -u root -p
CREATE DATABASE mydb;
CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON mydb.* TO 'myuser'@'localhost';
FLUSH PRIVILEGES;
/etc/mysql/my.cnf
或 /etc/my.cnf
/etc/mysql/conf.d/
或 /etc/mysql/mariadb.conf.d/
# Ubuntu/Debian
sudo apt install postgresql postgresql-contrib
# CentOS/RHEL
sudo yum install postgresql-server postgresql-contrib
sudo postgresql-setup initdb
sudo systemctl start postgresql
sudo systemctl enable postgresql
sudo -i -u postgres
createuser --interactive
createdb mydb
psql
ALTER USER myuser WITH PASSWORD 'password';
/var/lib/pgsql
或 /var/lib/postgresql
/var/lib/pgsql/data/postgresql.conf
/var/lib/pgsql/data/pg_hba.conf
# Ubuntu/Debian
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4
echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu $(lsb_release -cs)/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list
sudo apt update
sudo apt install mongodb-org
# CentOS/RHEL
sudo yum install mongodb-org
sudo systemctl start mongod
sudo systemctl enable mongod
mongo
use admin
db.createUser({
user: "admin",
pwd: "password",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
})
/etc/mongod.conf
# 启动服务
sudo systemctl start servicename
# 停止服务
sudo systemctl stop servicename
# 重启服务
sudo systemctl restart servicename
# 查看状态
sudo systemctl status servicename
# 设置开机启动
sudo systemctl enable servicename
# 备份单个数据库
mysqldump -u username -p database_name > backup.sql
# 备份所有数据库
mysqldump -u username -p --all-databases > all_backup.sql
# 恢复数据库
mysql -u username -p database_name < backup.sql
# 备份单个数据库
pg_dump -U username -d database_name -f backup.sql
# 备份所有数据库
pg_dumpall -U username -f all_backup.sql
# 恢复数据库
psql -U username -d database_name -f backup.sql
# 备份数据库
mongodump --db database_name --out /backup/path
# 恢复数据库
mongorestore --db database_name /backup/path/database_name
ANALYZE TABLE
(MySQL)或VACUUM ANALYZE
(PostgreSQL)EXPLAIN
分析慢查询/var/log/mysql/error.log
/var/lib/pgsql/data/pg_log/
/var/log/mongodb/mongod.log
命令行工具:
mysqladmin status
pg_stat_activity
mongostat
图形化工具:
专业监控系统:
通过以上配置和管理方法,您可以在Linux系统上高效地运行和维护各种数据库服务。