开发者学堂课程【7天突破PolarDB for PostgreSQL 2022版:从 Oracle/MySQL 到 PolarDB(一)】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/992/detail/14975
从Oracle/MySQL到PolarDB
内容介绍:
一、 基本概念
二、数据库、schema 与 owner>Oracle 中的数据库、Schema 实例
一、基本概念
主要是对比一下三个数据库的一些比较基础的一些概念,这节课定位比较初级,主要是为 PolarDB 深入去学习,深入去使用,来作为基础。
如果说作为基础,那么就从连接开始。如果对 oracle 和 mysql 有所了解就可以开始本节课。,其实主要是针对这样的情况,去学习 polardb,学习如何使用。
1、连接 Oracle
>Oracle 的常用连接方式
sqlplus / as sysdba 这样就连接了
[oracle@localhost ~]$ sqlplus u1/a@192.168.51.214:1521/orc1.mc u1是用户名,a为密码,IP地址:192.168.51.214,端口号:1521,服务名:orc1.mc以上就是oracle一个简易的连接串,orical也有其副客户端,客户端后:TNS-NAME,如果对其比较熟悉,就不再详细描述了。
SQL*Plus: Release 11.2.0.4.0 Production on星期五4月1 17:02:02 2022copyright (c) 1982,2013,oracle.All rights reserved.
连接到:
oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Productionwith the Partitioning,OLAP,Data Mining and Real Application Testing options
sQL>
2、连接 MySQL
>IP方式连接
[mysql@VAGE01 ~]$ mysql -usystem -pOracle1 -hlocalhost-P3306 -Dvage
warning: Using a password on the command line interface can be insecure.
welcome to the MysQL monitor. Commands end with ; or lg.
Your MysQL connection id is 4
Type 'help; ' or '\h’ for help. Type '\c' to clear the current input statement.
mysql>
-u︰用户名
-p:密码
-h: IP或主机名
-P:端口号,默认3306
-D:数据库名
MySQL 连接其实告诉你的客户端程序也就是连接程序,要连接的服务器在哪里。假如我是在一台机,跟另外一个就是客户端服务器分开,只要告诉客户端的程序说需要连接服务器在哪里,Oracle 也是相同的。假如我在一台服务器上连接另一台服务器。客户端和服务器分开了,那么你就需要告诉客户端,服务器在这个地方。
登陆号是:-hlocalhost。还有用户名,密码以及要连接的数据库。以上就是 mysql 的几个连接方式。。
3、连接 PolarDB
>本地连接
[postgres@localhost pg]$./prebuild/bip/psql -p 6016
psql (11.2)
Type "help" for help.
postgres=#
假设现在已经启动,假如没有启动就先假设启动,后面会有一系列启动的过程。先从连接开始:
[postgres@locaThost ~]s psql -p6016
psql (11.2)
Type "he1p" for he1p
.postgres=#
Psql 类似于它的客户端工具,指定一个端口号,其他的都不需要指定,Oracle也有类似的连接方式,sqlplus / as sysdba,也不需要密码就可以指定,相当于既然已经登录到操作系统,已经进入家里面。再进入室内也不需要验证,这就是最简单的连接方式。甚至端口号使用的是默认端口号,就直接psql就可以完成连接,以上就是本地连接。
如果需要远程连接就需要说你的ip地址,就跟你的mysql一样,需要用户名,密码,ip,等等,一般需要连接的数据库可以省略。
4、连接 PolarDB
>远程连接
vim pg_hba.conf l
85 # IPv4 local connections:
86 host all all 127.0.0.1/32 trust
87 host all all 192.168.51.1/24 trust
[postgres@localhost pg]$./prebuild/bin/psql -U postgres -h 192.168.51.214 -d
postgres -p 6016
psql (11.2)
Type "help" for help.
gres=#
注意:无需密码
首先需要做一个配置,配置是在数据库的文件中,
lpostgresl@1ocalhostbin]scd /data/pgdata3lpostgres@locaThost pgdata3]s'1s
里面有一个参数文件:pg_hba.conf. 需要在里面做一个配置
85 # IPv4 local connections:
86 host all all 127.0.0.1/32 trust
87 host all all 192.168.51.1/24 trust
找到这个段:IPv4 local
意义也非常明显:想要哪边的机器能够连接过来,除了本地的localhost,设置的是192.168.51.1这个段。这里做了一个修改,如果设置的是trust,登录是不需要任何密码的,
[postgres@localhost pg]$./prebuild/bin/psql -U postgres -h直接进行登录。也就是不需要验证信任远方来的客人。修改为 md5,试验 md5 的方式。这是第一步进行的配置,配置完成后需要把配置重新加载一下。
按照没有密码的方式进行连接,
[postgres@localhost pgdata3]$ psql -u postgres -h 192.168.51.214 -d postgres -p 6016
Password for user postgres:
psql 12.1, server_l1.2
Type "he1p" for help.
postgres=#
5、连接:配置密码验证
设置密码
vim pg_hba.conf(相当于 oracle 监听配置文件)
85 #IPv4 local connections:
86 hostall all 127.6.6.1/32 trust
87 hostall all 192.168.51.1/24md5
[postgres@localhost pg]$./prebuild/bin/psql -p 6016
psql (11.2)
Type "help" for help.
postgres=# alter user postgres PASSWORD 'abcde ' ;ALTER ROLE
postgres=# \q
127.6.6.1/32是默认的方式所以是 trust,远程的都是要配置成密码验证,这个密码是md5。
如果已经配置成密码验证,对于用户来说是要配置密码的,例如:postgres是默认用户,先将这个用户的密码改成 abcde,后面再改成123456,因为后面是用123456进行连接的,这就是配置密码,本来用户是没有密码的。
[postgres@localhost pg]$./prebuild/bin/psql -p 6016
psql (11.2)
进行完这个配置就要设置密码,这就是远程连接,只要设置了 md5就要设置密码,PolarDB 连接串跟 mysql 是有点像的。都是多个字符串进行连接,并且都是使用-u来表示用户名区别是 mysql 中的 u 是小写,这里是 polarDB,密码 mysql 是可以用一个选项放到命令行中,密码不允许使用,是将它放到下面去使用,这样就更保险。主机这一项都是小写-h,-d是要连接的数据库。-p表示端口号。所以 polarDB 连接跟mysql 更相似,oracle而是一个参数。关于修改密码,除了 alter user,这是标准secur,都支持 alter user ,不过 mysql 后面使用的不是 passeord,也就是前面相同,后面选项不大相同。除了这种修改密码的方式,pd 还有另外一种修改命令的方式,使用\命令。
更安全的设置密码
[postgres@localhost pg]$./prebuild/bin/psql -p6016 -Upostgres
psql (11.2)
Type "help" for help.
postgres=# lpassword
Enter new password:(此处输入密码)
Enter it again:(此处输入密码)
postgres=# \q
这是一种常用的应用形式,属于自身的客户端工具,属于自身的命令的一种使用方式。这就不属于标准 syql。这种方式设置完成之后,命令不大会显示,就会更加安全。如果已经安装了 prodb,可以连接一下。
6、介绍与连接相关的概念:
进程模式:
Oracle、polarDB 都属于进程模式,
线程模式:
Mysql 属于线程模式。
进程与线程没有什么优劣之分,早期 oracle\polardb 使用进程模式是因为历史很早,进程模式源于 PG,PG 源于 englis 数据库,最早起源于七十年代。
在那个时候并没有线程模式,线程模式出现的很晚,orcal 也是关系型数据库出现后,相当于第一批实现了关系型数据库,都是源于七十年代,那个时候没有线程模式,都是进程模式。
那是否说线程模式就优于进程模式,这也是不一定的。在操作系统中勉强认为线程模式跟进程模式是一回事,线程模式的优点是启动比较快,能启动多个进程。进程就不一样了,启动是非常消耗时间的。通过进程线程的模式,这两个模式肯定更适合长连接。如果是短连接模式,则mysql模式就可能更适合一些。除了这一点进程模式线程模式就没有任何的优劣之分了。将进程模式改成线程模式,不会有性能方面的收益,相反,将线程模式改成进程模式也不会有任何高可用的东西。
进程与线程:
(1)Oracle进程介绍
ps -ef|grep ora
oracle21269 1 Mare8 ? 00:04:21 ora_pmon_orcl
oracle212881 Mare8 ? 00:01:54 ora_dbwe_orcl
oracle212901 Mare8 ? 00:07:54 ora_lgwr_orcl
oracle212921 Mare8 ? 00:05:49 ora_ckpt_orcl
oracle 212941 0 Mare8 ?00:00:56 ora_smon_orcl
以上是 oracle 五大进程,根据名称也很容易识别。核心进程负责写板块,检查点等等
(2)PolarDB 进程介绍
[ root@localhost ~]# ps -eflgrep postgres
postgres 97571 0 18:00 pts/2 00:00:e0 /home/postgres/polardb/PolarDB-for-PostgresQL/ prebuild/bin/postgres -D /data/pgdata3
postgres 9760 9757 0 18:00 ?Ⅰ00:0e:00 postgres: checkpointerpostgres 97619757 0 18:00 ?
postgres 97629757 0 18:00 ?
e:00:00 postgres : walwriter
postgres97639757 0 18:0日 ?
0e:60:00 postgres : autovacuum launcher
postgres97649757 018:0日 ?
0:00:00 postgres : archiver
postgres97659757 0 18:0日 ?
00:00:00 postgres : stats collector
postgres 9766 9757 0 18:0日 ?
e:6e:0e postgres: logical replication
launcher
postgres 11268 10852 0 18:24 pts/200:00:00 ./prebuild/bin/psql -U postgres -h192.168.51.214 -d postgres -p 6016 -w
postgres 1127997570 18:24 ?
0e:0e:0e postgres : postgres postgres
192.168.51.214(17086) idle
通过这种方式进行查询:
[ root@localhost ~]# ps -eflgrep postgres postgres
查询结果跟 oracle 很相像,
当然也有一些独特的进程,例如 autovacuum,这个是 oracle 中没有的。Walwrite 有点类似于 lgwr,polarDB 是使用 air 日志。
名称上 oracle 对于客户端一个连接要起一个进程,一个连接要起一个进程来跟连接对应。来给客户端提供服务,Oracle 是一个服务进程,这是唯一一点不同,其他大体都相似。
(3)MySQL 线程
[mysql@localhost ~]$ ps -efTlgrep mysqldI
mysql12118 12118 12061 0 18:37 pts/200:0e:00 /bin/sh
/data/mysq1/app/bin/mysqld_safe --defaults-file=/mysql/product/my.cnf --user=mysqlmysql 12820 12820 12118 1 18:37 pts/2 00:00:00 /data/mysql/app/bin/mysqld --defaults-file=/mysql/product/my.cnf --basedir=/data/mysql/app --
datadir=/mysq1/product/data --plugin-dir=/data/mysq1/app/lib/plugin --log-error=/mysq1/product/data/myerror.log --pid-file=localhost.pid --
socket=/tmp/mysql.sock --port=6015
有个大写的 T,就是一个查进程,
启动进程:[root@1ocalhost ff]# su – mysql
Last login: _sun Apr 10 17:13:i9 csT 2022_on pts/1[mysq1@1ocalhost -]s history l grep mysq1_sa
909history l grep mysql_sa
启动成功,这三个数据库从基本原理上来说也是非常相似的,除了一点,oracle 和mysql 是使用安动体系,后面也会说到,除了特别指定,mysql 都是使用的 enodabe运行。当使用安动的方式来使用Mddc,相当于使用版本的方式来使用 mdb,关于更深的原理方面也不多讲了,主要讲解基本的概念部分。
7、Oracle 的启动 sqlplus / as sysdbastartup
Oracle 一定要移至本地方式,也就是采用登堂入室的方式启动。进入客户端用这个客户端发起应用,用这个客户端方式来发起 orcale 的可执行文件。Oracle 的启动稍微复杂点,直接运行 oracle 的可运行文件是不行的,启动分为以下三步骤:
I.打开参数文件
II.打开控制文件
III.打开所有数据文件
启动的三个步骤跟 polardb 很像,mysql 不太一样,文件的控制体系没有统一的控制文件。有类似的但是不叫作控制文件。
数据库的启停:
MySQL 启动
/data/mysql/app/bin/mysqld_safe --defaults-file=/mysql/product/my.cnf --user=mysql &
需要指定一下参数文件,不指定也可以,一般有其自己的搜索路径,一般会优先搜索etc 下面的 midecofe,如果想使用整个参数文件就可以将其全部复制,以上就是启动。
对于启动来说也就是参数,MySQL 也是一样有个参数文件,my,cnf。参数文件里面保存着内存,配置,因为不告诉数据库你要使用多大内存,它就无法从机器中分出内存来使用,一般来说,需要占据主机内存的一半。
Polardb 不大一样,占据的内存稍微小点,但是也是占一大块内存。Orcale 是要多余一半内存的。所以一定要再参数数据中告诉主机能够占用多少内存,这是参数文件的主要作用,参数文件在 polardb 中是起到很大作用的。
8、PolarDB 启动
./prebuild/bin/pg_ctl -D /data/pgdata3 start -l logfile还可以怎样启动?
有一个参数文件:-D /data/pgdata3指定一个数据库所在的目录,也就是默认的数据库所在的目录,参数也就存在于这个目录中。
postgres=#_ lq
[postgres@1ocalhost pgdata3]$
Ipostgres@1ocaThost pgdatas]s pwd/data/pgdata3
Lpostgres@locaThost pgdata3]s
参数文件:
7home/postgres[polardb/PolarD8-for-PostgresQL/prebuild/bin[postgres@1ocaThost bin]s cd /data/pgdata3
Lpostgres@localhost pgdata3]s1s
backup_labe1.old pg commit ts pg_hba.conf 、_pg notify pg. stat PG_VERSION polar_dma.conf
postmaster.pid
postgresql .auto.conf
restore_b1ock_7ist
base.tar.gzpo ctslodpd-_1oericcon pg-repIs]otpg stat tmp pg wal
postgresq1.conf
tablespace_map.old
globaiod dvnchiem p1091aa1. p9 serial. pg tblspc pg.xact
postmaster.opts
pg_hba.conf 也是参数文件之一,这个配置文件决定了是否能够连接,主要的参数文件是:postgresql.conf,这个是 polarDB 主要的参数文件,所以是到这个地方来寻找参数文件。有个选项 start,再加个日志。加日志是为了将内容输送到日志中以便于发现输送过程中的错误。
9、PolarDB:最直接的启动方式[postgres@localhost PolarDB-for-PostgreSQL]$ ./prebuild/bin/postgres -D /data/pgdata32022-04-01 18:45:01.108 CST [13360] LOG: Finished Loading Config File2022-04-01 18:45:01.132 CST[13360] LO6: listening on IPv6 address "::1",port 60162022-04-01 18:45:01.133 CST[13360] LOG: listening on IPv4 address "127.0.0.1" , port..…....2022-04-01 18:45:01.263 CST[13360] L06: database system is ready to acceptconnectionsMysql也可以直接运行可执行文件启动
postgres=#_ lq..
Lpostgres@locaThost ~]s psql -p6016psql (11.22
Type "heip" for help.
postgres=#_ lq
[postgres@localhost ~]s,pg_ctl -o /data/pgdata3 stop -1 logfilewaiting for server to shut down.... done
server stopped
[postgres@locaThost~]s ps -eflgrep postgresroot2882324906o i8:59 pts/i
oo:00:00 su -.postgres
postgres 28824 28823 0 18:59 pts/100:00:00 -bash
root28874 30286 0 18:59 pts/200:00:00 su -, postgrespostgres 28875288740 18:59 pts/200:00:00 -bash'_
postgres 31983 28824 0 19:36 pts/i00:00:00 ps -efpostgres 31984_28824_0 19:36 pts/1
00:00:0o grep --color=auto postgres
Lpostgres@locaThost ~]5
postgres-REL_12_STABLE/
[postgres@loca1host ~js cd po
polardb/.postgres/
postgresq1-12.1/
postgres-REL_12_STABLE/
[postgres@localhost -]$ cd po1ardb/PolarDB-for-PostgresQL/
Lpostgres@localhost Po1arDB-for-PostgresQL]s 1s
acloca1.m4config_configurecscope.in.out doc
README
Roadmap of PolarDB (1).md
build.sh config.log configure.in cscpe. oItout doc er CGNUmAKYT11e.in Makef1lepo]ardb_group.png
NOTICE
polardb.png
README.git sre
clean.sh_ config.status contrib cscope.po.out GNUmakefile LTCENSE
onekey.shprebuild
README.md
[postgres@loca1host PolarDB-for-PostgresQL]s
/pr
vin/postgres -o /data/pgdata3
放置到企业版中开源就只能自己研究,像 polardb 开源还是很充分的。这就是源码编译出来的版本,编译出来也是很方便的。直接 make 一下就可以,非常方便。
编译完成后的可执行文件是:
[postgres@localhost PolarDB-for-PostgresoL]s 1s -1Frt ./prebui1d/bin/postgres-rwXr-xr-x 1 postgres postgres 22555608 jul 28 2021 ./prebuild/bin/postgres*[postgres@1ocalhost PolarDB-for-PostgresQLjs]
lpostgres@1ocaThost PolarDB-for-PostgresoLj3
mysql-D 这个名称有段时间还引起了风潮,polarDB 跟 oracle 都出现的很早都在mysql 之前。
postgres@localhost PolarDB-for-PostgresqL]s. ....
lpostgres@loca1host PolarDB-for-PostgresoL]s ./prebui1d/bin/postgres.-D /data/pgdata3022-64-13 19:38:17.195 CST [32110] LoG:Finished Loading config File
启动成功,进行连接.
aata/pgaata3 ..__.[postgres@1ocalhost pgdata3]Slpostgres@localhost pgdata3js
[postgres@localhost pgdata3]s psq1 -u postgres -h 192.168.51.214 -d postgres -p 6016Password for user postgres:
psq1: error: could not connect to server: FATAL:, password authentication failed for user "“postgres"[postgres@loca1host pgdata3]s psq1 -U postgres -h 192.168.51.214 -d postgres -p 6016.因为没有放在后台连接,所以这个窗口就被占用了。同时也不建议直接运行程序,采用可执行文件方式去启动数据库。
Password for user postgres:
psql (12.i, server il1.2Type "he1p" for help.
MySQL 也可以,直接 mysql-D 就可以启动
10、数据库的关闭
pg_ctl -D /data/pgdata3 stopmysqladmin -uroot -proot -s/tmp/mysql.sock -P6015 shutdownsqlplus / as sysdbashutdown immediate/abort
启动跟关闭不是同一个命令,mysqladmin 是单独做一个数据库来进行管理,不止可以关闭,也可以用来进行其他的操作,oracle 也是用 sqlplus,这个是非常重要的,sqlplus 是启动跟停止都是同一个,也是非常方便的。
11、数据库的告警日志体系
cd /data/pgdata3
vim postgres.conf
log_destinationcn
参数:
364 #log_destination = 'stderr'# Valid values are combinations of365
#stderr, csvlog, syslog, and eventlog,
366
# depending on platform.csvlog
367
# requires logging_collector to be on.
oracle 中有 diagnostic_dest 参数,决定日志文件位置
MySQL 中 log_error 参数
启停关闭这些基本的东西就讲解完了,数据库研发也要是 dba,就能够知道如何开发一个数据库,作为 dba 来说也很关注数据库的警告,
Pg 的配置需要在阐述一些配置,log_destination。
配置错误输出到哪,输出到 stderr,通常使用 asvlog 使用得会多一些,自己会放到日志文件中,syslog 这种日志的体系中,如果希望有日志的话,需要自己配置一下,启动的时候需要加上-l logfile,如果不添加就是还是没有日志,oracle 是由这个diagnostic_dest 参数决定的,mysql 是用 log_error 参数来配置错误日志的位置,如果直接放到文件中,就会出现在目录里面。
poscgres-H_9 _.,..2e .7-Lpostgres@locaThost pgdata3]$ 1s -7Frttotal3008
在数据库中会多一个 log 目录,之前进行 orical 日志的时候都要将报错日志进行报警报错一下,这样数据库有问题也就知道了,这对于用户的使用感受就不同了,以上就是一些基础,如何启动,如何关闭,启动失败时,如何在日志里进行一些判断,意思就是启动失败时通过查看日志可以看到一些错误失败等等。
