![]() |
|
Customer Support
How do I connect to my MySQL database?
Before you can use MySQL at all, you need to create a database for yourself on your Control Panel. Once you have created a database, there are a few ways you can connect to it:- At a shell prompt you type all one 1 single line:
- mysql -u DBUSERNAME -h DBSERVER -p DBNAME
with the following replacements of the bolded terms above:
- Replace DBSERVER with the correct database servername for your site.
- Replace DBUSERNAME with your own mysql username.
- Replace DBNAME with your own mysql databasename.
Example:
- mysql -u joe -h db.joesmith.com -p joeydb
If you don't know your MySQL Username or MySQL Databasename, go here.
You will then be prompted for your MySQL password. If you don't know what that is, go here to set it to something that you will remember.
If you use preferences set in a .my.cnf file, the connection command would be much shorter, just:
- mysql
Once at the MySQL prompt, you can then issue SQL commands directly to the MySQL server. For correct SQL syntax, see the MySQL Manual.
- To connect from a PHP script, just put this in your file:
<?
using the same substitutions for the bold terms as above, plus substituting DBPASSWORD with your own mysql password.
mysql_connect("db.YOURDOMAIN", "DBUSERNAME", "DBPASSWORD");
mysql_select_db("DBNAME");
?>
Example:
- mysql_connect("db.joesmith.com", "joe", "secret");
- mysql_select_db("joeydb");
- To connect from a perl script, put this in your file:
#!/usr/bin/perl
use DBI;
$database = "DBNAME";
$hostname = "db.YOURDOMAIN";
$port = "3306";
$username = "DBUSERNAME";
$password = 'DBPASSWORD';
$dsn = "DBI:mysql:database=$database;host=$hostname;port=$port";
$dbh = DBI->connect($dsn, $username, $password) or die("Could not connect!");
$sql = "SELECT * FROM mytable";
$sth = $dbh->prepare($sql);
$sth->execute;
while(($column1, $column2) = $sth->fetchrow_array)
{
print "C1 = $column1, C2 = $column2n";
}
$dbh->disconnect;
where DBNAME, DBUSERNAME, and DBPASSWORD are your database name, database username and database password, and where YOURDOMAIN is your website's domain name with no "www." in front, for example, "mysite.com", or if you are not hosting your own domain name with us, then "mysite.modwest.com".
- For connections from your own computer or from outside our network, see this FAQ
- mysql_connect("db.joesmith.com", "joe", "secret");
Related Questions:
How do I import delimited data into MySQL?
How do I change my MySQL password?
How do I import a MySQL dumpfile into my database?
How can I export data in CSV or tab delimited format?
Why do i get a query syntax error 1064 from MySQL when the syntax seems correct?
How do I export and move my database tables between servers or copy databases?
I can't connect to local MySQL server through socket 'mysql.sock'
How do I create a .my.cnf MySQL preference file?
What is my database server name?
How do I change MySQL timezone?
What is my MySQL Username or Database Name?
MySQL says I have too many connections.
How do I check how much disk space my database is using?
A MySQL MYI table has errors in it.
Can I connect to MySQL remotely?
Can I connect to my MySQL database from my own computer?
I can't get phpMyAdmin to work.
phpMyAdmin gives SQL syntax error when I try to create table.
How can I use odbc_connect() in my PHP scripts to connect to MySQL?
What kind of database server do you offer?
How many databases do I get?
What am I allowed to do with my MySQL database?
Will we be able to do our own MySQL admin?
How do I import a MySQL dumpfile into my database via phpMyAdmin?
Do you offer MySQL 5?
How do I export a MySQL dumpfile via phpMyAdmin?
Browse Categories:Getting Started, FTP, Telnet/SSH, Moving Domains, E-mail, Traffic Reports, Mailing Lists, Apache, PHP, CGI, Other Server-Side Scripting, MySQL Database, Imaging Libraries, Other Software, Billing & Terms, Control Panel, E-commerce, Pre-Sales |
