Getting Started With MySQL

This document is designed to get you started when you have a database set up for you on the publicdb machine.

There are no logins on the machine itself, everything is done via the client programs (mysql and mysqladmin), or a local GUI, such as the local instance of phpMyAdmin we offer.

Changing Your Password

When your database is generated, it is given a randomly-generated password, which is mailed to you in plaintext. You'll probably want to reset it (note: in the following examples, soak is used as the client, but any machine on the department network will work, as long as it has the mysql client utilities).

 


    soak:$ which mysql
    /usr/bin/mysql
    soak:$ mysql -h publicdb -u <username> -p <database>
    Enter password:
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 14666465
    Server version: 5.5.5-10.1.24-MariaDB MariaDB Server

    Copyright (c) 2000, 2023, Oracle and/or its affiliates.

    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.

    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

    mysql> set password = PASSWORD('<newpasswd>');
    Query OK, 0 rows affected (0.00 sec)

    mysql> \q
    Bye
    soak:$ 

 

where:
<username> is your user name,
<database> is your database name, and
<newpasswd> is your new password.

Note you can use phpMyAdmin for this as well.

After you do this, all further mysql or mysqladmin commands will require:

 

"-h publicdb -u <username> -p"

 

Note that anything using the "-p" by itself on the command line will prompt you for your password after you hit "Enter".

Connecting To Your Database

To connect to your database, you would type:

 

soak:$ mysql -h publicdb -u <username> -p <database>

where:
<username> is your username, and
<database> is the database name (they are probably the same).

After you enter your password at the prompt, you'll be in the mysql command-line utility.

Backups

While this server is backed up nightly, you can periodically back up your own database by using the "mysqldump" command:

 

soak:$ mysqldump -h publicdb -u <username> -p <database> > <filename>

 

Getting Started with PHP

Here is some sample code to access the database via PHP:

 

   <?
      $machine = "publicdb.cs.princeton.edu";
      $user = "<username>";
      $passwd = "<password>";
      $db = "<database>";

      $connect = mysql_connect($machine,$user,$passwd);
      if(!$connect) {
         echo "Error: couldn't connect to db!<BR>\n";
         $error = mysql_error($connect);
         echo "<LI> $error\n";
         exit;
      }
      mysql_select_db($db,$connect);

      \\ query code here...
   ?>

 

You can get more information here [PHP Reference Manual].

Access Via C/C++

MySQL client libraries are available in your environment.

Access Via Java

Your database can also be accessed via your java programs via the mysql connector library, which is installed on our public cycle servers in /usr/share/java. The jar file is mysql-connector-java.jar, and further documentation can be found in the /usr/share/doc/mysql-connector-java-[VERSION] directory. Note that you need to set your CLASSPATH appropriately.