sudo mysqld_safe
sudo mysqladmin shutdown
sudo mysql
GRANT ALL ON menagerie.* TO 'user'@'localhost';
\q
mysql
SELECT USER();
CREATE DATABASE menagerie;
USE menagerie;
SELECT DATABASE();
CREATE TABLE pet (name VARCHAR(20), owner VARCHAR(20),
-> species VARCHAR(20), sex CHAR(1), birth DATE, death DATE);
SHOW TABLES;
DESCRIBE pet;
- Secure the MySQL accounts. See 5.3.1. General Security Guidelines
- See Chapter 10: Data Types
- Delimiter can be specified in the
LOAD DATAstatement - When writing tab-delimited files with Vim,
set noexpandtab - Pick up from 3.3.4.6. Working with
NULLvalues - Databases are stored in
/usr/local/mysql/data - User configuration file is
~/.my.cnf
After installing from the Mac OS X PKG Installer, you can use a different data directory for your databases:
-
Add the following lines to your
.bash_profileexport PATH=${PATH}:/usr/local/mysql/bin # mysqld_safe and mysqladmin export PATH=${PATH}:/usr/local/mysql/scripts # mysql_install_db export PATH=${PATH}:/usr/local/mysql/support-files # mysql.server -
Then install the new data directory
$ sudo mysql_install_db --basedir=/usr/local/mysql --datadir=/path/to/my/foobardata --user=Kitsudois needed to invoke the--useroption. ReplaceKitwith your own username. This will be the username that will run themysqld_safeserver later on.--basediris the MySQL installation directory. By default it is/usr/local/mysql. I needed to specify this explicitly because the script looked for the default installation directory from the current directory I was working on (~).--datadiris where you want to store your database files -
After executing the script, edit your
.my.cnffile and add the following lines under[mysqld]:[mysqld] datadir=/path/to/my/foobardata -
Take a look at the directory where you installed your database files:
$ cd /path/to/my/foobardata $ ls -lNote that there is no
*.errfile yet. The_mysqluser needs this to log errors. If you runmysqld_safe, you’ll get a permission denied error:$ mysqld_safe chown: /path/to/my/foobardata/host.name.err: Operation not permittedTo work around this, the
*.errfile must be created and assigned to_mysql. Runningsudo mysql_safedoes the job. The&makes sure you retain control of the shell prompt.$ sudo mysqld_safe &Then
shutdownthe server:$ sudo mysqladmin shutdown -
After following the above steps, you can now use your own
datadir, without having tosudo:$ mysqld_safe & $ sudo mysqladmin shutdown
To use MySQL with SQLAlchemy and MySQLdb in Python:
-
Install
mysqldbandsqlalchemy$ pip install MySQL-python $ pip install SQLAlchemy -
Add the following line to your
.bash_profile(from this Stack Overflow answer)export DYLD_LIBRARY_PATH=/usr/local/mysql/lib -
Then try it out with Python:
$ python >>> from sqlalchemy import create_engine >>> engine = create_engine("mysql://localhost") >>> engine.execute("SELECT 1").scalar() >>> engine.execute("SELECT USER()").scalar() >>> engine.execute("SELECT SIN(PI())").scalar() >>> engine.execute("SHOW DATABASES").fetchall()



