- 1 Character length restrictions
- 2 Using comments
- 3 Create a database
- 4 Create a user for that database with full privileges to it
- 5 Update user account to allow connecting from a different host
- 6 Reset password for user account
- 7 Viewing the account you're logged in as (equivalent of UNIX whoami)
- 8 Import or run SQL statements from file
- 9 Rename table
- 10 Insert a new row
- 11 Update field
- 12 Delete row
- 13 Delete user account
- 14 Delete database
- 15 Resetting the root user account
- 16 Viewing privileges granted to a user
- 17 Show the character set for a database
- 18 Show the database engine type
- 19 MySQL config/option file search order
- 20 References
Character length restrictions
- 64 characters for database name
- 16 characters for user name
- ? characters for password length
- Seems to be dependent on the version
These are all valid comment styles.
# Comment 1 -- Comment 2 /* Comment 3 */
Create a database
CREATE DATABASE my_test_db CHARACTER SET utf8;
Create a user for that database with full privileges to it
Depending on your needs, you'll setup the user in one of several ways.
Database server is on the same box as the application
Connecting via a UNIX socket
CREATE USER 'my_test_db_usr'@'localhost' IDENTIFIED BY 'INITIAL_PASSWORD'; GRANT ALL PRIVILEGES ON my_test_db.* TO 'my_test_db_usr'@'localhost';
Connecting via a TCP socket
Useful if the application is running inside of a chroot jail for instance
CREATE USER 'my_test_db_usr'@'127.0.0.1' IDENTIFIED BY 'INITIAL_PASSWORD'; GRANT ALL PRIVILEGES ON my_test_db.* TO 'my_test_db_usr'@'127.0.0.1';
Application is on a remote server
Remote host is specified via IP Address
CREATE USER 'my_test_db_usr'@'192.168.1.105' IDENTIFIED BY 'INITIAL_PASSWORD'; GRANT ALL PRIVILEGES ON my_test_db.* TO 'my_test_db_usr'@'192.168.1.105';
Remote host is specified by hostname
Valid DNS A record or /etc/hosts entry
CREATE USER 'my_test_db_usr'@'webserver1.example.org' IDENTIFIED BY 'INITIAL_PASSWORD'; GRANT ALL PRIVILEGES ON my_test_db.* TO 'my_test_db_usr'@'webserver1.example.org';
Update user account to allow connecting from a different host
If you created your MySQL user account and limited it to connections from a specific IP Address, at some point you'll need to create another account or update the host it is allowed to be used from (my choice) if the remote IP changes. One example would be moving the application server from one VLAN to another. In our example 192.168.2.25 is the new IP and the old IP was 192.168.1.105.
UPDATE user set Host = '192.168.2.25' WHERE User = 'my_test_db_usr' AND Host = '192.168.1.105'; FLUSH PRIVILEGES;
It's important to note however that you'll need to reapply database permissions for the affected user account however. What we've done so far has only adjusted the login or USAGE permission.
Reset password for user account
SET PASSWORD for 'my_test_db_usr'@'localhost' = PASSWORD('REAL_PASSWORD_HERE');
Viewing the account you're logged in as (equivalent of UNIX whoami)
USER()reports how you attempted to authenticate in MySQL
CURRENT_USER()reports how you were allowed to authenticate in MySQL
RolandoMySQLDBA notes: Sometimes, they are different
Import or run SQL statements from file
RENAME TABLE `my_test_db`.`table_name` TO `my_test_db`.`my_new_table_name`;
Insert a new row
INSERT INTO table_name ( `field_name1`, `field_name2`, `field_name3` ) VALUES ( 'Value1', 'Value2', 'Value3' );
UPDATE table_name SET table_field = 'Value' WHERE id = '1';
DELETE FROM virtual_domains WHERE id = '1' OR id = '7';
Delete user account
DROP USER 'my_test_db_usr'@'localhost';
DROP DATABASE 'my_test_db';
Resetting the root user account
1 nano /home/me/FILE 2 # Add SQL statement from section below 3 # Save & quit nano 4 sudo /etc/init.d/mysql stop 5 mysqld_safe --init-file=/home/me/FILE & 6 rm /home/me/FILE 7 sudo /etc/init.d/mysql restart
1 sudo /etc/init.d/mysql stop 2 sudo mysqld --skip-grant-tables & 3 mysql -u root mysql 4 UPDATE user SET Password=PASSWORD('YOURNEWPASSWORD') WHERE User='root'; FLUSH PRIVILEGES; exit;
Viewing privileges granted to a user
select * from mysql.user;
- Parse results by eye (not pretty)
SHOW GRANTS FOR username@ipaddress;
- For current user
Show the character set for a database
SHOW CREATE DATABASE database_name;
Show the database engine type
USE DATABASE database_name; SHOW CREATE TABLE table_name;
MySQL config/option file search order
The MySQL configuration file (referred to as an
option file) can be located in multiple places, but the order of precedence determines which configuration file will be used when you do not explicitly specify a file.
|defaults-extra-file||The file specified with --defaults-extra-file=path, if any|
As mentioned on the Using Option Files  reference page running
mysql with the
--help option will show you the preconfigured locations that MySQL will search for the options file.
$ mysql --help --verbose | grep -A 1 'Default options'
Default options are read from the following files in the given order: /etc/my.cnf /etc/mysql/my.cnf /usr/etc/my.cnf ~/.my.cnf
- On Unix platforms, MySQL ignores configuration files that are world-writable. This is intentional as a security measure.
- MySQL looks for option files in the order listed in the table reads any that exist. If multiple instances of a given option are found, the last instance takes precedence.
- Exception: For
mysqld, the first instance of the
--useroption is used as a security precaution, to prevent a user specified in an option file from being overridden on the command line.
- Exception: For
~represents the current user's home directory (the value of $HOME).
SYSCONFDIRrepresents the directory specified with the
SYSCONFDIRoption to CMake when MySQL was built. By default, this is the etc directory located under the compiled-in installation directory.
MYSQL_HOMEis an environment variable containing the path to the directory in which the server-specific
my.cnffile resides. If
MYSQL_HOMEis not set and you start the server using the
mysqld_safeattempts to set
DATADIRrepresent the path names of the MySQL base directory and data directory, respectively.
- If there is a
DATADIRbut not in
- Otherwise, if
MYSQL_HOMEis not set and there is no
In MySQL 5.5, use of
DATADIR as the location for
my.cnf is deprecated.
/usr/local/mysql/data for a binary installation or
/usr/local/var for a source installation. Note that this is the data directory location that was specified at configuration time, not the one specified with the --datadir option when mysqld starts. Use of --datadir at runtime has no effect on where the server looks for option files, because it looks for them before processing any options.
Any long option that may be given on the command line when running a MySQL program can be given in an option file as well. To get the list of available options for a program, run it with the
- 6.3.1 User Names and Passwords
- 9.2 Schema Object Names
- Are there multiline comment delimiters in SQL that are vendor agnostic?
- MySql: Give Root User Logon Permission From Any Host
- how do I see which user I am logged in as in MySQL?
- WikiBooks - MySQL CheatSheet
- Set / Change / Reset the MySQL root password on Ubuntu Linux
- MySQL :: MySQL 5.0 Reference Manual :: C.5.4.1 How to Reset the Root Password
- How can I show user's privileges in MySQL?
- How to show user privileges on mysql
- How do I see what character set a database / table / column is in MySQL?
- How can I check MySQL engine type for a specific table?
- MySQL 5.5 Reference Manual :: 4 MySQL Programs :: 4.2 Using MySQL Programs :: 4.2.6 Using Option Files