When working with MySQL, you might encounter a common issue when trying to grant privileges for database names containing special characters like hyphens. The standard GRANT statement fails with syntax errors:
-- This works fine
GRANT ALL PRIVILEGES ON normal_db.* TO 'user'@'localhost';
-- This fails with syntax error
GRANT ALL PRIVILEGES ON db-with-hyphen.* TO 'user'@'localhost';
MySQL interprets the hyphen as a minus operator unless properly escaped. The solution is to use backticks () around identifiers containing special characters:
-- Correct syntax with backticks
GRANT SELECT, INSERT, UPDATE, DELETE ON astpp-eth01.* TO 'portal'@'localhost'
IDENTIFIED BY 'Ab7g12Xh35' WITH GRANT OPTION;
If you prefer not to use backticks or need compatibility with various MySQL clients:
-- Using double quotes (works in most configurations)
GRANT ALL PRIVILEGES ON "astpp-eth01".* TO 'portal'@'localhost';
-- Using the official ANSI SQL standard quoting
GRANT ALL PRIVILEGES ON [astpp-eth01].* TO 'portal'@'localhost'; -- SQL Server style
While backticks solve the immediate problem, consider these additional recommendations:
- Avoid special characters in database names when possible
- Use underscores instead of hyphens for better compatibility
- Be consistent with your naming conventions across all environments
Here's a complete example showing user creation and privilege assignment:
-- Create user
CREATE USER 'api_user'@'%' IDENTIFIED BY 'secure_password123';
-- Grant privileges to hyphenated database
GRANT SELECT, INSERT ON production-db.* TO 'api_user'@'%';
-- Grant privileges to multiple special-named databases
GRANT ALL PRIVILEGES ON test-db-2023, temp-db.* TO 'api_user'@'%';
-- Flush privileges to apply changes
FLUSH PRIVILEGES;
Watch out for these frequent mistakes:
-- WRONG: Using single quotes (will be interpreted as string literal)
GRANT ALL ON 'db-name'.* TO 'user'@'localhost';
-- WRONG: Mixing quote styles
GRANT ALL ON db-name".* TO 'user'@'localhost';
-- WRONG: Forgetting backticks when using hyphens
GRANT ALL ON db-name.* TO 'user'@'localhost';
Different database systems handle special characters differently:
- MySQL/MariaDB: Requires backticks
- PostgreSQL: Uses double quotes
- SQL Server: Uses square brackets
Always check your specific database system's documentation when working with special characters in identifiers.
When working with MySQL, you might encounter a common syntax error when trying to grant privileges for database names containing hyphens (dashes). The standard GRANT statement fails because MySQL interprets the hyphen as a minus operator in this context.
-- This works:
GRANT ALL PRIVILEGES ON normal_db.* TO 'user'@'localhost';
-- This fails:
GRANT ALL PRIVILEGES ON db-with-dash.* TO 'user'@'localhost';
The solution is to use backticks () to properly escape database names containing special characters. Backticks are MySQL's identifier quote character and allow you to use reserved words or special characters in database, table, and column names.
-- Correct syntax:
GRANT SELECT, INSERT, UPDATE, DELETE ON astpp-eth01.* TO 'portal'@'localhost'
IDENTIFIED BY 'Ab7g12Xh35' WITH GRANT OPTION;
If you're managing databases with special characters frequently, consider these additional methods:
-- Using backticks for both database and table names:
GRANT SELECT ON odd-db.strange-table TO 'user'@'%';
-- For multiple privileges:
GRANT SELECT, INSERT, CREATE TEMPORARY TABLES
ON weird-name-db.* TO 'app_user'@'192.168.%';
While escaping works, it's generally better to avoid special characters in database names. Consider these naming conventions:
- Use underscores instead of hyphens (astpp_eth01)
- Stick to alphanumeric characters and underscores
- Keep names lowercase for cross-platform compatibility
If you're still encountering problems, check these potential issues:
-- Verify database name exists:
SHOW DATABASES LIKE 'astpp-eth01';
-- Check existing grants:
SHOW GRANTS FOR 'portal'@'localhost';
-- Flush privileges after changes:
FLUSH PRIVILEGES;