Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gabrielbidula/5739ed41b638840c4a422e3511008c1a to your computer and use it in GitHub Desktop.
Save gabrielbidula/5739ed41b638840c4a422e3511008c1a to your computer and use it in GitHub Desktop.
create a user / create a database / grant access privileges on mysql/mariadb
#! /bin/bash
newUser='testuser'
newDbPassword='testpwd'
newDb='testdb'
host=localhost
#host='%'
# MySQL 5.7 and earlier versions
#commands="CREATE DATABASE \`${newDb}\`;CREATE USER '${newUser}'@'${host}' IDENTIFIED BY '${newDbPassword}';GRANT USAGE ON *.* TO '${newUser}'@'${host}' IDENTIFIED BY '${newDbPassword}';GRANT ALL privileges ON \`${newDb}\`.*
TO '${newUser}'@'${host}' IDENTIFIED BY '${newDbPassword}';FLUSH PRIVILEGES;"
# MySQL 8 and higher versions
commands="CREATE DATABASE \`${newDb}\`;CREATE USER '${newUser}'@'${host}' IDENTIFIED BY '${newDbPassword}';GRANT USAGE ON *.* TO '${newUser}'@'${host}';GRANT ALL ON \`${newDb}\`.* TO '${newUser}'@'${host}';FLUSH PRIVILEGES;"
echo "${commands}" | /usr/bin/mysql -u root -p
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment