尝试在系统配置期间创建数据库。我将在下面附加configuration.nix文件的内容。运行sudo nixos-rebuild switch
命令后,将启动mariadb服务,创建数据库和具有给定口令的用户。但是,数据库为空,尽管我希望将main.sql文件的内容写入数据库。
configuration.nix
let
statsConfig = {
db = "test1";
user = "stats";
password = "1";
};
in
{
services.mysql = {
package = pkgs.mariadb;
enable = true;
ensureDatabases = [
statsConfig.db
];
replication.role = "master";
replication.slaveHost = "127.0.0.1";
replication.masterUser = "${statsConfig.user}";
replication.masterPassword = "${statsConfig.password}";
initialDatabases = [{ name = "${statsConfig.db}"; schema = ./main.sql; }];
initialScript = ./main.sql;
ensureUsers = [
{
name = "${statsConfig.user}";
ensurePermissions = {
"${statsConfig.db}.*" = "ALL PRIVILEGES";
};
}
];
};
systemd.services.setdbpass = {
description = "MySQL database password setup";
wants = [ "mariadb.service" ];
wantedBy = [ "multi-user.target" ];
serviceConfig = {
ExecStart = ''
${pkgs.mariadb}/bin/mysql -e "grant all privileges on ${statsConfig.db}.* to ${statsConfig.user}@localhost identified by '${statsConfig.password}';" ${statsConfig.db}
'';
User = "root";
PermissionsStartOnly = true;
RemainAfterExit = true;
};
};
}
main.sql
create table tests
( Id INTEGER NOT NULL,
Name VARCHAR(255) NOT NULL,
primary key(Id)
);
insert into tests values (1, 'a');
1条答案
按热度按时间g52tjvyc1#
您的main.sql没有被复制到/nix/store,因此它将无法找到它。
https://sourcegraph.com/github.com/vlktomas/nix-examples/-/blob/web/Laravel/laravel/cd.nix?L112&subtree=true
以及
https://sourcegraph.com/github.com/tumblr/jetpants/-/blob/testing/packages/mysql-container/service.nix?L114&subtree=true
有如何使用它的例子。