Benutzer-Werkzeuge

Webseiten-Werkzeuge


becki:linux:mysql_commands

Dies ist eine alte Version des Dokuments!


MySql Tips and Commands

— slack 11.0 2007-06-26 , slack 12.0 2007-10-18

Setup

Install inital Database

According to /etc/rc.d/rc.mysqld do the following:

su -                          # become user 'root'
chmod +x /etc/rc.d/rc.mysqld  # start mysqld at every boot
mysql_install_db --user=mysql # install inital database (/var/lib/mysql)
/etc/rc.d/rc.mysqld start     # start mysqld

Set password for root user

Get your values for <yourHostName> and <yourDomainName> with hostname -f

mysql_install_db strongly recommends to setup a root password:

su -                         # become user 'root'
mysqladmin -u root password <yourPassword>
#mysqladmin -u root -h <yourHostName> password <yourPassword>

Don't issue the second command, it fails, because of 2 independent reasons:

  1. Access from other hostnames than 'localhost' is not allowed and
  2. <yourHostName> is somehow always extended to <yourHostName>.<yourDomainName> on Slackware. (More in Mysql manual at „4.2.11 Causes of Acccess denied Errors“

We don't care about this quirk and just delete all rows from the table mysql/user which contain <yourHostName> as host:

pager most;
USE mysql;
SELECT * FROM USER; # TO CHECK
DELETE FROM USER WHERE host='<yourHostName>'; 
FLUSH privileges;
SELECT * FROM USER; # TO CHECK

DELETEME The follwing is obsolete since 2007-10-18 (Slack 12.0)

Comment out the line SKIP=„–skip-networking“ in /etc/rc.d/rc.mysqld and restart mysqld with /etc/rc.d/rc.mysqld resart. Log into the monitor with mysql -u root -p, then issue the following commands:

pager most;
USE mysql;
SELECT * FROM USER; # TO CHECK
UPDATE USER SET host='<yourHostName>.<yourDomainName>' WHERE host='<yourHostName>';
FLUSH privileges;
SELECT * FROM USER; # TO CHECK

Now the command mysqladmin -u root -h <yourHostName> password <yourPassword> should work

Comment in the line SKIP=„–skip-networking“ in /etc/rc.d/rc.mysqld again and restart mysqld with /etc/rc.d/rc.mysqld resart again.

MySql Commands

Misc

PAGER most; /* or */ PAGER less; # Redirect output TO a pager
NOPAGER;                         # Switch off redirection
 
SHOW DATABASES; # Display the availabel databaese ON the server
 
USE myDatabase; # Subsequent commands refer TO myDatabase

See also Secrets of the MySQL Client Command Line

Managing Databases

/* Create a database with name myDb.
    Shell alternative: mysqladmin create myDb */
CREATE DATABASE myDb;
 
/* Create a user for the new datase with all possible access privileges.
    Affects mysql.user and mysql.table??? */
GRANT ALL PRIVILEGES ON myDb.* TO myUser@localhost IDENTIFIED BY 'myPassword';
 
/* Delete the database with all tables. */
DROP DATABASE myDb;

Managing Tables

SHOW TABLES; # SHOW ALL TABLES OF a dababase
 
DESCRIBE myTable; # SHOW COLUMN DATA types OF a TABLE
 
RENAME TABLE oldname TO newname;
 
/* To alter the data tye of a column or eg to remove auto_increment. */
ALTER TABLE myTable MODIFY myColumn MEDIUMINT UNSIGNED;
 
/* Delete Tables */
DROP TABLE [IF EXISTS] myTable [, my2ndTable, ...] [RESTRICT | CASCADE]

Working with Data

SELECT * FROM myTable; # SHOW content OF a TABLE
 
/* Query 2 tables at the same time (see 3.6.6. Using Foreign Keys) */
SELECT a.*, b.myCol1 FROM myTabA a INNER JOIN myTabB b ON a.id = b.a_id WHERE ...;
 
/* To test for NULL, you cannot use the arithmetic comparison operators such as =, <, or <> */
SELECT * FROM myTable WHERE myColumn IS NULL;
 
SELECT COUNT(*) FROM myTable; # Shows the NUMBER OF LINES OF a TABLE
 
TRUNCATE myTable; # DELETE ALL ROWS OF a TABLE, LIKE DELETE, but faster
 
/* Delete rows with conditions */
DELETE [LOW_PRIORITY] FROM myTable [WHERE where_definition] [LIMIT ROWS]

Backup and Restore

mysqldump --opt dbname                   # Backup a Database
mysql db_name < backup-file.sql          # Restore the Database
mysqldump [--opt] --all_databases > file # Backup  all Databases

Old, German & unsorted

pager or \P; nopager or \pager on/off, wich pager to use is set in ??? env-variable

INSERT INTO tabelname SET colname1=('c1val1'), ...;
  Es müssen nicht alle Spalten angegeben werden
INSERT INTO urlConnector VALUES (2, 1);
  alle Spalten müssen angegben werden; NULL wenn kein Wert vorliegt

mysql -u user --pager="less -S" database
  Als Benutzer user in die Datenbank database einloggen. Output geht nach less, und Zeilen werden nicht umgebrochen
  Wenn man schon eingeloggt ist, kann man das gleiche erreichen mit "mysql> \P less -S"
  -t      Output in table format (for batch mode)
  -vvv    very verbose (for batch mode ?)
  -?      show all command line options (unlike 'man mysql' or the manual)
  --pager use pager set in $PAGER environment variable

varchar(<länge>)	Datentyp String
date			Datentyp Datum

load data infile "<dateiname>" into table <table>;
  Daten aus einer Textdatei tabgetrennt in die Tabelle laden
Cookies helfen bei der Bereitstellung von Inhalten. Diese Website verwendet Cookies. Mit der Nutzung der Website erklären Sie sich damit einverstanden, dass Cookies auf Ihrem Computer gespeichert werden. Außerdem bestätigen Sie, dass Sie unsere Datenschutzerklärung gelesen und verstanden haben. Wenn Sie nicht einverstanden sind, verlassen Sie die Website. Weitere Information
becki/linux/mysql_commands.1256675685.txt.gz · Zuletzt geändert: 2009-10-27 20:34 von becki

Impressum - Datenschutzerklärung