Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9289953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T20:18:10+00:00 2026-06-18T20:18:10+00:00

I have some troubles with GRANT and variables together in MySql. SET @username :=

  • 0

I have some troubles with GRANT and variables together in MySql.

SET @username := 'user123', @pass := 'pass123';

GRANT USAGE ON *.* TO @username@'%' IDENTIFIED BY @pass;
GRANT INSERT (header1, header2, headern) ON `data` TO @username@'%';
GRANT SELECT (header1, header2) ON `data2` TO @username@'%';

I’d like to put username and password into variables at the begining of the script and then later use them in GRANT

So instead of this:

GRANT USAGE ON *.* TO 'user123'@'%' IDENTIFIED BY 'pass123';

I’d like to use something like this:

GRANT USAGE ON *.* TO @username@'%' IDENTIFIED BY pass;

I’d really appreciate, if someone could show me the proper statements.
Thank you in advence!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T20:18:11+00:00Added an answer on June 18, 2026 at 8:18 pm
    SET @object = '*.*';
    SET @user = '''user1''@''localhost''';
    
    SET @query = CONCAT('GRANT UPDATE ON ', @object, ' TO ', @user);
    PREPARE stmt FROM @query;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
    

    DROP PROCEDURE IF EXISTS `test`.`spTest`$$
    
    CREATE DEFINER=`root`@`localhost` PROCEDURE `spTest`( varLogin char(16), varPassword char(64) )
    BEGIN
        DECLARE varPasswordHashed CHAR(41);
        SELECT PASSWORD(varPassword) INTO varPasswordHashed;
    
        # Any of the following 3 lines will cause the creation to fail
        CREATE USER varLogin@'localhost' IDENTIFIED BY varPassword;
        GRANT USAGE ON test.* TO varLogin@'localhost' IDENTIFIED BY varPassword;
        GRANT USAGE ON test.* TO varLogin@'localhost' IDENTIFIED BY PASSWORD varPasswordHashed;
    
        ## The following 3 lines won't cause any problem at create time
        CREATE USER varLogin@'localhost' IDENTIFIED BY 'AnyPassordString';
        GRANT USAGE ON test.* TO varLogin@'localhost' IDENTIFIED BY 'AnyPassordString';
        GRANT USAGE ON test.* TO varLogin@'localhost' IDENTIFIED BY PASSWORD  'AnyPassordString';  
    END$$
    
    DELIMITER;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some troubles with the below script. proc checkUser {userip} { set search
I have some troubles with jQuery. I have a set of Divs with .square
I have some troubles with my school project. I have a class: #include Group.h
I have some troubles with plotting the figure. I have a file with some
I have some troubles mapping a JSON Array to RestKit. This is what the
I have some troubles with getting the data from the website. The website source
I have some troubles with a method having a typed List parameter, inherited from
I have some troubles with positioning my label/password field. With this code they both
I have some troubles while writing logs from log4net to the file. I seem
I have some troubles with receiving data on server side. Here goes the code:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.