AO Logo
 
  
  
  
Add Database
Add User
AO
AO Industries, Inc.
Application Infrastructure ProviderApplication Infrastructure Provider
Sign UpWhat's NewClient AreaContact UsSite Map
 
your location:   home page ··· aoserv platform ··· software components ··· aoserv client ··· how to ··· mysql howto ··· add mysql user
Adding a MySQL User

AOSH Commands
add_username package username
add_mysql_user username
add_mysql_server_user username server ""
add_mysql_db_user database_name server username true true true true true true true true
wait_for_mysql_user_rebuild server
set_mysql_server_user_password username server password

Simple API
/**
 * Adds a MySQLUser to the system.
 *
 * @param  aoClient     the SimpleAOClient to use
 * @param  packageName  the name of the Package
 * @param  username     the new username to allocate
 * @param  server       the hostname of the server to add the account to
 * @param  database     the new user will be granted access to this database
 * @param  password     the password for the new account
 */
public static void addMySQLUser(
    SimpleAOClient aoClient,
    String packageName,
    String username,
    String server,
    String database,
    String password
) throws IOException, SQLException {
    // Reserve the username
    aoClient.addUsername(packageName, username);

    // Indicate the username will be used for MySQL accounts
    aoClient.addMySQLUser(username);

    // Grant access to the server
    aoClient.addMySQLServerUser(username, server, MySQLHost.ANY_LOCAL_HOST);

    // Grant access to the database
    aoClient.addMySQLDBUser(database, server, username, true, true, true, true, true, true, true, true);

    // Commit the changes before setting the password
    aoClient.waitForMySQLUserRebuild(server);

    // Set the password
    aoClient.setMySQLServerUserPassword(username, server, password);
}

Full API
/**
 * Adds a MySQLUser to the system.
 *
 * @param  conn         the AOServConnector/code> to use
 * @param  packageName  the name of the Package
 * @param  username     the new username to allocate
 * @param  server       the hostname of the server to add the account to
 * @param  database     the new user will be granted access to this database
 * @param  password     the password for the new account
 *
 * @return  the new MySQLServerUser
 */
public static MySQLServerUser addMySQLUser(
    AOServConnector conn,
    String packageName,
    String username,
    String server,
    String database,
    String password
) throws IOException, SQLException {
    // Find the Package
    Package pk=conn.getPackageTable().getPackage(packageName);

    // Reserve the username
    pk.addUsername(username);
    Username un=conn.getUsernameTable().getUsername(username);

    // Indicate the username will be used for MySQL accounts
    un.addMySQLUser();
    MySQLUser mu=un.getMySQLUser();

    // Resolve the Server
    Server se=conn.getServerTable().getServer(server);

    // Grant access to the server
    int msuPKey=mu.addMySQLServerUser(se, MySQLHost.ANY_LOCAL_HOST);
    MySQLServerUser msu=conn.getMySQLServerUserTable().getMySQLServerUser(msuPKey);
    
    // Find the MySQLDatabase
    MySQLDatabase md=se.getMySQLDatabase(database);

    // Grant access to the database
    md.addMySQLServerUser(msu, true, true, true, true, true, true, true, true);

    // Commit the changes before setting the password
    se.waitForMySQLUserRebuild();

    // Set the password
    msu.setPassword(password);
    
    // Return the object
    return msu;
}
Copyright © 2000-2024 AO Industries, Inc.