NixOS创建数据库(mysql、mariadb)

vd2z7a6w  于 2023-03-02  发布在  Mysql
关注(0)|答案(1)|浏览(186)

尝试在系统配置期间创建数据库。我将在下面附加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');

相关问题