前端技术
HTML
CSS
Javascript
前端框架和UI库
VUE
ReactJS
AngularJS
JQuery
NodeJS
JSON
Element-UI
Bootstrap
Material UI
服务端和客户端
Java
Python
PHP
Golang
Scala
Kotlin
Groovy
Ruby
Lua
.net
c#
c++
后端WEB和工程框架
SpringBoot
SpringCloud
Struts2
MyBatis
Hibernate
Tornado
Beego
Go-Spring
Go Gin
Go Iris
Dubbo
HessianRPC
Maven
Gradle
数据库
MySQL
Oracle
Mongo
中间件与web容器
Redis
MemCache
Etcd
Cassandra
Kafka
RabbitMQ
RocketMQ
ActiveMQ
Nacos
Consul
Tomcat
Nginx
Netty
大数据技术
Hive
Impala
ClickHouse
DorisDB
Greenplum
PostgreSQL
HBase
Kylin
Hadoop
Apache Pig
ZooKeeper
SeaTunnel
Sqoop
Datax
Flink
Spark
Mahout
数据搜索与日志
ElasticSearch
Apache Lucene
Apache Solr
Kibana
Logstash
数据可视化与OLAP
Apache Atlas
Superset
Saiku
Tesseract
系统与容器
Linux
Shell
Docker
Kubernetes
[主键 Primary Key ]的搜索结果
这里是文章列表。热门标签的颜色随机变换,标签颜色没有特殊含义。
点击某个标签可搜索标签相关的文章。
点击某个标签可搜索标签相关的文章。
MySQL
...储对应字段的数据。 主键(Primary Key) , 主键是一个或一组列,其值能够唯一标识表中的每一行记录。在MySQL建表语法中,通过primary key关键字定义主键,如例子中的id字段被设为主键,且具有自增特性(auto_increment),这意味着每当有新的记录插入时,系统会自动为id字段生成一个唯一的递增数值。 自动递增(Auto_increment) , 在MySQL中,auto_increment是一个属性,可用于整数类型的字段上,当插入新记录时,如果该字段没有明确赋值,MySQL将自动为其分配一个比当前已存在的最大值大1的新值。例如,文章中id字段设置为auto_increment,即每次新增记录时,id字段的值会自动递增。 字符集(Charset) , 字符集是在数据库中表示和存储文本数据的一套编码规则,如UTF8就是一种常用的字符集,它可以支持多种语言字符的存储和显示。在MySQL建表语法中,default charset=utf8指定了新建表的默认字符集为UTF-8,确保能够兼容并正确处理不同语言环境下的文本数据。
2023-10-30 22:22:20
117
码农
MySQL
...长字符串(20), PRIMARY KEY (id) ); 3. 添加数据 INSERT INTO user(username, password, email, phone) VALUES('user1', '123456', 'user1@email.com', '123456'); INSERT INTO user(username, password, email) VALUES('user2', 'abc123', 'user2@email.com'); 4. 检索数据 选取 从 user; 选取 username, email 从 user; 选取 从 user 在…条件下 username = 'user1'; 选取 COUNT() 从 user; 5. 修改数据 UPDATE user 设定 password = 'newpassword' 在…条件下 id = 1; 6. 删除数据 DELETE 从 user 在…条件下 id = 2; MySQL提供了丰富的功能和灵活的检索语言,可以满足大部分业务数据的保存需求。
2023-01-17 16:44:32
123
程序媛
MySQL
...INCREMENT PRIMARY KEY, name VARCHAR(30) NOT NULL, email VARCHAR(50) NOT NULL, reg_date TIMESTAMP )"; if (mysqli_query($conn, $sql)) { echo "数据表test创建成功"; } else { echo "创建数据表错误: " . mysqli_error($conn); } 以上代码将在您的MySQL数据库中创建名为test的数据表。该表包含id、name、email和reg_date列。id列将自动递增,并将作为主键。name和email列不能为NULL,而reg_date列将保存创建行的时间戳。 上传数据到MySQL数据库中可能需要一些额外的数据处理。您可以从CSV文件、文本文件、XML文件、JSON数据或通过表格收集的数据中读取数据,然后将其转换为MySQL可以处理的常规数据格式。使用以下PHP代码将数据上传到MySQL数据库中: $myfile = fopen("data.txt", "r") or die("不能打开文件!"); while (!feof($myfile)) { $line = fgets($myfile); $line_arr = explode(",", $line); $name = $line_arr[0]; $email = $line_arr[1]; $sql = "INSERT INTO test (name, email) VALUES ('$name', '$email')"; mysqli_query($conn, $sql); } fclose($myfile); echo "上传数据到MySQL数据库成功"; 以上代码将从文本文件中获取数据,并将其上传到MySQL数据库的test数据表中。请注意,我们将数据数组中的第一和第二个元素映射到MySQL表test中的name和email列。 当您上传或更新数据时,请记得在您的PHP脚本中使用适当的错误处理和安全措施,以确保数据库安全。
2024-01-19 14:50:17
333
数据库专家
转载文章
... (Foreign Key) , 在数据库设计中,外键是一个字段,其值引用了另一个表的主键。在文章提及的com_area表结构中,pid字段即为外键,它引用了本表的id字段(主键),这种设置用来表达地区间的层级关系,如北京市(id=2)是东城区(id=3)的父级地区,通过pid将它们关联起来。 Unicode编码 (Unicode) , Unicode是一种国际标准字符集,用于统一和涵盖全球所有语言文字的编码方案。在SQL语句中,name字段使用了utf8_unicode_ci编码,这意味着存储在该字段中的地区名称支持Unicode编码,能够正确处理中文字符以及其他多种语言的文字信息,确保全国地址数据的多语言兼容性和准确性。 自增主键 (Auto-increment Primary Key) , 在数据库表结构中,自增主键是一种特殊的主键约束,它的特点是每次插入新记录时,主键字段的值会自动递增。在com_area表中,id字段被定义为自增主键,意味着当向表中插入新的地区记录时,系统会自动为该记录分配一个唯一的、大于已有记录主键值的新ID,简化了数据插入操作,同时保证了主键字段的唯一性,有助于维护数据的一致性和完整性。
2023-06-30 09:11:08
62
转载
Oracle
... ( ID INT PRIMARY KEY, Name VARCHAR2(50), Email VARCHAR2(50), JobTitle VARCHAR2(50) ); 为了找出所有Email字段重复的记录,我们可以使用GROUP BY和HAVING子句: sql SELECT Email, COUNT() FROM Employees GROUP BY Email HAVING COUNT() > 1; 这段SQL会返回所有出现次数大于1的邮箱地址,这就意味着这些邮箱存在重复记录。 2. 删除重复记录 识别出重复记录后,我们需要谨慎地删除它们,确保不破坏数据完整性。一种策略是保留每个重复组的第一条记录,并删除其他重复项。为此,我们可以创建临时表,并用ROW_NUMBER()窗口函数来标识每组重复记录的顺序: sql -- 创建临时表并标记重复记录的顺序 CREATE TABLE Temp_Employees AS SELECT ID, Name, Email, JobTitle, ROW_NUMBER() OVER(PARTITION BY Email ORDER BY ID) as RowNum FROM Employees; -- 删除临时表中RowNum大于1的重复记录 DELETE FROM Temp_Employees WHERE RowNum > 1; -- 将无重复记录的临时表数据回迁到原表 INSERT INTO Employees (ID, Name, Email, JobTitle) SELECT ID, Name, Email, JobTitle FROM Temp_Employees; -- 清理临时表 DROP TABLE Temp_Employees; 上述代码流程中,我们首先创建了一个临时表Temp_Employees,为每个Email字段相同的组分配行号(根据ID排序)。然后删除行号大于1的记录,即除每组第一条记录以外的所有重复记录。最后,我们将去重后的数据重新插入原始表并清理临时表。 3. 防止未来新增重复记录 为了避免将来再次出现此类问题,我们可以为容易重复的字段添加唯一约束。例如,对于上面例子中的Email字段: sql ALTER TABLE Employees ADD CONSTRAINT Unique_Email UNIQUE (Email); 这样,在尝试插入新的具有已存在Email值的记录时,Oracle将自动阻止该操作。 总结 处理Oracle数据库中的重复记录问题是一个需要细心和策略的过程。在这个过程中,咱们得把数据结构摸得门儿清,像老朋友一样灵活运用SQL查询和DML语句。同时呢,咱们也得提前打个“预防针”,确保以后不再犯同样的错误。在这一整个寻觅答案和解决问题的旅程中,我们不停地琢磨、动手实践、灵活变通,这恰恰就是人与科技亲密接触所带来的那种无法抗拒的魅力。希望本文中给出的实例和小窍门,能真正帮到您,让管理维护您的Oracle数据库变得轻轻松松,确保数据稳稳妥妥、整整齐齐的。
2023-02-04 13:46:08
48
百转千回
Datax
...,还把它设成了关键的主键。这样一来,当我们往里边输入数据的时候,就特别容易踩到“唯一键约束冲突”这个坑。 四、解决方案 对于上述问题,我们可以采取以下几种解决方案: 1. 数据预处理 在插入数据之前,我们需要对数据进行有效的去重处理。例如,我们可以使用Python的pandas库来进行数据去重。具体的代码如下: python import pandas as pd 读取数据 df = pd.read_csv('data.csv') 去重 df.drop_duplicates(inplace=True) 写入数据 df.to_sql('users', engine, if_exists='append', index=False) 这段代码会先读取数据,然后对数据进行去重处理,最后再将处理后的数据写入到数据库中。 2. 调整数据库设计 如果我们发现是由于数据库设计不当导致的唯一键约束冲突,那么我们就需要调整数据库的设计。比如说,我们能够把那些重复的字段挪到另一个表格里头,然后在往里填充数据的时候,就像牵线搭桥一样,通过外键让这两个表格建立起亲密的关系。 sql CREATE TABLE users ( id INT PRIMARY KEY, email VARCHAR(50) UNIQUE ); CREATE TABLE user_info ( id INT PRIMARY KEY, user_id INT, info VARCHAR(50), FOREIGN KEY (user_id) REFERENCES users(id) ); 在这段SQL语句中,我们将用户表中的email字段设置为唯一键,并将其移到了user_info表中,然后通过user_id字段将两个表关联起来。 五、总结 以上就是解决Datax Writer插件写入数据时触发唯一键约束冲突的方法。需要注意的是,这只是其中的一种方法,具体的操作方式还需要根据实际情况来确定。另外,为了让这种问题离我们远远的,咱们最好养成棒棒的数据处理习惯,别让数据重复“撞车”。
2023-10-27 08:40:37
721
初心未变-t
Cassandra
...的时候,给它设定一个主键(就像身份证号那样重要),Cassandra这个小机灵鬼就会先瞅一眼主键的第一部分——分区键,然后对这个分区键进行一种叫做哈希运算的神奇操作。这个操作结束后,会产生一个哈希值,Cassandra就把它当作地址标签,把这个标签对应的表数据“嗖”地一下,精准投放到集群中的某个特定节点上。这种策略可以确保数据在所有节点间均匀分布,有效避免热点问题。 cql CREATE TABLE users ( user_id int, username text, email text, PRIMARY KEY (user_id) ) WITH partitioner = 'org.apache.cassandra.dht.Murmur3Partitioner'; 上述代码创建了一个名为users的表,其中user_id作为分区键。Cassandra会根据user_id的哈希值来决定数据存储的位置。 2.2 哈希分区示例思考 想象一下,如果我们有数百万个用户ID,使用哈希分区就可以保证每个节点都能承载一定比例的数据量,而不是全部集中在某一节点上,从而实现了负载均衡。 3. 范围分区策略 有序存储与查询的优势 3.1 范围分区概念 范围分区策略允许你按照指定列的顺序对数据进行分区,特别适用于那些需要按时间序列或者某种连续值进行查询的场景。比如,在处理像日志分析、查看金融交易记录这些情况时,我们完全可以按照时间戳来给数据分区,就像把不同时间段的日记整理到不同的文件夹里那样。 cql CREATE TABLE transaction_history ( account_id int, transaction_time timestamp, amount decimal, PRIMARY KEY ((account_id), transaction_time) ) WITH CLUSTERING ORDER BY (transaction_time DESC); 在这个例子中,我们创建了一个transaction_history表,account_id作为分区键,transaction_time作为排序键。这样一来,一个账户的所有交易记录都会像日记本一样,按照发生的时间顺序乖乖地排好队,储存在同一个“分区”里。当你需要查询时,就仿佛翻看日记一样,可以根据时间范围迅速找到你需要的交易信息,既高效又方便。 3.2 范围分区应用探讨 假设我们需要查询特定账户在某段时间内的交易记录,范围分区就能发挥巨大作用。在这种情况哈希分区虽然也不错,但是范围分区更能发挥它的超能力。想象一下,就像在图书馆找书一样,如果你知道书大概的类别和编号范围,你就可以直接去那个区域扫一眼,省时又高效。同样道理,范围分区利用Cassandra特有的排序功能,可以实现快速定位和扫描某个范围的数据,这样一来,在这种场景下的读取性能就更胜一筹啦。 4. 结论 选择合适的分区策略 Cassandra的哈希分区和范围分区各有优势,选择哪种策略取决于具体的应用场景和查询需求。在设计数据模型这回事儿上,咱们得像侦探破案一样,先摸透业务逻辑的来龙去脉,再揣摩出用户大概会怎么查询。然后,咱就可以灵活耍弄这些分区策略,把数据存储和检索效率往上提,让它们嗖嗖地跑起来。同时,咱也别忘了要兼顾数据分布的均衡性和查询速度,只有这样,才能让Cassandra这个分布式数据库充分发挥出它的威力,展现出最大的价值!毕竟,如同生活中的许多决策一样,关键在于权衡与适应,而非机械地遵循规则。
2023-11-17 22:46:52
578
春暖花开
Cassandra
...imestamp, PRIMARY KEY (lock_id) ) WITH default_time_to_live = 60; 这里,lock_id表示要锁定的资源标识,owner记录当前持有锁的节点信息,timestamp用于判断锁的有效期。设置TTL(Time To Live)这玩意儿,其实就像是给一把锁定了个“保质期”,为的是防止出现死锁这么个尴尬情况。想象一下,某个节点正握着一把锁,结果突然嗝屁了还没来得及把锁解开,这时候要是没个机制在一定时间后自动让锁失效,那不就僵持住了嘛。所以呢,这个TTL就是来扮演救场角色的,到点就把锁给自动释放了。 3. 使用Cassandra实现分布式锁的基本逻辑 为了获取锁,一个节点需要执行以下步骤: 1. 尝试插入锁定记录 - 使用INSERT IF NOT EXISTS语句尝试向distributed_lock表中插入一条记录。 cql INSERT INTO distributed_lock (lock_id, owner, timestamp) VALUES ('resource_1', 'node_A', toTimestamp(now())) IF NOT EXISTS; 如果插入成功,则说明当前无其他节点持有该锁,因此本节点获得了锁。 2. 检查插入结果 - Cassandra的INSERT语句会返回一个布尔值,指示插入是否成功。只有当插入成功时,节点才认为自己成功获取了锁。 3. 锁维护与释放 - 节点在持有锁期间应定期更新timestamp以延长锁的有效期,避免因超时而被误删。 - 在完成临界区操作后,节点通过DELETE语句释放锁: cql DELETE FROM distributed_lock WHERE lock_id = 'resource_1'; 4. 实际应用中的挑战与优化 然而,在实际场景中,直接使用上述简单方法可能会遇到一些挑战: - 竞争条件:多个节点可能同时尝试获取锁,单纯依赖INSERT IF NOT EXISTS可能导致冲突。 - 网络延迟:在网络分区或高延迟情况下,一个节点可能无法及时感知到锁已被其他节点获取。 为了解决这些问题,我们可以在客户端实现更复杂的算法,如采用CAS(Compare and Set)策略,或者引入租约机制并结合心跳维持,确保在获得锁后能够稳定持有并最终正确释放。 5. 结论与探讨 虽然Cassandra并不像Redis那样提供了内置的分布式锁API,但它凭借其强大的分布式能力和灵活的数据模型,仍然可以通过精心设计的查询语句和客户端逻辑实现分布式锁功能。当然,在真实生产环境中,实施这样的方案之前,需要充分考虑性能、容错性以及系统的整体复杂度。每个团队会根据自家业务的具体需求和擅长的技术工具箱,挑选出最合适、最趁手的解决方案。就像有时候,面对复杂的协调难题,还不如找一个经验丰富的“老司机”帮忙,比如用那些久经沙场、深受好评的分布式协调服务,像是ZooKeeper或者Consul,它们往往能提供更加省时省力又高效的解决之道。不过,对于已经深度集成Cassandra的应用而言,直接在Cassandra内实现分布式锁也不失为一种有创意且贴合实际的策略。
2023-03-13 10:56:59
503
追梦人
DorisDB
...ble WHERE key = 'some_value'; 通过分析这个执行计划,我们可以了解到查询涉及哪些分区、索引是否被有效利用等关键信息,从而为优化工作找准方向。 3. 优化策略一 合理设计表结构与分区策略 - 列选择性优化:由于DorisDB是列式存储,高选择性的列(即唯一或接近唯一的列)能更好地发挥其优势。例如,对于用户ID这样的列,将其设为主键或构建Bloom Filter索引,可以大幅提升查询性能。 sql -- 创建包含主键的表 CREATE TABLE my_table ( user_id INT PRIMARY KEY, ... ); - 分区设计:根据业务需求和数据分布特性,合理设计分区策略至关重要。比如,咱们可以按照时间段给数据分区,这样做的好处可多了。首先呢,能大大减少需要扫描的数据量,让查询过程不再那么费力;其次,还能巧妙地利用局部性原理,就像你找东西时先从最近的地方找起一样,这样就能显著提升查询的效率,让你的数据查找嗖嗖快! sql -- 按天分区 CREATE TABLE my_table ( ... ) PARTITION BY RANGE (dt) ( PARTITION p20220101 VALUES LESS THAN ("2022-01-02"), PARTITION p20220102 VALUES LESS THAN ("2022-01-03"), ... ); 4. 优化策略二 SQL查询优化 - 避免全表扫描:尽量在WHERE子句中指定明确的过滤条件,利用索引加速查询。例如,假设我们已经为user_id字段创建了索引,那么以下查询会更高效: sql SELECT FROM my_table WHERE user_id = 123; - 减少数据传输量:只查询需要的列,避免使用SELECT 。同时,合理运用聚合函数和分组,避免不必要的计算和排序。 sql -- 只查询特定列,避免全表扫描 SELECT user_name, email FROM my_table WHERE user_id = 123; -- 合理运用GROUP BY和聚合函数 SELECT COUNT(), category FROM my_table GROUP BY category; 5. 优化策略三 系统配置调优 DorisDB提供了丰富的系统参数供用户调整以适应不同场景下的性能需求。比方说,你可以通过调节max_scan_range_length这个参数,来决定每次查询时最多能扫描多少数据范围,就像控制扫地机器人的清扫范围那样。再者,通过巧妙调整那些和内存相关的设置,就能让服务器资源得到充分且高效的利用,就像精心安排储物空间,让每个角落都物尽其用。 6. 结语 优化DorisDB的SQL查询性能是一个综合且持续的过程,需要结合业务特点和数据特征,从表结构设计、查询语句编写到系统配置调整等多个维度着手。每个环节都需细心打磨,才能使DorisDB在大数据洪流中游刃有余,提供更为出色的服务。每一次对DorisDB的优化,都是我们携手这位好伙伴,一起摸爬滚打、不断解锁新技能、共同进步的重要印记。这样一来,咱的数据分析之路也能走得更顺溜,效率嗖嗖往上涨,就像坐上了火箭一样快呢!
2023-05-07 10:47:25
500
繁华落尽
转载文章
...全 也可被用作分布式Key-Value存储 事务处理与数据分析处理混合型数据库 支持丰富的SQL语句类型,比如:关联子查询 支持绝大部分PostgreSQL的SQL语句 分布式多版本并发控制(MVCC:Multi-version Concurrency Control) 支持JSON和XML格式 Postgres-XL缺少的功能 内建的高可用机制 使用外部机制实现高可能,如:Corosync/Pacemaker 有未来功能提升的空间 增加节点/重新分片数据(re-shard)的简便性 数据重分布(redistribution)期间会锁表 可采用预分片(pre-shard)方式解决,在同台物理服务器上建立多个数据节点,每个节点存储一个数据分片。数据重分布时,将一些数据节点迁出即可 某些外键、唯一性约束功能 Postgres-XL架构 [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-M9lFuEIP-1640133702200)(./assets/postgre-xl.jpg)] 基于开源项目Postgres-XC XL增加了MPP,允许数据节点间直接通讯,交换复杂跨节点关联查询相关数据信息,减少协调器负载。 多个协调器(Coordinator) 应用程序的数据库连入点 分析查询语句,生成执行计划 多个数据节点(DataNode) 实际的数据存储 数据自动打散分布到集群中各数据节点 本地执行查询 一个查询在所有相关节点上并行查询 全局事务管理器(GTM:Global Transaction Manager) 提供事务间一致性视图 部署GTM Proxy实例,以提高性能 Postgre-XL主要组件 GTM (Global Transaction Manager) - 全局事务管理器 GTM是Postgres-XL的一个关键组件,用于提供一致的事务管理和元组可见性控制。 GTM Standby GTM的备节点,在pgxc,pgxl中,GTM控制所有的全局事务分配,如果出现问题,就会导致整个集群不可用,为了增加可用性,增加该备用节点。当GTM出现问题时,GTM Standby可以升级为GTM,保证集群正常工作。 GTM-Proxy GTM需要与所有的Coordinators通信,为了降低压力,可以在每个Coordinator机器上部署一个GTM-Proxy。 Coordinator --协调器 协调器是应用程序到数据库的接口。它的作用类似于传统的PostgreSQL后台进程,但是协调器不存储任何实际数据。实际数据由数据节点存储。协调器接收SQL语句,根据需要获取全局事务Id和全局快照,确定涉及哪些数据节点,并要求它们执行(部分)语句。当向数据节点发出语句时,它与GXID和全局快照相关联,以便多版本并发控制(MVCC)属性扩展到集群范围。 Datanode --数据节点 用于实际存储数据。表可以分布在各个数据节点之间,也可以复制到所有数据节点。数据节点没有整个数据库的全局视图,它只负责本地存储的数据。接下来,协调器将检查传入语句,并制定子计划。然后,根据需要将这些数据连同GXID和全局快照一起传输到涉及的每个数据节点。数据节点可以在不同的会话中接收来自各个协调器的请求。但是,由于每个事务都是惟一标识的,并且与一致的(全局)快照相关联,所以每个数据节点都可以在其事务和快照上下文中正确执行。 Postgres-XL继承了PostgreSQL Postgres-XL是PostgreSQL的扩展并继承了其很多特性: 复杂查询 外键 触发器 视图 事务 MVCC(多版本控制) 此外,类似于PostgreSQL,用户可以通过多种方式扩展Postgres-XL,例如添加新的 数据类型 函数 操作 聚合函数 索引类型 过程语言 安装 环境说明 由于资源有限,gtm一台、另外两台身兼数职。 主机名 IP 角色 端口 nodename 数据目录 gtm 192.168.20.132 GTM 6666 gtm /nodes/gtm 协调器 5432 coord1 /nodes/coordinator xl1 192.168.20.133 数据节点 5433 node1 /nodes/pgdata gtm代理 6666 gtmpoxy01 /nodes/gtm_pxy1 协调器 5432 coord2 /nodes/coordinator xl2 192.168.20.134 数据节点 5433 node2 /nodes/pgdata gtm代理 6666 gtmpoxy02 /nodes/gtm_pxy2 要求 GNU make版本 3.8及以上版本 [root@pg ~] make --versionGNU Make 3.82Built for x86_64-redhat-linux-gnuCopyright (C) 2010 Free Software Foundation, Inc.License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>This is free software: you are free to change and redistribute it.There is NO WARRANTY, to the extent permitted by law. 需安装GCC包 需安装tar包 用于解压缩文件 默认需要GNU Readline library 其作用是可以让psql命令行记住执行过的命令,并且可以通过键盘上下键切换命令。但是可以通过--without-readline禁用这个特性,或者可以指定--withlibedit-preferred选项来使用libedit 默认使用zlib压缩库 可通过--without-zlib选项来禁用 配置hosts 所有主机上都配置 [root@xl2 11] cat /etc/hosts127.0.0.1 localhost192.168.20.132 gtm192.168.20.133 xl1192.168.20.134 xl2 关闭防火墙、Selinux 所有主机都执行 关闭防火墙: [root@gtm ~] systemctl stop firewalld.service[root@gtm ~] systemctl disable firewalld.service selinux设置: [root@gtm ~]vim /etc/selinux/config 设置SELINUX=disabled,保存退出。 This file controls the state of SELinux on the system. SELINUX= can take one of these three values: enforcing - SELinux security policy is enforced. permissive - SELinux prints warnings instead of enforcing. disabled - No SELinux policy is loaded.SELINUX=disabled SELINUXTYPE= can take one of three two values: targeted - Targeted processes are protected, minimum - Modification of targeted policy. Only selected processes are protected. mls - Multi Level Security protection. 安装依赖包 所有主机上都执行 yum install -y flex bison readline-devel zlib-devel openjade docbook-style-dsssl gcc 创建用户 所有主机上都执行 [root@gtm ~] useradd postgres[root@gtm ~] passwd postgres[root@gtm ~] su - postgres[root@gtm ~] mkdir ~/.ssh[root@gtm ~] chmod 700 ~/.ssh 配置SSH免密登录 仅仅在gtm节点配置如下操作: [root@gtm ~] su - postgres[postgres@gtm ~] ssh-keygen -t rsa[postgres@gtm ~] cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys[postgres@gtm ~] chmod 600 ~/.ssh/authorized_keys 将刚生成的认证文件拷贝到xl1到xl2中,使得gtm节点可以免密码登录xl1~xl2的任意一个节点: [postgres@gtm ~] scp ~/.ssh/authorized_keys postgres@xl1:~/.ssh/[postgres@gtm ~] scp ~/.ssh/authorized_keys postgres@xl2:~/.ssh/ 对所有提示都不要输入,直接enter下一步。直到最后,因为第一次要求输入目标机器的用户密码,输入即可。 下载源码 下载地址:https://www.postgres-xl.org/download/ [root@slave ~] ll postgres-xl-10r1.1.tar.gz-rw-r--r-- 1 root root 28121666 May 30 05:21 postgres-xl-10r1.1.tar.gz 编译、安装Postgres-XL 所有节点都安装,编译需要一点时间,最好同时进行编译。 [root@slave ~] tar xvf postgres-xl-10r1.1.tar.gz[root@slave ~] ./configure --prefix=/home/postgres/pgxl/[root@slave ~] make[root@slave ~] make install[root@slave ~] cd contrib/ --安装必要的工具,在gtm节点上安装即可[root@slave ~] make[root@slave ~] make install 配置环境变量 所有节点都要配置 进入postgres用户,修改其环境变量,开始编辑 [root@gtm ~]su - postgres[postgres@gtm ~]vi .bashrc --不是.bash_profile 在打开的文件末尾,新增如下变量配置: export PGHOME=/home/postgres/pgxlexport LD_LIBRARY_PATH=$PGHOME/lib:$LD_LIBRARY_PATHexport PATH=$PGHOME/bin:$PATH 按住esc,然后输入:wq!保存退出。输入以下命令对更改重启生效。 [postgres@gtm ~] source .bashrc --不是.bash_profile 输入以下语句,如果输出变量结果,代表生效 [postgres@gtm ~] echo $PGHOME 应该输出/home/postgres/pgxl代表生效 配置集群 生成pgxc_ctl.conf配置文件 [postgres@gtm ~] pgxc_ctl prepare/bin/bashInstalling pgxc_ctl_bash script as /home/postgres/pgxl/pgxc_ctl/pgxc_ctl_bash.ERROR: File "/home/postgres/pgxl/pgxc_ctl/pgxc_ctl.conf" not found or not a regular file. No such file or directoryInstalling pgxc_ctl_bash script as /home/postgres/pgxl/pgxc_ctl/pgxc_ctl_bash.Reading configuration using /home/postgres/pgxl/pgxc_ctl/pgxc_ctl_bash --home /home/postgres/pgxl/pgxc_ctl --configuration /home/postgres/pgxl/pgxc_ctl/pgxc_ctl.confFinished reading configuration. PGXC_CTL START Current directory: /home/postgres/pgxl/pgxc_ctl 配置pgxc_ctl.conf 新建/home/postgres/pgxc_ctl/pgxc_ctl.conf文件,编辑如下: 对着模板文件一个一个修改,否则会造成初始化过程出现各种神奇问题。 pgxcInstallDir=$PGHOMEpgxlDATA=$PGHOME/data pgxcOwner=postgres---- GTM Master -----------------------------------------gtmName=gtmgtmMasterServer=gtmgtmMasterPort=6666gtmMasterDir=$pgxlDATA/nodes/gtmgtmSlave=y Specify y if you configure GTM Slave. Otherwise, GTM slave will not be configured and all the following variables will be reset.gtmSlaveName=gtmSlavegtmSlaveServer=gtm value none means GTM slave is not available. Give none if you don't configure GTM Slave.gtmSlavePort=20001 Not used if you don't configure GTM slave.gtmSlaveDir=$pgxlDATA/nodes/gtmSlave Not used if you don't configure GTM slave.---- GTM-Proxy Master -------gtmProxyDir=$pgxlDATA/nodes/gtm_proxygtmProxy=y gtmProxyNames=(gtm_pxy1 gtm_pxy2) gtmProxyServers=(xl1 xl2) gtmProxyPorts=(6666 6666) gtmProxyDirs=($gtmProxyDir $gtmProxyDir) ---- Coordinators ---------coordMasterDir=$pgxlDATA/nodes/coordcoordNames=(coord1 coord2) coordPorts=(5432 5432) poolerPorts=(6667 6667) coordPgHbaEntries=(0.0.0.0/0)coordMasterServers=(xl1 xl2) coordMasterDirs=($coordMasterDir $coordMasterDir)coordMaxWALsernder=0 没设置备份节点,设置为0coordMaxWALSenders=($coordMaxWALsernder $coordMaxWALsernder) 数量保持和coordMasterServers一致coordSlave=n---- Datanodes ----------datanodeMasterDir=$pgxlDATA/nodes/dn_masterprimaryDatanode=xl1 主数据节点datanodeNames=(node1 node2)datanodePorts=(5433 5433) datanodePoolerPorts=(6668 6668) datanodePgHbaEntries=(0.0.0.0/0)datanodeMasterServers=(xl1 xl2)datanodeMasterDirs=($datanodeMasterDir $datanodeMasterDir)datanodeMaxWalSender=4datanodeMaxWALSenders=($datanodeMaxWalSender $datanodeMaxWalSender) 集群初始化,启动,停止 初始化 pgxc_ctl -c /home/postgres/pgxc_ctl/pgxc_ctl.conf init all 输出结果: /bin/bashInstalling pgxc_ctl_bash script as /home/postgres/pgxc_ctl/pgxc_ctl_bash.Installing pgxc_ctl_bash script as /home/postgres/pgxc_ctl/pgxc_ctl_bash.Reading configuration using /home/postgres/pgxc_ctl/pgxc_ctl_bash --home /home/postgres/pgxc_ctl --configuration /home/postgres/pgxc_ctl/pgxc_ctl.conf/home/postgres/pgxc_ctl/pgxc_ctl.conf: line 189: $coordExtraConfig: ambiguous redirectFinished reading configuration. PGXC_CTL START Current directory: /home/postgres/pgxc_ctlStopping all the coordinator masters.Stopping coordinator master coord1.Stopping coordinator master coord2.pg_ctl: directory "/home/postgres/pgxc/nodes/coord/coord1" does not existpg_ctl: directory "/home/postgres/pgxc/nodes/coord/coord2" does not existDone.Stopping all the datanode masters.Stopping datanode master datanode1.Stopping datanode master datanode2.pg_ctl: PID file "/home/postgres/pgxc/nodes/datanode/datanode1/postmaster.pid" does not existIs server running?Done.Stop GTM masterwaiting for server to shut down.... doneserver stopped[postgres@gtm ~]$ echo $PGHOME/home/postgres/pgxl[postgres@gtm ~]$ ll /home/postgres/pgxl/pgxc/nodes/gtm/gtm.^C[postgres@gtm ~]$ pgxc_ctl -c /home/postgres/pgxc_ctl/pgxc_ctl.conf init all/bin/bashInstalling pgxc_ctl_bash script as /home/postgres/pgxc_ctl/pgxc_ctl_bash.Installing pgxc_ctl_bash script as /home/postgres/pgxc_ctl/pgxc_ctl_bash.Reading configuration using /home/postgres/pgxc_ctl/pgxc_ctl_bash --home /home/postgres/pgxc_ctl --configuration /home/postgres/pgxc_ctl/pgxc_ctl.conf/home/postgres/pgxc_ctl/pgxc_ctl.conf: line 189: $coordExtraConfig: ambiguous redirectFinished reading configuration. PGXC_CTL START Current directory: /home/postgres/pgxc_ctlInitialize GTM masterERROR: target directory (/home/postgres/pgxc/nodes/gtm) exists and not empty. Skip GTM initilializationDone.Start GTM masterserver startingInitialize all the coordinator masters.Initialize coordinator master coord1.ERROR: target coordinator master coord1 is running now. Skip initilialization.Initialize coordinator master coord2.The files belonging to this database system will be owned by user "postgres".This user must also own the server process.The database cluster will be initialized with locale "en_US.UTF-8".The default database encoding has accordingly been set to "UTF8".The default text search configuration will be set to "english".Data page checksums are disabled.fixing permissions on existing directory /home/postgres/pgxc/nodes/coord/coord2 ... okcreating subdirectories ... okselecting default max_connections ... 100selecting default shared_buffers ... 128MBselecting dynamic shared memory implementation ... posixcreating configuration files ... okrunning bootstrap script ... okperforming post-bootstrap initialization ... creating cluster information ... oksyncing data to disk ... okfreezing database template0 ... okfreezing database template1 ... okfreezing database postgres ... okWARNING: enabling "trust" authentication for local connectionsYou can change this by editing pg_hba.conf or using the option -A, or--auth-local and --auth-host, the next time you run initdb.Success.Done.Starting coordinator master.Starting coordinator master coord1ERROR: target coordinator master coord1 is already running now. Skip initialization.Starting coordinator master coord22019-05-30 21:09:25.562 EDT [2148] LOG: listening on IPv4 address "0.0.0.0", port 54322019-05-30 21:09:25.562 EDT [2148] LOG: listening on IPv6 address "::", port 54322019-05-30 21:09:25.563 EDT [2148] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432"2019-05-30 21:09:25.601 EDT [2149] LOG: database system was shut down at 2019-05-30 21:09:22 EDT2019-05-30 21:09:25.605 EDT [2148] LOG: database system is ready to accept connections2019-05-30 21:09:25.612 EDT [2156] LOG: cluster monitor startedDone.Initialize all the datanode masters.Initialize the datanode master datanode1.Initialize the datanode master datanode2.The files belonging to this database system will be owned by user "postgres".This user must also own the server process.The database cluster will be initialized with locale "en_US.UTF-8".The default database encoding has accordingly been set to "UTF8".The default text search configuration will be set to "english".Data page checksums are disabled.fixing permissions on existing directory /home/postgres/pgxc/nodes/datanode/datanode1 ... okcreating subdirectories ... okselecting default max_connections ... 100selecting default shared_buffers ... 128MBselecting dynamic shared memory implementation ... posixcreating configuration files ... okrunning bootstrap script ... okperforming post-bootstrap initialization ... creating cluster information ... oksyncing data to disk ... okfreezing database template0 ... okfreezing database template1 ... okfreezing database postgres ... okWARNING: enabling "trust" authentication for local connectionsYou can change this by editing pg_hba.conf or using the option -A, or--auth-local and --auth-host, the next time you run initdb.Success.The files belonging to this database system will be owned by user "postgres".This user must also own the server process.The database cluster will be initialized with locale "en_US.UTF-8".The default database encoding has accordingly been set to "UTF8".The default text search configuration will be set to "english".Data page checksums are disabled.fixing permissions on existing directory /home/postgres/pgxc/nodes/datanode/datanode2 ... okcreating subdirectories ... okselecting default max_connections ... 100selecting default shared_buffers ... 128MBselecting dynamic shared memory implementation ... posixcreating configuration files ... okrunning bootstrap script ... okperforming post-bootstrap initialization ... creating cluster information ... oksyncing data to disk ... okfreezing database template0 ... okfreezing database template1 ... okfreezing database postgres ... okWARNING: enabling "trust" authentication for local connectionsYou can change this by editing pg_hba.conf or using the option -A, or--auth-local and --auth-host, the next time you run initdb.Success.Done.Starting all the datanode masters.Starting datanode master datanode1.WARNING: datanode master datanode1 is running now. Skipping.Starting datanode master datanode2.2019-05-30 21:09:33.352 EDT [2404] LOG: listening on IPv4 address "0.0.0.0", port 154322019-05-30 21:09:33.352 EDT [2404] LOG: listening on IPv6 address "::", port 154322019-05-30 21:09:33.355 EDT [2404] LOG: listening on Unix socket "/tmp/.s.PGSQL.15432"2019-05-30 21:09:33.392 EDT [2404] LOG: redirecting log output to logging collector process2019-05-30 21:09:33.392 EDT [2404] HINT: Future log output will appear in directory "pg_log".Done.psql: FATAL: no pg_hba.conf entry for host "192.168.20.132", user "postgres", database "postgres"psql: FATAL: no pg_hba.conf entry for host "192.168.20.132", user "postgres", database "postgres"Done.psql: FATAL: no pg_hba.conf entry for host "192.168.20.132", user "postgres", database "postgres"psql: FATAL: no pg_hba.conf entry for host "192.168.20.132", user "postgres", database "postgres"Done.[postgres@gtm ~]$ pgxc_ctl -c /home/postgres/pgxc_ctl/pgxc_ctl.conf stop all/bin/bashInstalling pgxc_ctl_bash script as /home/postgres/pgxc_ctl/pgxc_ctl_bash.Installing pgxc_ctl_bash script as /home/postgres/pgxc_ctl/pgxc_ctl_bash.Reading configuration using /home/postgres/pgxc_ctl/pgxc_ctl_bash --home /home/postgres/pgxc_ctl --configuration /home/postgres/pgxc_ctl/pgxc_ctl.conf/home/postgres/pgxc_ctl/pgxc_ctl.conf: line 189: $coordExtraConfig: ambiguous redirectFinished reading configuration. PGXC_CTL START Current directory: /home/postgres/pgxc_ctlStopping all the coordinator masters.Stopping coordinator master coord1.Stopping coordinator master coord2.pg_ctl: directory "/home/postgres/pgxc/nodes/coord/coord1" does not existDone.Stopping all the datanode masters.Stopping datanode master datanode1.Stopping datanode master datanode2.pg_ctl: PID file "/home/postgres/pgxc/nodes/datanode/datanode1/postmaster.pid" does not existIs server running?Done.Stop GTM masterwaiting for server to shut down.... doneserver stopped[postgres@gtm ~]$ pgxc_ctl/bin/bashInstalling pgxc_ctl_bash script as /home/postgres/pgxc_ctl/pgxc_ctl_bash.Installing pgxc_ctl_bash script as /home/postgres/pgxc_ctl/pgxc_ctl_bash.Reading configuration using /home/postgres/pgxc_ctl/pgxc_ctl_bash --home /home/postgres/pgxc_ctl --configuration /home/postgres/pgxc_ctl/pgxc_ctl.conf/home/postgres/pgxc_ctl/pgxc_ctl.conf: line 189: $coordExtraConfig: ambiguous redirectFinished reading configuration. PGXC_CTL START Current directory: /home/postgres/pgxc_ctlPGXC monitor allNot running: gtm masterRunning: coordinator master coord1Not running: coordinator master coord2Running: datanode master datanode1Not running: datanode master datanode2PGXC stop coordinator master coord1Stopping coordinator master coord1.pg_ctl: directory "/home/postgres/pgxc/nodes/coord/coord1" does not existDone.PGXC stop datanode master datanode1Stopping datanode master datanode1.pg_ctl: PID file "/home/postgres/pgxc/nodes/datanode/datanode1/postmaster.pid" does not existIs server running?Done.PGXC monitor allNot running: gtm masterRunning: coordinator master coord1Not running: coordinator master coord2Running: datanode master datanode1Not running: datanode master datanode2PGXC monitor allNot running: gtm masterNot running: coordinator master coord1Not running: coordinator master coord2Not running: datanode master datanode1Not running: datanode master datanode2PGXC exit[postgres@gtm ~]$ pgxc_ctl -c /home/postgres/pgxc_ctl/pgxc_ctl.conf init all/bin/bashInstalling pgxc_ctl_bash script as /home/postgres/pgxc_ctl/pgxc_ctl_bash.Installing pgxc_ctl_bash script as /home/postgres/pgxc_ctl/pgxc_ctl_bash.Reading configuration using /home/postgres/pgxc_ctl/pgxc_ctl_bash --home /home/postgres/pgxc_ctl --configuration /home/postgres/pgxc_ctl/pgxc_ctl.conf/home/postgres/pgxc_ctl/pgxc_ctl.conf: line 189: $coordExtraConfig: ambiguous redirectFinished reading configuration. PGXC_CTL START Current directory: /home/postgres/pgxc_ctlInitialize GTM masterERROR: target directory (/home/postgres/pgxc/nodes/gtm) exists and not empty. Skip GTM initilializationDone.Start GTM masterserver startingInitialize all the coordinator masters.Initialize coordinator master coord1.Initialize coordinator master coord2.The files belonging to this database system will be owned by user "postgres".This user must also own the server process.The database cluster will be initialized with locale "en_US.UTF-8".The default database encoding has accordingly been set to "UTF8".The default text search configuration will be set to "english".Data page checksums are disabled.fixing permissions on existing directory /home/postgres/pgxc/nodes/coord/coord1 ... okcreating subdirectories ... okselecting default max_connections ... 100selecting default shared_buffers ... 128MBselecting dynamic shared memory implementation ... posixcreating configuration files ... okrunning bootstrap script ... okperforming post-bootstrap initialization ... creating cluster information ... oksyncing data to disk ... okfreezing database template0 ... okfreezing database template1 ... okfreezing database postgres ... okWARNING: enabling "trust" authentication for local connectionsYou can change this by editing pg_hba.conf or using the option -A, or--auth-local and --auth-host, the next time you run initdb.Success.The files belonging to this database system will be owned by user "postgres".This user must also own the server process.The database cluster will be initialized with locale "en_US.UTF-8".The default database encoding has accordingly been set to "UTF8".The default text search configuration will be set to "english".Data page checksums are disabled.fixing permissions on existing directory /home/postgres/pgxc/nodes/coord/coord2 ... okcreating subdirectories ... okselecting default max_connections ... 100selecting default shared_buffers ... 128MBselecting dynamic shared memory implementation ... posixcreating configuration files ... okrunning bootstrap script ... okperforming post-bootstrap initialization ... creating cluster information ... oksyncing data to disk ... okfreezing database template0 ... okfreezing database template1 ... okfreezing database postgres ... okWARNING: enabling "trust" authentication for local connectionsYou can change this by editing pg_hba.conf or using the option -A, or--auth-local and --auth-host, the next time you run initdb.Success.Done.Starting coordinator master.Starting coordinator master coord1Starting coordinator master coord22019-05-30 21:13:03.998 EDT [25137] LOG: listening on IPv4 address "0.0.0.0", port 54322019-05-30 21:13:03.998 EDT [25137] LOG: listening on IPv6 address "::", port 54322019-05-30 21:13:04.000 EDT [25137] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432"2019-05-30 21:13:04.038 EDT [25138] LOG: database system was shut down at 2019-05-30 21:13:00 EDT2019-05-30 21:13:04.042 EDT [25137] LOG: database system is ready to accept connections2019-05-30 21:13:04.049 EDT [25145] LOG: cluster monitor started2019-05-30 21:13:04.020 EDT [2730] LOG: listening on IPv4 address "0.0.0.0", port 54322019-05-30 21:13:04.020 EDT [2730] LOG: listening on IPv6 address "::", port 54322019-05-30 21:13:04.021 EDT [2730] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432"2019-05-30 21:13:04.057 EDT [2731] LOG: database system was shut down at 2019-05-30 21:13:00 EDT2019-05-30 21:13:04.061 EDT [2730] LOG: database system is ready to accept connections2019-05-30 21:13:04.062 EDT [2738] LOG: cluster monitor startedDone.Initialize all the datanode masters.Initialize the datanode master datanode1.Initialize the datanode master datanode2.The files belonging to this database system will be owned by user "postgres".This user must also own the server process.The database cluster will be initialized with locale "en_US.UTF-8".The default database encoding has accordingly been set to "UTF8".The default text search configuration will be set to "english".Data page checksums are disabled.fixing permissions on existing directory /home/postgres/pgxc/nodes/datanode/datanode1 ... okcreating subdirectories ... okselecting default max_connections ... 100selecting default shared_buffers ... 128MBselecting dynamic shared memory implementation ... posixcreating configuration files ... okrunning bootstrap script ... okperforming post-bootstrap initialization ... creating cluster information ... oksyncing data to disk ... okfreezing database template0 ... okfreezing database template1 ... okfreezing database postgres ... okWARNING: enabling "trust" authentication for local connectionsYou can change this by editing pg_hba.conf or using the option -A, or--auth-local and --auth-host, the next time you run initdb.Success.The files belonging to this database system will be owned by user "postgres".This user must also own the server process.The database cluster will be initialized with locale "en_US.UTF-8".The default database encoding has accordingly been set to "UTF8".The default text search configuration will be set to "english".Data page checksums are disabled.fixing permissions on existing directory /home/postgres/pgxc/nodes/datanode/datanode2 ... okcreating subdirectories ... okselecting default max_connections ... 100selecting default shared_buffers ... 128MBselecting dynamic shared memory implementation ... posixcreating configuration files ... okrunning bootstrap script ... okperforming post-bootstrap initialization ... creating cluster information ... oksyncing data to disk ... okfreezing database template0 ... okfreezing database template1 ... okfreezing database postgres ... okWARNING: enabling "trust" authentication for local connectionsYou can change this by editing pg_hba.conf or using the option -A, or--auth-local and --auth-host, the next time you run initdb.Success.Done.Starting all the datanode masters.Starting datanode master datanode1.Starting datanode master datanode2.2019-05-30 21:13:12.077 EDT [25392] LOG: listening on IPv4 address "0.0.0.0", port 154322019-05-30 21:13:12.077 EDT [25392] LOG: listening on IPv6 address "::", port 154322019-05-30 21:13:12.079 EDT [25392] LOG: listening on Unix socket "/tmp/.s.PGSQL.15432"2019-05-30 21:13:12.114 EDT [25392] LOG: redirecting log output to logging collector process2019-05-30 21:13:12.114 EDT [25392] HINT: Future log output will appear in directory "pg_log".2019-05-30 21:13:12.079 EDT [2985] LOG: listening on IPv4 address "0.0.0.0", port 154322019-05-30 21:13:12.079 EDT [2985] LOG: listening on IPv6 address "::", port 154322019-05-30 21:13:12.081 EDT [2985] LOG: listening on Unix socket "/tmp/.s.PGSQL.15432"2019-05-30 21:13:12.117 EDT [2985] LOG: redirecting log output to logging collector process2019-05-30 21:13:12.117 EDT [2985] HINT: Future log output will appear in directory "pg_log".Done.psql: FATAL: no pg_hba.conf entry for host "192.168.20.132", user "postgres", database "postgres"psql: FATAL: no pg_hba.conf entry for host "192.168.20.132", user "postgres", database "postgres"Done.psql: FATAL: no pg_hba.conf entry for host "192.168.20.132", user "postgres", database "postgres"psql: FATAL: no pg_hba.conf entry for host "192.168.20.132", user "postgres", database "postgres"Done. 启动 pgxc_ctl -c /home/postgres/pgxc_ctl/pgxc_ctl.conf start all 关闭 pgxc_ctl -c /home/postgres/pgxc_ctl/pgxc_ctl.conf stop all 查看集群状态 [postgres@gtm ~]$ pgxc_ctl monitor all/bin/bashInstalling pgxc_ctl_bash script as /home/postgres/pgxc_ctl/pgxc_ctl_bash.Installing pgxc_ctl_bash script as /home/postgres/pgxc_ctl/pgxc_ctl_bash.Reading configuration using /home/postgres/pgxc_ctl/pgxc_ctl_bash --home /home/postgres/pgxc_ctl --configuration /home/postgres/pgxc_ctl/pgxc_ctl.conf/home/postgres/pgxc_ctl/pgxc_ctl.conf: line 189: $coordExtraConfig: ambiguous redirectFinished reading configuration. PGXC_CTL START Current directory: /home/postgres/pgxc_ctlRunning: gtm masterRunning: coordinator master coord1Running: coordinator master coord2Running: datanode master datanode1Running: datanode master datanode2 配置集群信息 分别在数据节点、协调器节点上分别执行以下命令: 注:本节点只执行修改操作即可(alert node),其他节点执行创建命令(create node)。因为本节点已经包含本节点的信息。 create node coord1 with (type=coordinator,host=xl1, port=5432);create node coord2 with (type=coordinator,host=xl2, port=5432);alter node coord1 with (type=coordinator,host=xl1, port=5432);alter node coord2 with (type=coordinator,host=xl2, port=5432);create node datanode1 with (type=datanode, host=xl1,port=15432,primary=true,PREFERRED);create node datanode2 with (type=datanode, host=xl2,port=15432);alter node datanode1 with (type=datanode, host=xl1,port=15432,primary=true,PREFERRED);alter node datanode2 with (type=datanode, host=xl2,port=15432);select pgxc_pool_reload(); 分别登陆数据节点、协调器节点验证 postgres= select from pgxc_node;node_name | node_type | node_port | node_host | nodeis_primary | nodeis_preferred | node_id-----------+-----------+-----------+-----------+----------------+------------------+-------------coord1 | C | 5432 | xl1 | f | f | 1885696643coord2 | C | 5432 | xl2 | f | f | -1197102633datanode2 | D | 15432 | xl2 | f | f | -905831925datanode1 | D | 15432 | xl1 | t | f | 888802358(4 rows) 测试 插入数据 在数据节点1,执行相关操作。 通过协调器端口登录PG [postgres@xl1 ~]$ psql -p 5432psql (PGXL 10r1.1, based on PG 10.6 (Postgres-XL 10r1.1))Type "help" for help.postgres= create database lei;CREATE DATABASEpostgres= \c lei;You are now connected to database "lei" as user "postgres".lei= create table test1(id int,name text);CREATE TABLElei= insert into test1(id,name) select generate_series(1,8),'测试';INSERT 0 8lei= select from test1;id | name----+------1 | 测试2 | 测试5 | 测试6 | 测试8 | 测试3 | 测试4 | 测试7 | 测试(8 rows) 注:默认创建的表为分布式表,也就是每个数据节点值存储表的部分数据。关于表类型具体说明,下面有说明。 通过15432端口登录数据节点,查看数据 有5条数据 [postgres@xl1 ~]$ psql -p 15432psql (PGXL 10r1.1, based on PG 10.6 (Postgres-XL 10r1.1))Type "help" for help.postgres= \c lei;You are now connected to database "lei" as user "postgres".lei= select from test1;id | name----+------1 | 测试2 | 测试5 | 测试6 | 测试8 | 测试(5 rows) 登录到节点2,查看数据 有3条数据 [postgres@xl2 ~]$ psql -p15432psql (PGXL 10r1.1, based on PG 10.6 (Postgres-XL 10r1.1))Type "help" for help.postgres= \c lei;You are now connected to database "lei" as user "postgres".lei= select from test1;id | name----+------3 | 测试4 | 测试7 | 测试(3 rows) 两个节点的数据加起来整个8条,没有问题。 至此Postgre-XL集群搭建完成。 创建数据库、表时可能会出现以下错误: ERROR: Failed to get pooled connections 是因为pg_hba.conf配置不对,所有节点加上host all all 192.168.20.0/0 trust并重启集群即可。 ERROR: No Datanode defined in cluster 首先确认是否创建了数据节点,也就是create node相关的命令。如果创建了则执行select pgxc_pool_reload();使其生效即可。 集群管理与应用 表类型说明 REPLICATION表:各个datanode节点中,表的数据完全相同,也就是说,插入数据时,会分别在每个datanode节点插入相同数据。读数据时,只需要读任意一个datanode节点上的数据。 建表语法: CREATE TABLE repltab (col1 int, col2 int) DISTRIBUTE BY REPLICATION; DISTRIBUTE :会将插入的数据,按照拆分规则,分配到不同的datanode节点中存储,也就是sharding技术。每个datanode节点只保存了部分数据,通过coordinate节点可以查询完整的数据视图。 CREATE TABLE disttab(col1 int, col2 int, col3 text) DISTRIBUTE BY HASH(col1); 模拟数据插入 任意登录一个coordinate节点进行建表操作 [postgres@gtm ~]$ psql -h xl1 -p 5432 -U postgrespostgres= INSERT INTO disttab SELECT generate_series(1,100), generate_series(101, 200), 'foo';INSERT 0 100postgres= INSERT INTO repltab SELECT generate_series(1,100), generate_series(101, 200);INSERT 0 100 查看数据分布结果: DISTRIBUTE表分布结果 postgres= SELECT xc_node_id, count() FROM disttab GROUP BY xc_node_id;xc_node_id | count ------------+-------1148549230 | 42-927910690 | 58(2 rows) REPLICATION表分布结果 postgres= SELECT count() FROM repltab;count -------100(1 row) 查看另一个datanode2中repltab表结果 [postgres@datanode2 pgxl9.5]$ psql -p 15432psql (PGXL 10r1.1, based on PG 10.6 (Postgres-XL 10r1.1))Type "help" for help.postgres= SELECT count() FROM repltab;count -------100(1 row) 结论:REPLICATION表中,datanode1,datanode2中表是全部数据,一模一样。而DISTRIBUTE表,数据散落近乎平均分配到了datanode1,datanode2节点中。 新增数据节点与数据重分布 在线新增节点、并重新分布数据。 新增datanode节点 在gtm集群管理节点上执行pgxc_ctl命令 [postgres@gtm ~]$ pgxc_ctl/bin/bashInstalling pgxc_ctl_bash script as /home/postgres/pgxc_ctl/pgxc_ctl_bash.Installing pgxc_ctl_bash script as /home/postgres/pgxc_ctl/pgxc_ctl_bash.Reading configuration using /home/postgres/pgxc_ctl/pgxc_ctl_bash --home /home/postgres/pgxc_ctl --configuration /home/postgres/pgxc_ctl/pgxc_ctl.confFinished reading configuration. PGXC_CTL START Current directory: /home/postgres/pgxc_ctlPGXC 在服务器xl3上,新增一个master角色的datanode节点,名称是datanode3 端口号暂定5430,pool master暂定6669 ,指定好数据目录位置,从两个节点升级到3个节点,之后要写3个none none应该是datanodeSpecificExtraConfig或者datanodeSpecificExtraPgHba配置PGXC add datanode master datanode3 xl3 15432 6671 /home/postgres/pgxc/nodes/datanode/datanode3 none none none 等待新增完成后,查询集群节点状态: postgres= select from pgxc_node;node_name | node_type | node_port | node_host | nodeis_primary | nodeis_preferred | node_id-----------+-----------+-----------+-----------+----------------+------------------+-------------datanode1 | D | 15432 | xl1 | t | f | 888802358datanode2 | D | 15432 | xl2 | f | f | -905831925datanode3 | D | 15432 | xl3 | f | f | -705831925coord1 | C | 5432 | xl1 | f | f | 1885696643coord2 | C | 5432 | xl2 | f | f | -1197102633(4 rows) 节点新增完毕 数据重新分布 由于新增节点后无法自动完成数据重新分布,需要手动操作。 DISTRIBUTE表分布在了node1,node2节点上,如下: postgres= SELECT xc_node_id, count() FROM disttab GROUP BY xc_node_id;xc_node_id | count ------------+-------1148549230 | 42-927910690 | 58(2 rows) 新增一个节点后,将sharding表数据重新分配到三个节点上,将repl表复制到新节点 重分布sharding表postgres= ALTER TABLE disttab ADD NODE (datanode3);ALTER TABLE 复制数据到新节点postgres= ALTER TABLE repltab ADD NODE (datanode3);ALTER TABLE 查看新的数据分布: postgres= SELECT xc_node_id, count() FROM disttab GROUP BY xc_node_id;xc_node_id | count ------------+--------700122826 | 36-927910690 | 321148549230 | 32(3 rows) 登录datanode3(新增的时候,放在了xl3服务器上,端口15432)节点查看数据: [postgres@gtm ~]$ psql -h xl3 -p 15432 -U postgrespsql (PGXL 10r1.1, based on PG 10.6 (Postgres-XL 10r1.1))Type "help" for help.postgres= select count() from repltab;count -------100(1 row) 很明显,通过 ALTER TABLE tt ADD NODE (dn)命令,可以将DISTRIBUTE表数据重新分布到新节点,重分布过程中会中断所有事务。可以将REPLICATION表数据复制到新节点。 从datanode节点中回收数据 postgres= ALTER TABLE disttab DELETE NODE (datanode3);ALTER TABLEpostgres= ALTER TABLE repltab DELETE NODE (datanode3);ALTER TABLE 删除数据节点 Postgresql-XL并没有检查将被删除的datanode节点是否有replicated/distributed表的数据,为了数据安全,在删除之前需要检查下被删除节点上的数据,有数据的话,要回收掉分配到其他节点,然后才能安全删除。删除数据节点分为四步骤: 1.查询要删除节点dn3的oid postgres= SELECT oid, FROM pgxc_node;oid | node_name | node_type | node_port | node_host | nodeis_primary | nodeis_preferred | node_id -------+-----------+-----------+-----------+-----------+----------------+------------------+-------------11819 | coord1 | C | 5432 | datanode1 | f | f | 188569664316384 | coord2 | C | 5432 | datanode2 | f | f | -119710263316385 | node1 | D | 5433 | datanode1 | f | t | 114854923016386 | node2 | D | 5433 | datanode2 | f | f | -92791069016397 | dn3 | D | 5430 | datanode1 | f | f | -700122826(5 rows) 2.查询dn3对应的oid中是否有数据 testdb= SELECT FROM pgxc_class WHERE nodeoids::integer[] @> ARRAY[16397];pcrelid | pclocatortype | pcattnum | pchashalgorithm | pchashbuckets | nodeoids ---------+---------------+----------+-----------------+---------------+-------------------16388 | H | 1 | 1 | 4096 | 16397 16385 1638616394 | R | 0 | 0 | 0 | 16397 16385 16386(2 rows) 3.有数据的先回收数据 postgres= ALTER TABLE disttab DELETE NODE (dn3);ALTER TABLEpostgres= ALTER TABLE repltab DELETE NODE (dn3);ALTER TABLEpostgres= SELECT FROM pgxc_class WHERE nodeoids::integer[] @> ARRAY[16397];pcrelid | pclocatortype | pcattnum | pchashalgorithm | pchashbuckets | nodeoids ---------+---------------+----------+-----------------+---------------+----------(0 rows) 4.安全删除dn3 PGXC$ remove datanode master dn3 clean 故障节点FAILOVER 1.查看当前集群状态 [postgres@gtm ~]$ psql -h xl1 -p 5432psql (PGXL 10r1.1, based on PG 10.6 (Postgres-XL 10r1.1))Type "help" for help.postgres= SELECT oid, FROM pgxc_node;oid | node_name | node_type | node_port | node_host | nodeis_primary | nodeis_preferred | node_id-------+-----------+-----------+-----------+-----------+----------------+------------------+-------------11739 | coord1 | C | 5432 | xl1 | f | f | 188569664316384 | coord2 | C | 5432 | xl2 | f | f | -119710263316387 | datanode2 | D | 15432 | xl2 | f | f | -90583192516388 | datanode1 | D | 15432 | xl1 | t | t | 888802358(4 rows) 2.模拟datanode1节点故障 直接关闭即可 PGXC stop -m immediate datanode master datanode1Stopping datanode master datanode1.Done. 3.测试查询 只要查询涉及到datanode1上的数据,那么该查询就会报错 postgres= SELECT xc_node_id, count() FROM disttab GROUP BY xc_node_id;WARNING: failed to receive file descriptors for connectionsERROR: Failed to get pooled connectionsHINT: This may happen because one or more nodes are currently unreachable, either because of node or network failure.Its also possible that the target node may have hit the connection limit or the pooler is configured with low connections.Please check if all nodes are running fine and also review max_connections and max_pool_size configuration parameterspostgres= SELECT xc_node_id, FROM disttab WHERE col1 = 3;xc_node_id | col1 | col2 | col3------------+------+------+-------905831925 | 3 | 103 | foo(1 row) 测试发现,查询范围如果涉及到故障的node1节点,会报错,而查询的数据范围不在node1上的话,仍然可以查询。 4.手动切换 要想切换,必须要提前配置slave节点。 PGXC$ failover datanode node1 切换完成后,查询集群 postgres= SELECT oid, FROM pgxc_node;oid | node_name | node_type | node_port | node_host | nodeis_primary | nodeis_preferred | node_id -------+-----------+-----------+-----------+-----------+----------------+------------------+-------------11819 | coord1 | C | 5432 | datanode1 | f | f | 188569664316384 | coord2 | C | 5432 | datanode2 | f | f | -119710263316386 | node2 | D | 15432 | datanode2 | f | f | -92791069016385 | node1 | D | 15433 | datanode2 | f | t | 1148549230(4 rows) 发现datanode1节点的ip和端口都已经替换为配置的slave了。 本篇文章为转载内容。原文链接:https://blog.csdn.net/qianglei6077/article/details/94379331。 该文由互联网用户投稿提供,文中观点代表作者本人意见,并不代表本站的立场。 作为信息平台,本站仅提供文章转载服务,并不拥有其所有权,也不对文章内容的真实性、准确性和合法性承担责任。 如发现本文存在侵权、违法、违规或事实不符的情况,请及时联系我们,我们将第一时间进行核实并删除相应内容。
2023-01-30 11:09:03
94
转载
Cassandra
...r_id uuid PRIMARY KEY, name text, email text, unlogged ) WITH bloom_filter_fp_chance = 0.01 AND caching = {'keys': 'ALL', 'rows_per_partition': 'NONE'} AND comment = 'Fast writes, no durability'; 在这个例子中,unlogged关键字被添加到表定义中,声明这是一个UNLOGGED TABLES。嘿,你知道吗?咱们加了个小技巧,那就是把caching开关调到"不缓存行"模式,这样写入数据的时候速度能嗖嗖的快呢! 四、潜在风险与注意事项 1. 数据完整性 由于没有日志记录,如果集群崩溃,UNLOGGED TABLES的数据可能会丢失,这可能导致数据一致性问题。 2. 备份与恢复 由于缺乏日志,备份和恢复可能依赖于其他手段,如定期全量备份。 3. 监控与维护 需要更频繁地监控,确保数据的实时性和可用性。 五、实际应用案例 假设你在构建一个实时新闻聚合应用,用户点击行为需要迅速记录以便进行实时分析。你知道吗,如果你要记录用户的日常操作,可以选择用"未日志化表",这样即使偶尔漏掉点旧信息,你那实时显示的精准度也不会打折! 然而,如果应用涉及到法律合规或金融交易,那么你可能需要使用普通表格类型,以确保数据的完整性和满足法规要求。 六、总结与权衡 在Cassandra中,UNLOGGED TABLES是一个工具箱中的瑞士军刀,适用于特定场景下的性能优化。关键看你怎么定夺,就是得琢磨清楚你的业务到底啥需求,数据又有多宝贝,还有你能不能容忍点儿小误差,就这么简单。每种选择都有其代价,因此明智地评估和选择合适的表类型至关重要。 记住,数据科学家和工程师的角色不仅仅是编写代码,更是要理解业务需求,然后根据这些需求做出最佳技术决策。在Cassandra的世界里,这就是UNLOGGED TABLES发挥作用的地方。
2024-06-12 10:55:34
492
青春印记
MySQL
...NOT NULL, PRIMARY KEY (id) ); MySQL还支持多种不同的数据存储引擎,包括InnoDB、MyISAM、Memory等。每种存储引擎有其各自的优缺点,使用者可以根据需要进行选择和配置。 SHOW ENGINES; 在工业实时数据管理中,MySQL的主要使用场景包括数据采集、生产监控、质量控制、故障诊断等。使用者可以通过对MySQL的数据表进行操作,快速地获取到所需的数据并进行实时分析和处理。 总结来说,MySQL是一种可靠、高效的工业实时数据库,可以为使用者提供完善的数据管理和分析功能。
2024-02-07 16:13:02
55
逻辑鬼才
JSON
... ( id INT PRIMARY KEY, name VARCHAR(50), email VARCHAR(50), address VARCHAR(100) ); -- 解读JSON数据 var data = JSON.parse('[ { "id": 1, "name": "Alice", "email": "alice@example.com", "address": { "street": "123 Main St", "city": "Anytown", "state": "USA", "zipcode": "12345" } }, { "id": 2, "name": "Bob", "email": "bob@example.com", "address": { "street": "456 High St", "city": "Anytown", "state": "USA", "zipcode": "67890" } } ]'); -- 将数据结构添加数据库表 for(var i = 0; i< data.length; i++) { var user = data[i]; var query = "INSERT INTO users (id, name, email, address) VALUES (?, ?, ?, ?)"; db.query(query, [user.id, user.name, user.email, JSON.stringify(user.address)]); } 在上述代码中,我们使用了JavaScript语言进行示例展示,但是相应的处理在其他编程语言,例如Python、Java、PHP等,也有相应的实现方法。总的来说,将JSON数据转化成表格形式,可以方便地对数据进行增删改查等处理,提高数据的处理速度和数据管控的便捷性。
2023-11-04 08:47:08
443
算法侠
MySQL
...INCREMENT PRIMARY KEY, user_id INT, expense_date DATE, expense_amount DECIMAL(10,2), expense_description VARCHAR(255), expense_status ENUM('pending','approved','rejected') ); 以上代码创建了一个名为expense_reports的表格,其中包含用户ID、批准日期、费用金额、费用描述和状态等信息。expense_status可以有三个可能的值:“pending”、“approved”和“rejected”。这个表格将保存所有报销申请的明细。 在微信小程序中,用户可以通过界面递交报销申请,并填写表格。这些数据将被采集并保存到MySQL数据库中。下面是一个示例: INSERT INTO expense_reports (user_id, expense_date, expense_amount, expense_description, expense_status) VALUES (1, '2021-06-01', 33.50, '午餐', 'pending'); 以上代码将在expense_reports表格中插入一条记录,其中包含ID为1的用户的报销申请。此申请包括在2021年6月1日递交、金额为33.50美元的午餐。其状态为“pending”(尚未审核)。 当维护员进入微信小程序时,他们将能够查看所有未处理的申请。他们可以查看数据、批准或驳回申请。此操作表现为“修改”表中的状态列。以下是一个示例: UPDATE expense_reports SET expense_status = 'approved' WHERE id = 1; 以上代码将ID为1的报销申请状态修改为“approved”(已核准)。这代表申请已经通过,可以支付报销金额。 总的来说,微信小程序费用报销审核是一个非常有用的工具,它可以简化报销流程、增加批准速度并提高工作效率。MySQL是实现这个功能的关键。通过建立数据库、创建表格和执行SQL命令,MySQL提供了一种可靠且强大的方式来保存和维护用户递交的申请。
2023-08-09 15:20:34
98
软件工程师
Oracle
...yeeID INT PRIMARY KEY, FirstName VARCHAR(50), LastName VARCHAR(50), HireDate DATE); 2. Force Logging Force Logging模式是在任何情况下都强制数据库记录日志。这种模式常用于数据安全性高或者需要快速恢复的环境。 以下是使用Force Logging模式创建新表的SQL语句: sql ALTER DATABASE OPEN LOGGING; CREATE TABLE Employees ( EmployeeID INT PRIMARY KEY, FirstName VARCHAR(50), LastName VARCHAR(50), HireDate DATE); 3. Nologging Nologging模式尽量减少日志的记录,主要用于提高数据库性能。但是,在这种模式下,一旦出现错误,就无法通过日志进行恢复。 以下是使用Nologging模式创建新表的SQL语句: sql ALTER DATABASE OPEN NOARCHIVELOG; CREATE TABLE Employees ( EmployeeID INT PRIMARY KEY, FirstName VARCHAR(50), LastName VARCHAR(50), HireDate DATE); 二、日志记录模式的使用情况 根据业务需求和性能考虑,选择合适的日志记录模式是非常重要的。以下是一些使用日志记录模式的情况: 1. 数据安全性要求高的环境 在这种环境下,推荐使用Force Logging模式,因为它强制数据库记录日志,并且可以在出现错误后快速恢复数据库。 2. 性能优先的环境 在这种环境下,推荐使用Nologging模式,因为它减少了日志的记录,提高了数据库的性能。但是需要注意的是,一旦出现错误,就无法通过日志进行恢复。 3. 普通的数据库环境 在这种环境下,推荐使用Logging模式,因为它既能够记录日志,又不会严重影响数据库的性能。 三、结论 了解Oracle数据库的日志记录模式可以帮助我们更好地管理和维护数据库。挑对日志记录的方式,咱们就能在确保数据库跑得溜又安全的前提下,最大程度地挠到业务需求的痒处。希望这篇文章能像一位贴心的朋友,帮您把Oracle数据库那神秘的日志记录模式掰开了、揉碎了,让您轻轻松松掌握住,明明白白理解透。
2023-10-22 22:38:41
276
人生如戏-t
MySQL
...INCREMENT 主键 KEY, first_name VARCHAR(30) 不能为空 NULL, last_name VARCHAR(30) 不能为空 NULL, email VARCHAR(50), age INT(3) ); 3. 将记录输入到表中。可以使用 插入到 INTO 语句将记录添加到表中。以下是一个例子: 插入到 INTO customers (first_name, last_name, email, age) VALUES ('John', 'Doe', 'john@example.com', '30'), ('Mary', 'Smith', 'mary@example.com', '25'), ('Jane', 'Doe', 'jane@example.com', '40'); 4. 查看表中的记录。可以使用 SELECT 语句查看表中的内容。以下是一个例子: SELECT FROM customers; 5. 删除表。如果不再需要这个表,可以使用 DROP TABLE 语句进行删除。以下是一个例子: DROP TABLE customers; 总之,在 MySQL 中建立一个可访问的表是一个很好的开始。通过这个表,我们可以保管和维护我们的记录,并在需要的时候访问它们。
2023-01-01 19:53:47
73
代码侠
PostgreSQL
...id serial primary key, name varchar(50), department varchar(50) ); 步骤二:选择要创建索引的列 接下来,我们需要选择要创建索引的列。例如,如果我们想要根据name列创建一个索引,我们可以这样做: sql CREATE INDEX idx_employees_name ON employees (name); 在这个例子中,idx_employees_name是我们给索引起的名字,ON employees (name)表示我们在employees表的name列上创建了一个新的索引。 步骤三:创建索引 最后,我们可以通过执行上述SQL语句来创建索引。要是没啥意外,PostgreSQL会亲口告诉我们一个好消息,那就是索引已经妥妥地创建成功啦! sql CREATE INDEX idx_employees_name ON employees (name); 如何查看已创建的索引? 如果你想知道哪些索引已经被创建在你的表上,你可以使用pg_indexes系统视图。这个视图可厉害了,它囊括了所有的索引信息,从索引的名字,到它所对应绑定的表,再到索引的各种类型,啥都一清二楚,明明白白。 sql SELECT FROM pg_indexes WHERE tablename = 'employees'; 这将会返回一个结果集,其中包含了employees表上的所有索引的信息。 创建可以显示值的索引 在PostgreSQL中,创建一个可以显示值的索引很简单。我们只需要在创建索引的时候指定我们想要使用的索引类型即可。目前,PostgreSQL支持多种索引类型,包括B-tree、哈希、GiST、SP-GiST和GIN等。不同的索引类型就像不同类型的工具,各有各的适用场合。所以,你得根据自己的实际需求,像挑选合适的工具一样,去选择最适合你的索引类型。别忘了,对症下药才能发挥最大效用! 以下是一个创建B-tree索引的例子: sql CREATE INDEX idx_employees_name_btree ON employees (name); 在这个例子中,idx_employees_name_btree是我们给索引起的名字,ON employees (name)表示我们在employees表的name列上创建了一个新的B-tree索引。如果你想创建不同类型的索引,那就简单啦,只需要把“btree”这个词儿换成你心水的索引类型就大功告成啦!就像是换衣服一样,根据你的需求选择不同的“款式”就行。 总结 创建一个可以显示值的索引并不难。其实,你只需要用一句“CREATE INDEX”命令,就能轻松搞定创建索引的事儿。具体来说,就是在这句命令里头,告诉系统你要在哪个表上建索引、打算对哪一列建立索引,还有你希望用哪种类型的索引,一切就OK啦!就像是在跟数据库说:“嗨,我在某某表的某某列上,想要创建一个这样那样的索引!”另外,你还可以使用pg_indexes系统视图来查看已创建的所有索引。希望这篇文章能对你有所帮助!
2023-11-30 10:13:56
261
半夏微凉_t
PostgreSQL
...id SERIAL PRIMARY KEY, name VARCHAR(255) NOT NULL, age INT NOT NULL, address VARCHAR(255) ); 现在,我们想要在“name”列上创建一个索引,以便我们可以更快地查找员工的名字。那么,我们就可以使用以下的SQL语句: sql CREATE INDEX idx_employees_name ON employees (name); 在这个语句中,“idx_employees_name”是我们给新创建的索引命名的字符串,“employees”是我们想要在其上创建索引的表名,“name”是我们想要在哪个列上创建索引的列名。 查看索引 如果我们已经创建了一个索引,但不确定它是否起作用或者我们想要查看所有已存在的索引,我们可以使用以下的SQL语句: sql SELECT FROM pg_indexes WHERE tablename = ''; 在这个语句中,“是我们想要查看其索引的表名。“pg_indexes”是PostgreSQL的一个系统表,它包含了所有的索引信息。 性能优化 虽然索引可以帮助我们加快查询速度,但是过多的索引也会影响数据库的性能。因此,在创建索引时,我们需要权衡索引的数量和查询效率之间的关系。通常来说,当你的表格里头的数据条数蹭蹭地超过10万大关的时候,那就真的得琢磨琢磨给它创建个索引了,这样一来才能让数据查找更溜更快。此外,咱们也得留意一下,别在那些频繁得不得了的列上乱建索引。要知道,这样做的话,索引维护起来可是会让人头疼的,成本噌噌往上涨。 总的来说,索引是提高数据库查询效率的重要手段。在PostgreSQL这个数据库里,我们能够用几句简单的SQL命令轻松创建索引。而且,更酷的是,还可以借助系统自带的索引管理工具,像看菜单一样直观地查看索引的各种状态,甚至还能随心所欲地调整它们,就像给你的数据仓库整理目录一样方便。但是,我们也需要注意不要滥用索引,以免影响数据库的整体性能。
2023-06-18 18:39:15
1325
海阔天空_t
MySQL
...HAR(255), PRIMARY KEY (id) ); 这段命令将会在example数据库中创建一个名为users的新表,包含id、name和email三个字段。 步骤6:查询数据库 在MySQL服务器上,你可以通过以下命令来查询新创建的数据库和表: sql SHOW DATABASES; SHOW TABLES FROM example; SELECT FROM example.users; 以上就是测试MySQL是否安装完整的几个基本步骤。经过这些步骤,你就能确保MySQL的服务器软件、客户端小工具、命令行神器还有数据文件都妥妥地安装好了,并且随时可以正常启动,愉快地使用起来啦!同时呢,你还可以亲自去瞅瞅MySQL的运行状况啊,还有它的性能表现啥的,这样一来,就能更棒地打理和调优你的MySQL数据库了,让它的表现更上一层楼! 总结起来,要想保证MySQL能够正常运行,就需要对其进行全面的测试。这包括瞅瞅MySQL服务的小火车跑得顺不顺畅,确保它能稳妥连接。咱们还要亲自上手,捣鼓捣鼓创建数据库和表的操作,再溜达一圈,试试查询功能灵不灵光,这些可都是必不可少的环节~只要按照上述步骤进行操作,就能够确保MySQL安装的完整性。
2023-06-26 18:05:53
32
风轻云淡_t
Superset
....Integer, primary_key=True) alert_type = db.Column(db.String(255), nullable=False) email_sent = db.Column(db.Boolean, nullable=False) email_address = db.Column(db.String(255), nullable=False) audit_model = EmailAudit.__table__ session = sessionmaker(bind=db.engine)() session.execute( audit_model.insert(), [ {"alert_type": "some alert", "email_sent": False, "email_address": "someone@example.com"}, ], ) session.commit() 在这个示例中,我们首先创建了一个名为 email_alert_recipients 的数据库表,该表包含了我们要发送邮件的通知类型和接收者的邮箱地址。 然后,我们创建了一个名为 EmailAudit 的模型,该模型将用于跟踪邮件是否已被发送。这个模型里头有个字段叫 email_sent,你可把它想象成个邮筒上的小旗子。当我们顺利把邮件“嗖”地一下送出去了,就立马把这个小旗子立起来,标记为True,表示这封邮件已经成功发送啦! 最后,我们调用 security_manager.add_email_alert 方法来创建一个新通知,并将其关联到 EmailAudit 模型。 以上就是在Superset中设置SMTP服务器以及使用Superset发送邮件通知的基本步骤。经过这些个步骤,你就能轻轻松松地在Superset上和大伙儿分享你的新发现和独到见解啦!
2023-10-01 21:22:27
61
蝶舞花间-t
转载文章
自增主键 (AUTO_INCREMENT) , 在MySQL等关系型数据库中,自增主键是一种特殊的字段类型设置,它会自动为每条新插入的记录生成一个唯一的、递增的整数值作为主键。在文章语境中,当表中的某个字段被定义为自增主键时,每次执行插入操作,系统会自动为该字段分配一个新的、大于已有最大值的整数,以此保证主键的唯一性。 唯一键 (unique key) , 在数据库设计中,唯一键约束是一种用于确保表中某列或某几列组合数据具有唯一性的机制。在文章提及的问题情境下,表中的“abc”字段被设为唯一键,意味着在同一张表内,不允许有两条记录的“abc”字段值相同。如果尝试插入已存在的“abc”值,数据库将拒绝此次插入操作以维持数据完整性。 触发器 (trigger) , 触发器是数据库管理系统中的一种数据库对象,它在特定数据库操作(如INSERT、UPDATE或DELETE)发生时自动执行一段预定义的SQL代码。在文中提到的场景中,作者试图创建一个触发器来解决自增主键不连续的问题,即在每次向表中插入新记录后,通过触发器重置AUTO_INCREMENT值。然而,在实际应用中,由于语法限制或其他因素,文中所述的触发器实现方式并未成功解决问题。
2023-08-26 08:19:54
92
转载
MyBatis
...INCREMENT PRIMARY KEY, name VARCHAR(255), description TEXT, FULLTEXT(description) ); 这里,我们为description字段添加了一个全文索引,这意味着我们可以在这个字段上执行全文搜索。 2.2 MyBatis映射文件配置 接下来,在MyBatis的映射文件(Mapper XML)中定义相应的SQL查询语句。这里的关键在于正确地构建全文搜索的SQL语句。比如,假设我们要实现根据商品描述搜索商品的功能,可以这样编写: xml SELECT FROM product WHERE MATCH(description) AGAINST ({keyword} IN NATURAL LANGUAGE MODE) 这里的MATCH(description) AGAINST ({keyword})就是全文搜索的核心部分。“IN NATURAL LANGUAGE MODE”就是用大白话来搜东西,这种方式更直接、更接地气。搜出来的结果也会按照跟你要找的东西的相关程度来排个序。 3. 实际应用中的常见问题及解决方案 在实际开发过程中,可能会遇到一些配置不当导致全文搜索功能失效的情况。这里,我将分享几个常见的问题及其解决方案。 3.1 搜索结果不符合预期 问题描述:当你执行全文搜索时,发现搜索结果并不是你期望的那样,可能是因为搜索关键词太短或者太常见,导致匹配度不高。 解决方法:尝试调整全文搜索的模式,比如使用BOOLEAN MODE来提高搜索精度。此外,确保搜索关键词足够长且具有一定的独特性,可以显著提高搜索效果。 xml SELECT FROM product WHERE MATCH(description) AGAINST ({keyword} IN BOOLEAN MODE) 3.2 性能瓶颈 问题描述:随着数据量的增加,全文搜索可能会变得非常慢,影响用户体验。 解决方法:优化索引设计,比如适当减少索引字段的数量,或者对索引进行分区。另外,也可以考虑在应用层缓存搜索结果,减少数据库负担。 4. 总结与展望 通过上述内容,我们了解了如何在MyBatis项目中正确配置全文搜索功能,并探讨了一些实际操作中可能遇到的问题及解决策略。全文搜索这东西挺强大的,但你得小心翼翼地设置才行。要是设置得好,不仅能让人用起来更爽,还能让整个应用变得更全能、更灵活。 当然,这只是全文搜索配置的一个起点。随着业务越做越大,技术也越来越先进,我们可以试试更多高大上的功能,比如支持多种语言,还能处理同义词啥的。希望本文能对你有所帮助,如果有任何疑问或想法,欢迎随时交流讨论! --- 希望这篇文章能够帮助到你,如果有任何具体的需求或者想了解更多细节,随时告诉我!
2024-11-06 15:45:32
135
岁月如歌
站内搜索
用于搜索本网站内部文章,支持栏目切换。
知识学习
实践的时候请根据实际情况谨慎操作。
随机学习一条linux命令:
journalctl
- 查看系统日志。
推荐内容
推荐本栏目内的其它文章,看看还有哪些文章让你感兴趣。
2023-04-28
2023-08-09
2023-06-18
2023-04-14
2023-02-18
2023-04-17
2024-01-11
2023-10-03
2023-09-09
2023-06-13
2023-08-07
2023-03-11
历史内容
快速导航到对应月份的历史文章列表。
随便看看
拉到页底了吧,随便看看还有哪些文章你可能感兴趣。
时光飞逝
"流光容易把人抛,红了樱桃,绿了芭蕉。"