How to use docknode to enter MySQL password

27 views Asked by At

some code:

const stream = require("stream");
const streamPromises = require("stream/promises");
const Docker = require("dockerode");
const moment = require("moment");

const docker = new Docker();

async function exec(container, cmd, opts) {
  const dockerExec = await container.exec({
    ...opts,
    AttachStderr: true,
    AttachStdout: true,
    AttachStdin: true,
    Cmd: cmd,
    Tty: true,
  });

  const dockerExecStream = await dockerExec.start({
    hijack: true,
    stdin: true,
  });
  dockerExecStream.pipe(process.stdout);

  dockerExecStream.on("data", (data) => {
    dockerExecStream.write(Buffer.from("123456"));
    dockerExecStream.end();
  });
  dockerExecStream.on("error", (e, ...args) => {
    console.log("exec error:", e, ...args);
  });

  const stdoutStream = new stream.PassThrough();
  const stderrStream = new stream.PassThrough();

  docker.modem.demuxStream(dockerExecStream, stdoutStream, stderrStream);

  dockerExecStream.resume();

  await streamPromises.finished(dockerExecStream);

  const stderr = stderrStream.read();
  const stdout = stdoutStream.read();

  const dockerExecInfo = await dockerExec.inspect();

  return {
    exitCode: dockerExecInfo.ExitCode,
    stderr: stderr?.toString(),
    stdout: stdout?.toString(),
  };
}

const fileName = `backup${moment().format("YYYYMMDDHHmmss")}.sql`;
const inContainerPath = "/home";
const cmd = `mysqldump -u ${username} -p ${database} -c > ${inContainerPath}/${fileName}`;
const mysqlContainer = docker.getContainer("mysql.Id");
const res = await exec(mysqlContainer, [...cmd.split(" ")]);
console.log(res);

How to automatically enter a MySQL password and successfully run it?

Note:

I have tried this command before, and if it is run in code, it will be rejected by MySQL. It can be executed using the cmd tool mysqldump --user="root" --password="123456" satcd -c > /home/backup20231113164033.sql

errorCode:

mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: Got error: 1045: Access denied for user '"root"'@'localhost' (using password: YES) when trying to connect

I don't want to make modifications through my.cnf configuration. What should I do

0

There are 0 answers