How do I find my localhost MySQL URL?

For the MySQL database to work as intended, you will need a script calling or querying the database for the information it stores. You must connect your script to the database with a configuration file to accomplish this.

Please check out this article to know where to locate your configuration file:

  • Location of Script Configuration Files

Are you looking for instructions on how to connect to MySQL remotely? If so, please visit the article, How to Connect to MySQL Remotely.


What are my database configuration settings?

Platforms like WordPress, WHMCS, and Joomla usually have a default configuration file ready for you to edit with the appropriate information.

Hostname = localhost
Database name = cpanelUsername_databaseName
Database username = cpanelUsername_databaseUsername
Database password = the one you used when you created your username

In the sample below, we will use the database settings of WordPress (file format of wp-config.php).

cPanel username = example
Hostname = localhost
Database name = testdb
Database username = snappyuser
Database password = 0PXwMLtq%Xbt

// ** MySQL Settings ** //
/** The name of the database for WordPress */
define('DB_NAME', 'example_testdb');

/** MySQL database username */
define('DB_USER', 'example_snappyuser');

/** MySQL database password */
define('DB_PASSWORD', '0PXwMLtq%Xbt');

/** MySQL hostname */
define('DB_HOST', 'localhost');

Note: It is possible to connect to the database using your cPanel username and password. However, we NEVER recommend this. Every time you change or reset your cPanel password, your databases will stop working until you update your configuration files again.

  • The database name and username may change when moving databases to our servers. This change must be updated in your scripts.
  • On Shared hosting, the database username cannot be changed without the cPanel username in it. This is to prevent database names and usernames from conflicting with others on the same server.
  • To get the correct privileges added, please reassign your database user's privileges in cPanel. Please check out the article,  for the instructions.

MySQL user has no privileges

We have noticed an occasional bug wherein adding an IP address to the MySQL remote access list results in the corresponding MySQL user having no privileges since the 11.25 update.

We have already notified cPanel about the issue in hopes of this bug being corrected in newer updates.

First, you need to establish a connection with the data source you want to use. A data source can be a DBMS, a legacy file system, or some other source of data with a corresponding JDBC driver. Typically, a JDBC application connects to a target data source using one of two classes:

  • DriverManager: This fully implemented class connects an application to a data source, which is specified by a database URL. When this class first attempts to establish a connection, it automatically loads any JDBC 4.0 drivers found within the class path. Note that your application must manually load any JDBC drivers prior to version 4.0.

  • DataSource: This interface is preferred over DriverManager because it allows details about the underlying data source to be transparent to your application. A DataSource object's properties are set so that it represents a particular data source. See Connecting with DataSource Objects for more information. For more information about developing applications with the DataSource class, see the latest The Java EE Tutorial.

Note: The samples in this tutorial use the DriverManager class instead of the DataSource class because it is easier to use and the samples do not require the features of the DataSource class.

This page covers the following topics:

Connecting to your DBMS with the DriverManager class involves calling the method

jdbc:derby:[subsubprotocol:][databaseName][;attribute=value]*
2. The following method,
jdbc:derby:[subsubprotocol:][databaseName][;attribute=value]*
3, establishes a database connection:

public Connection getConnection() throws SQLException {

    Connection conn = null;
    Properties connectionProps = new Properties();
    connectionProps.put("user", this.userName);
    connectionProps.put("password", this.password);

    if (this.dbms.equals("mysql")) {
        conn = DriverManager.getConnection(
                   "jdbc:" + this.dbms + "://" +
                   this.serverName +
                   ":" + this.portNumber + "/",
                   connectionProps);
    } else if (this.dbms.equals("derby")) {
        conn = DriverManager.getConnection(
                   "jdbc:" + this.dbms + ":" +
                   this.dbName +
                   ";create=true",
                   connectionProps);
    }
    System.out.println("Connected to database");
    return conn;
}

The method

jdbc:derby:[subsubprotocol:][databaseName][;attribute=value]*
2 establishes a database connection. This method requires a database URL, which varies depending on your DBMS. The following are some examples of database URLs:

  1. MySQL:

    jdbc:derby:[subsubprotocol:][databaseName][;attribute=value]*
    
    5, where
    jdbc:derby:[subsubprotocol:][databaseName][;attribute=value]*
    
    6 is the name of the server hosting your database, and
    jdbc:derby:[subsubprotocol:][databaseName][;attribute=value]*
    
    7 is the port number

  2. Java DB:

    jdbc:derby:[subsubprotocol:][databaseName][;attribute=value]*
    
    8, where
    jdbc:derby:[subsubprotocol:][databaseName][;attribute=value]*
    
    9 is the name of the database to connect to, and
    jdbc:mysql://[host][,failoverhost...]
        [:port]/[database]
        [?propertyName1][=propertyValue1]
        [&propertyName2][=propertyValue2]...
    
    0 instructs the DBMS to create the database.

    Note: This URL establishes a database connection with the Java DB Embedded Driver. Java DB also includes a Network Client Driver, which uses a different URL.

This method specifies the user name and password required to access the DBMS with a

jdbc:mysql://[host][,failoverhost...]
    [:port]/[database]
    [?propertyName1][=propertyValue1]
    [&propertyName2][=propertyValue2]...
1 object.

Note:

  • Typically, in the database URL, you also specify the name of an existing database to which you want to connect. For example, the URL

    jdbc:mysql://[host][,failoverhost...]
        [:port]/[database]
        [?propertyName1][=propertyValue1]
        [&propertyName2][=propertyValue2]...
    
    2 represents the database URL for the MySQL database named
    jdbc:mysql://[host][,failoverhost...]
        [:port]/[database]
        [?propertyName1][=propertyValue1]
        [&propertyName2][=propertyValue2]...
    
    3. The samples in this tutorial use a URL that does not specify a specific database because the samples create a new database.

  • In previous versions of JDBC, to obtain a connection, you first had to initialize your JDBC driver by calling the method

    jdbc:mysql://[host][,failoverhost...]
        [:port]/[database]
        [?propertyName1][=propertyValue1]
        [&propertyName2][=propertyValue2]...
    
    4. This methods required an object of type
    jdbc:mysql://[host][,failoverhost...]
        [:port]/[database]
        [?propertyName1][=propertyValue1]
        [&propertyName2][=propertyValue2]...
    
    5. Each JDBC driver contains one or more classes that implements the interface
    jdbc:mysql://[host][,failoverhost...]
        [:port]/[database]
        [?propertyName1][=propertyValue1]
        [&propertyName2][=propertyValue2]...
    
    5. The drivers for Java DB are
    jdbc:mysql://[host][,failoverhost...]
        [:port]/[database]
        [?propertyName1][=propertyValue1]
        [&propertyName2][=propertyValue2]...
    
    7 and
    jdbc:mysql://[host][,failoverhost...]
        [:port]/[database]
        [?propertyName1][=propertyValue1]
        [&propertyName2][=propertyValue2]...
    
    8, and the one for MySQL Connector/J is
    jdbc:mysql://[host][,failoverhost...]
        [:port]/[database]
        [?propertyName1][=propertyValue1]
        [&propertyName2][=propertyValue2]...
    
    9. See the documentation of your DBMS driver to obtain the name of the class that implements the interface
    jdbc:mysql://[host][,failoverhost...]
        [:port]/[database]
        [?propertyName1][=propertyValue1]
        [&propertyName2][=propertyValue2]...
    
    5.

    Any JDBC 4.0 drivers that are found in your class path are automatically loaded. (However, you must manually load any drivers prior to JDBC 4.0 with the method

    jdbc:mysql://[host][,failoverhost...]
        [:port]/[database]
        [?propertyName1][=propertyValue1]
        [&propertyName2][=propertyValue2]...
    
    4.)

The method returns a DriverManager2 object, which represents a connection with the DBMS or a specific database. Query the database through this object.

A database connection URL is a string that your DBMS JDBC driver uses to connect to a database. It can contain information such as where to search for the database, the name of the database to connect to, and configuration properties. The exact syntax of a database connection URL is specified by your DBMS.

Java DB Database Connection URLs

The following is the database connection URL syntax for Java DB:

jdbc:derby:[subsubprotocol:][databaseName][;attribute=value]*

  • DriverManager3 specifies where Java DB should search for the database, either in a directory, in memory, in a class path, or in a JAR file. It is typically omitted.
  • DriverManager4 is the name of the database to connect to.
  • DriverManager5 represents an optional, semicolon-separated list of attributes. These attributes enable you to instruct Java DB to perform various tasks, including the following:
    • Create the database specified in the connection URL.
    • Encrypt the database specified in the connection URL.
    • Specify directories to store logging and trace information.
    • Specify a user name and password to connect to the database.

See Java DB Developer's Guide and Java DB Reference Manual from Java DB Technical Documentation for more information.