CentOS 7.2环境搭建实录(第三章:mysql安装和postgresql安装)
时间:2023-02-16
浏览:329
作者:佚名
积累知识,胜过积蓄金银!毕竟在##column_title##开发的过程中,会遇到各种各样的问题,往往都是一些细节知识点还没有掌握好而导致的,因此基础知识点的积累是很重要的。下面本文《CentOS 7.2环境搭建实录(第三章:mysql安装和postgresql安装)》,就带大家讲解一下MySQL、postgresql、Linux知识点,若是你对本文感兴趣,或者是想搞懂其中某个知识点,就请你继续往下看吧~
第三章:mysql安装和postgresql安装
mysql安装
下载mysql
cd /usr/local/src wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz tar -zxvf mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz mv mysql-5.7.18-linux-glibc2.5-x86_64 /usr/local/mysql
创建mysql用户
groupadd mysql useradd -r -g mysql mysql
初始化数据库
cd /usr/local/mysql mkdir data chwon -R mysql:mysql data bin/mysqld --initialize --user=mysql --datadir=/usr/local/mysql/data #记录初始化数据库的root用户密码 #也可以使用下面的命令,两者的区别就是一个有初始化密码,一个没有 bin/mysqld --initialize-insecure --user=mysql --datadir=/usr/local/mysql/data
配置启动信息
vi /etc/my.cnf #修改配置如下图所示:

#保存退出 cp -a support-files/mysql.service /etc/init.d/mysqld chmod +x /etc/init.d/mysqld #尝试启动mysql /etc/init.d/mysqld start #成功如下图:

#备注:mysql启动,重启有时会报下图错误:

mmap(137428992 bytes) failed; errno 12 #错误是mysql启动内存不足导致的,解决方法如下: mkdir -p /var/cache/swap/ dd if=/dev/zero of=/var/cache/swap/swap0 bs=1M count=512 chmod 0600 /var/cache/swap/swap0 mkswap /var/cache/swap/swap0 swapon /var/cache/swap/swap0 #这样就可以顺利启动mysql数据库 #将swap分区设置为开机启动: vi /etc/fstab #加入下列代码 /var/cache/swap/swap0 swap swap defaults 0 0
添加环境变量
export PATH=$PATH:/usr/local/mysql:/usr/local/mysql/bin
设置开机启动
chkconfig --add mysqld chkconfig mysqld on
mysql相关设置
#第一次进入需要输入初始密码:
mysql -uroot -p #初始root密码
mysql > set password=PASSWORD(''); #设置密码为空
mysql > use mysql;
mysql > update user set host = '%' where user = 'root'; #修改host
mysql > select user, host from user; #查看修改结果
mysql > flush privileges; #刷新设置
mysql > exit #退出
#在本地进行连接测试,结果如下图:

#安装成功
mysql安装汇总
安装包存放点:/usr/local/src/ mysql配置文件:/etc/my.cnf mysql数据信息目录:/usr/local/mysql/data/ mysql日志文件:/var/log/mariadb/mariadb.log mysqlpid文件:/var/run/mariadb/mariadb.pid(配置文件里是这样写的,实际根据服务器生成的具体文件为主,通过ps aux|grep mysql查看) 进入mysql数据库: mysql(无密码) mysql -u用户名 -p用户密码(有密码) 开启mysql: service mysqld start systemctl start mysqld 关闭mysql: service mysqld stop systemctl stop mysqld 重启mysql service mysqld restart syatemctl restart mysqld mysql状态 service mysqld status syatemctl status mysqld
postgresql安装
下载postgresql
cd /usr/local/src wget https://ftp.postgresql.org/pub/source/v10.1/postgresql-10.1.tar.gz tar -zxvf postgresql-10.1.tar.gz
postgresql编译安装
cd /postgresql-10.1 ./configure make && make install
添加postgres用户
adduser postgres
初始化postgresql数据库
cd /usr/local/pgsql mkdir data mkdir logs touch logs/pgsql.log #设置postgres数据信息和日志权限 chown -R postgres:postgres data chown -R postgres:postgres logs #初始化postgresql bin/initdb -D /usr/local/pgsql/data #根据提示执行 bin/pg_ctl start -D /usr/local/pgsql/data -l /usr/local/pgsql/logs/pgsql.log #成功启动出现下列信息 waiting for server to start.... done server started
进入postgresql数据库
bin/psql postgres=# \l #查看数据库信息 postgres=# \du #查看用户信息 postgres=# CREATE DATABASE root; #创建root数据库 postgres=# \l #查看创建结果 postgres=# CREATE ROLE root SUPERUSER #创建root超级用户 postgres=# ALTER ROLE root LOGIN #设置root用户登录权限 postgres=# \du #查看创建结果 退出postgresql数据库
修改postgresql配置文件
su - root cp /usr/local/src/postgresql-10.1/contrib/start-scripts/linux /etc/init.d/postgresql vi /etc/init.d/postgresql chmod +x /etc/init.d/postgresql 设置如下信息:

保存退出,测试如下命令: /etc/init.d/postgresql stop /etc/init.d/postgresql start /etc/init.d/postgresql restart 使用root用户进入postgresql数据库 /usr/local/pgsql/bin/psql 效果如下图:

postgresql安装成功
配置环境变量
cd /etc/profile export PATH=$PATH:/usr/local/pgsql/bin
配置开机启动
chkconfig --add postgresql chkconfig postgresql on
postgresql远程连接
vi /usr/local/pgsql/data/postgresql.conf # 修改如下:

# 保存退出 vi /usr/local/pgsql/data/pg_hba.conf # 修改如下:

# 保存退出 # 本地测试结果:

# 远程连接成功
postgresql安装总结
安装包存放点:/usr/local/src/
postgresql配置文件:/usr/local/pgsql/data/postgresql.conf
/usr/local/pgsql/data/pg_hba.conf
/etc/init.d/postgresql
postgresql数据目录:/usr/local/pgsql/data
postgresql日志文件:/usr/local/pgsql/logs/pgsql.log
postgresqlpid文件:/usr/local/pgsql/data/postmaster.pid
进入postgresql数据库:
psql # 已配置环境变量
/usr/local/pgsql/bin/psql # 未配置环境变量
启动postgresql
/etc/init.d/postgresql start
service postgresql start
systemctl start postgresql
停止postgresql
/etc/init.d/postgresql stop
service postgresql stop
systemctl stop postgresql
重启postgresql
/etc/init.d/postgresql restart
service postgresql restart
systemctl restart postgresql
安装php扩展
如果在之前编译php时没有加入--with-pdo-pgsql,--with-pgsql这两个模块,我们只需要在php编译文件里编译就可以使用postgresqlphp模块 # 安装pdo_pgsql模块 cd /usr/local/src/php-7.2.2/ext/pdo_pgsql/ /usr/local/php/bin/phpize ./configure --with-php-config=/usr/local/php/bin/php-config make && make install # 安装pgsql模块 cd /usr/local/src/php-7.2.2/ext/pgsql/ /usr/local/php/bin/phpize ./configure --with-php-config=/usr/local/php/bin/php-config make && make install service php-fpm restart # 重启php php -m # 查看模块列表 查看phpinfo()结果如下:

安装成功
其他文章
第一章:环境配置和nginx安装
第二章:php安装
第四章:python环境配置
今天关于《CentOS 7.2环境搭建实录(第三章:mysql安装和postgresql安装)》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!
本文内容来源于互联网,如有侵权请联系删除。
近期热门
11.18
11.15
站内热门
最新文章
11.28
11.28
11.28
热门专题
