在CentOS虛擬機上安裝數據庫的過程取決于您選擇的數據庫類型。以下是兩種常見數據庫(MySQL和PostgreSQL)的安裝指南:
sudo yum update -y
sudo yum install mysql-server -y
sudo systemctl start mysqld
sudo systemctl enable mysqld
grep 'temporary password' /var/log/mysqld.log
mysql -u root -p
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
FLUSH PRIVILEGES;
sudo yum install mysql -y
sudo nano /etc/my.cnf
在[client]部分添加以下配置:
[client]
default-character-set = utf8mb4
sudo systemctl status firewalld
sudo systemctl stop firewalld
sudo systemctl disable firewalld
mysql -u root -p
CREATE USER 'new_user'@'%' IDENTIFIED BY 'new_password';
GRANT ALL PRIVILEGES ON *.* TO 'new_user'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
mysqldump
備份整個數據庫:mysqldump -u root -p database_name > database_name_backup.sql
mysqlpump
備份整個數據庫(CentOS 8及以上版本):mysqlpump -u root -p --all-databases > all_databases_backup.sql
mysqldump
恢復數據庫:mysql -u root -p database_name < database_name_backup.sql
yum install -y systemd-devel readline readline-devel zlib-devel gcc
wget https://ftp.postgresql.org/pub/source/v16.0/postgresql-16.0.tar.gz
tar -zxvf postgresql-16.0.tar.gz -C /home/
cd postgresql-16.0
./configure --prefix=/usr/local/pgsql --without-icu --with-systemd
make && make install
chown -R postgres:postgres /usr/local/pgsql
su - postgres
/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data
/usr/lib/systemd/system/postgresql.service
文件:vim /usr/lib/systemd/system/postgresql.service
systemctl daemon-reload
systemctl enable postgresql
systemctl start postgresql
postgres
用戶:su - postgres
/usr/local/pgsql/bin/psql
CREATE USER admin WITH PASSWORD 'admin';
CREATE DATABASE tt OWNER admin;
postgresql.conf
和pg_hba.conf
以允許遠程連接:vim /usr/local/pgsql/data/postgresql.conf
listen_addresses = '*'
port = 5432
vim /usr/local/pgsql/data/pg_hba.conf
host all all 0.0.0.0/0 md5
pg_ctl -D /usr/local/pgsql/data -l logfile start
psql
連接數據庫:psql -U admin -d tt
以上步驟提供了在CentOS虛擬機上安裝和配置MySQL和PostgreSQL的基本指南。根據您的具體需求,您可能需要進行更多的配置和優化。