Question

sudo mysql-create-db-and-user - SQL syntax ERROR 1064 (42000) at line 1

Can anyone help get this command working again? This cmd used to work in the putty console -

sudo mysql-create-db-and-user dbuserhere dbnamehere passwordhere

This error now appears -

ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IDENTIFIED BY 'passwordhere'' at line 1

I have MySQL version 8.0.28 on Ubuntu 20.04.4 LTS.

The database is created but I can’t connect to it using the username and password used in the cmd.

I can only guess it stopped working after I did sudo apt update and sudo apt upgrade.

This is the error in /var/log/mysql/error.log

2022-05-26T04:50:06.754109Z 21 [Warning] [MY-013360] [Server] Plugin sha256_password reported: ''sha256_password' is deprecated and will be removed in a future release. Please use caching_sha2_password instead'

Submit an answer
Answer a question...

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Accepted Answer

I managed to do what I want by using these commands -

CREATE DATABASE dbnamehere;

CREATE USER 'dbuserhere'@'localhost' IDENTIFIED WITH mysql_native_password BY 'passwordhere';
GRANT ALL on dbnamehere.* TO 'dbuserhere'@'localhost';
KFSys
Site Moderator
Site Moderator badge
June 21, 2022

Hi @twc8ac35a8636,

The new script should look like this:

#!/bin/bash

# Functions
ok() { echo -e '\e[32m'$1'\e[m'; } # Green

EXPECTED_ARGS=3
E_BADARGS=65
MYSQL=`which mysql`

# Create Database
Q1="CREATE DATABASE IF NOT EXISTS $1;"
# Create User
Q2="CREATE USER IF NOT EXISTS '$2'@'localhost' IDENTIFIED BY '$3';"
# Add permissions
Q3="GRANT ALL ON *.* TO '$2'@'localhost';"
Q4="FLUSH PRIVILEGES;"
SQL="${Q1}${Q2}${Q3}${Q4}"

if [ $# -ne $EXPECTED_ARGS ]
then
  echo "Usage: $0 dbname dbuser dbpass"
  exit $E_BADARGS
fi
 
$MYSQL -e "$SQL"

ok "Database $1 and user $2 created with a password $3"

Let me know how it goes.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Are you using the NPM module? https://www.npmjs.com/package/mysql-create-db-and-user

that’s a script you invoke, post your script WITHOUT credentials here.