我似乎再也无法在Grafana中保存新的数据源了。
特别是,我正在尝试添加新的InfluxDB数据库作为数据源。当点击Add按钮时,会在UI中弹出一个错误Problem! Failed to add datasource
。
Grafana日志显示以下内容:
t=2018-07-17T09:59:32+0000 lvl=eror msg="Failed to add datasource" logger=context userId=0 orgId=1 uname= error="pq: null value in column "id" violates not-null constraint"
检查数据库日志(PostgreSQL(有一个相关错误:
2018-07-19 07:12:46 UTC:10.204.145.134(36768):admin@grafana:[477]:DETAIL: Failing row contains (null, 1, 0, influxdb, jenkins, proxy, http://localhost:8086, root, root, jenkins, f, , , f, {}, 2018-07-19 07:12:46, 2018-07-19 07:12:46, f, {}).
2018-07-19 07:12:46 UTC:10.204.145.134(36768):admin@grafana:[477]:STATEMENT: INSERT INTO "data_source"
正如您所看到的,UI似乎试图插入null
作为产生错误的索引。
尽管我们最近迁移了数据库(从一个PG迁移到另一个PG,相同的版本(,但配置没有改变,似乎也没有任何其他不良影响。
编辑:似乎这实际上影响了Grafana在添加新资源时试图执行的任何数据库操作。我刚刚让一个开发人员尝试导入一个新的面板,PostgreSQL日志显示了一个类似的错误:
2018-07-19 08:05:07 UTC:10.204.25.220(34412):sharedadmin@grafana:[14263]:DETAIL: Failing row contains (null, 1, pcs-again, PCS-AGAIN, {"__requires":[{"id":"grafana","name":"Grafana","type":"grafana"..., 1, 2018-07-19 08:05:07, 2018-07-19 08:05:07, -1, -1, 0, ).
经过深入研究,我们终于找到了答案。这些问题存在于我们用来从一个RDS实例迁移到另一个实例的AWS数据库迁移服务(DMS(中。DMS似乎没有很好地处理PostgreSQL到PostgreSQL,一些注意事项可以在这里的文档中找到。
在Grafana的情况下,流复制没有拾取列修饰符。迁移的表之一:
grafana-> d data_source
Table "public.data_source"
Column | Type | Modifiers
---------------------+--------------------------------+-----------
id | integer | not null
org_id | bigint | not null
version | integer | not null
type | character varying(255) | not null
name | character varying(190) | not null
access | character varying(255) | not null
url | character varying(255) | not null
password | character varying(255) |
user | character varying(255) |
database | character varying(255) |
basic_auth | boolean | not null
basic_auth_user | character varying(255) |
basic_auth_password | character varying(255) |
is_default | boolean | not null
json_data | text |
created | timestamp(6) without time zone | not null
updated | timestamp(6) without time zone | not null
with_credentials | boolean | not null
secure_json_data | text |
Indexes:
"data_source_pkey" PRIMARY KEY, btree (id)
以及来自未迁移实例的对应表:
grafana=> d data_source
Table "public.data_source"
Column | Type | Modifiers
---------------------+-----------------------------+-----------------------------------------------------------
id | integer | not null default nextval('data_source_id_seq1'::regclass)
org_id | bigint | not null
version | integer | not null
type | character varying(255) | not null
name | character varying(190) | not null
access | character varying(255) | not null
url | character varying(255) | not null
password | character varying(255) |
user | character varying(255) |
database | character varying(255) |
basic_auth | boolean | not null
basic_auth_user | character varying(255) |
basic_auth_password | character varying(255) |
is_default | boolean | not null
json_data | text |
created | timestamp without time zone | not null
updated | timestamp without time zone | not null
with_credentials | boolean | not null default false
secure_json_data | text |
Indexes:
"data_source_pkey1" PRIMARY KEY, btree (id)
"UQE_data_source_org_id_name" UNIQUE, btree (org_id, name)
"IDX_data_source_org_id" btree (org_id)
这个故事的寓意是DMS并不适用于所有数据库,请彻底阅读文档,在某些情况下使用本地PostgreSQL工具更好。
为了解决这个特殊的问题,我们删除了数据库(在确保导出了所有仪表板之后(,重新创建了它,然后重新启动了Grafana。
data_source
和user_auth
。
grafana=# d data_source
Table "public.data_source"
Column | Type | Collation | Nullable | Default
---------------------+-----------------------------+-----------+----------+-----------------------------------------
id | integer | | not null | nextval('data_source_id_seq'::regclass) <=== (Probably its ok Constraint)
grafana=# d user_auth
Table "public.user_auth"
Column | Type | Collation | Nullable | Default
----------------------+-----------------------------+-----------+----------+---------------------------------------
id | bigint | | not null | <=== (Probably it is blank)
在grafana
PSQL数据库中以grafana
用户身份登录并运行:
Alter table user_auth:
alter table user_auth
alter column id TYPE integer,
alter column id SET not null;
CREATE SEQUENCE user_auth_id_seq
START WITH 1
MINVALUE 1
NO MAXVALUE
CACHE 1;
CMD:
grafana=# CREATE SEQUENCE user_auth_id_seq
grafana-# START WITH 1
grafana-# MINVALUE 1
grafana-# NO MAXVALUE
grafana-# CACHE 1;
CREATE SEQUENCE
grafana=# ALTER TABLE user_auth ALTER COLUMN id SET DEFAULT nextval('user_auth_id_seq');
ALTER TABLE
grafana=# d user_auth
Table "public.user_auth"
Column | Type | Collation | Nullable | Default
----------------------+-----------------------------+-----------+----------+---------------------------------------
id | integer | | not null | nextval('user_auth_id_seq'::regclass) <=== (check it is ok now)
下一次测试:
grafana=# SELECT nextval('user_auth_id_seq');
nextval
---------
1
(1 row)
grafana=# SELECT nextval('user_auth_id_seq');
nextval
---------
2
(1 row)
grafana=# SELECT nextval('user_auth_id_seq');
nextval
---------
3
(1 row)
尝试使用ldap进行连接。
问候