在CentOS镜像中安装数据库,你可以选择多种数据库系统,如MySQL、PostgreSQL、MariaDB等。以下是在CentOS 7上安装MySQL的步骤作为示例:
更新系统包
sudo yum update -y
安装MySQL服务器
sudo yum install -y mysql-server
启动MySQL服务
sudo systemctl start mysqld
设置MySQL开机自启
sudo systemctl enable mysqld
获取临时密码 安装完成后,MySQL会生成一个临时密码,你可以在日志文件中找到它:
sudo grep 'temporary password' /var/log/mysqld.log
运行安全脚本 使用临时密码登录MySQL,并运行安全脚本以设置新密码和其他安全选项:
sudo mysql_secure_installation
按照提示操作,设置新密码,删除匿名用户,禁止root远程登录等。
登录MySQL 使用新设置的密码登录MySQL:
mysql -u root -p
如果你想安装PostgreSQL,可以按照以下步骤进行:
添加PostgreSQL仓库
sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
安装PostgreSQL服务器
sudo yum install -y postgresql-server
启动PostgreSQL服务
sudo systemctl start postgresql
设置PostgreSQL开机自启
sudo systemctl enable postgresql
初始化数据库 PostgreSQL在安装过程中会自动初始化数据库。你可以通过以下命令检查状态:
sudo systemctl status postgresql
获取初始密码 初始密码通常在安装日志中,你可以查看日志文件:
sudo grep 'password' /var/log/postgresql/postgresql-*.log
切换到postgres用户并修改密码
sudo -u postgres psql
在psql命令行中,修改密码:
ALTER USER postgres WITH PASSWORD 'your_new_password';
MariaDB是MySQL的一个分支,安装步骤与MySQL类似:
更新系统包
sudo yum update -y
安装MariaDB服务器
sudo yum install -y mariadb-server mariadb
启动MariaDB服务
sudo systemctl start mariadb
设置MariaDB开机自启
sudo systemctl enable mariadb
获取临时密码
sudo grep 'temporary password' /var/log/mariadb/mariadb.log
运行安全脚本
sudo mysql_secure_installation
登录MariaDB
mysql -u root -p
以上步骤适用于CentOS 7。如果你使用的是CentOS 8或其他版本,步骤可能会有所不同。请根据实际情况进行调整。