MySQL Usage: Difference between revisions

From CLONWiki
Jump to navigation Jump to search
Boiarino (talk | contribs)
No edit summary
 
Boiarino (talk | contribs)
No edit summary
Line 1: Line 1:
---++Dump/Restore
To login as root:
Login as root
 
<pre>
  ssh root@clondb1
mysql -u root -p
  mysql -u root -p
</pre>
 
Dump one database or several databases
To dump one database or several databases:
<pre>
 
mysqldump --opt db_name > /usr/local/clas/mysql_dump/backup-file.sql
  mysqldump --opt db_name > /usr/local/clas/mysql_dump/backup-file.sql
mysqldump --databases db_name1 [db_name2 ...] > /usr/local/clas/mysql_dump/my_databases.sql
  mysqldump --databases db_name1 [db_name2 ...] > /usr/local/clas/mysql_dump/my_databases.sql
</pre>
 
Restore one database
To restore one database:
<pre>
 
mysql -u root -p db_name < /usr/local/clas/mysql_dump/backup-file.sql
  mysql -u root -p db_name < /usr/local/clas/mysql_dump/backup-file.sql
</pre>
 
Restore all databases ('USE <db_name>' statement must be in the file):
To restore all databases ('USE <db_name>' statement must be in the file):
<pre>
 
mysql -u root -p < /usr/local/clas/mysql_dump/backup-file.sql
  mysql -u root -p < /usr/local/clas/mysql_dump/backup-file.sql
</pre>
 
Copy database from one server to another
To copy database from one server to another:
<pre>
 
mysqldump --opt db_name | mysql --host=remote_host -C db_name
  mysqldump --opt db_name | mysql --host=remote_host -C db_name
</pre>
 
Dump all databases
To dump all databases:
<pre>
 
mysqldump --all-databases > /usr/local/clas/mysql_dump/all_databases.sql
  mysqldump --all-databases > /usr/local/clas/mysql_dump/all_databases.sql
</pre>
 
There is a script '/database/dumpdb' (from Bryan Hess) which is running from cron job,
NOTE: there is a script '/database/dumpdb' (obtained from Bryan Hess and adjusted) which is running from cron job,
it is dumping all databases from clondb1 to /usr/local/clas/mysql_dump
it is dumping all databases from clondb1 to /mysql/clondb1_dump/ directory on fileserver. See usage inside the script.
directory on fileserver. See usage inside the script.
 
Creating new table: for example:
To creating new table (example):
<pre>
 
create table clonalarm(
  create table clonalarm(
       alarm_id integer not null,
       alarm_id integer not null,
       alarm_status integer default NULL,
       alarm_status integer default NULL,
Line 37: Line 37:
       alarm_text varchar(120) default NULL,
       alarm_text varchar(120) default NULL,
       help_id integer default NULL
       help_id integer default NULL
)\g
  )\g
</pre>
 
To import data from ASCII file into existing table use 'mysqlimport' (example):
 
  mysqlimport -u root -p clasprod ./clonalarm.txt


To import data from ASCII file into existing table use 'mysqlimport', for example:
<pre>
mysqlimport -u root -p clasprod ./clonalarm.txt
</pre>
where 'clasprod' is database name and 'clonalarm.txt' is file in /database directory.
where 'clasprod' is database name and 'clonalarm.txt' is file in /database directory.
File name must coinside with table name !
File name must coinside with table name !


---++Autorization
To autorize 'clasrun' access from clon10.jlab.org to 'clas_online' database do following:
Login as root
 
<pre>
  mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP ON clas_online.* TO clasrun@clon10.jlab.org;
mysql -u root -p
 
</pre>
For example, to autorize 'clasrun' access from clon10.jlab.org to 'clas_online' database do following:
<pre>
mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP ON clas_online.* TO clasrun@clon10.jlab.org;
</pre>
Other example:
Other example:
<pre>
 
mysql> grant all on clas_online.* to clasrun@clon10.jlab.org identified by 'your_mysql_user_password';
  mysql> grant all on clas_online.* to clasrun@clon10.jlab.org identified by 'your_mysql_user_password';
</pre>
 
To revoke access, do following:
To revoke access, do following:
<pre>
revoke all on clas_online.* from clasrun@clon10.jlab.org;
</pre>


---++User access
  revoke all on clas_online.* from clasrun@clon10.jlab.org;
 
For example if 'clasrun' want to assess 'test' database on 'clondb1' from remote host, it should use following commands:
For example if 'clasrun' want to assess 'test' database on 'clondb1' from remote host, it should use following commands:
<pre>
 
mysql -h clondb1 -u clasrun
  mysql -h clondb1 -u clasrun
mysql> use clasprod;
  mysql> use clasprod;
mysql> show tables;
  mysql> show tables;
mysql> describe clonalarm;
  mysql> describe clonalarm;
mysql> select * from clonalarm;
  mysql> select * from clonalarm;
mysql> quit
  mysql> quit
</pre>
-- Main.SergeyBoyarinov - 19 Jun 2006

Revision as of 12:44, 10 January 2007

To login as root:

 ssh root@clondb1
 mysql -u root -p

To dump one database or several databases:

 mysqldump --opt db_name > /usr/local/clas/mysql_dump/backup-file.sql
 mysqldump --databases db_name1 [db_name2 ...] > /usr/local/clas/mysql_dump/my_databases.sql

To restore one database:

 mysql -u root -p db_name < /usr/local/clas/mysql_dump/backup-file.sql

To restore all databases ('USE <db_name>' statement must be in the file):

 mysql -u root -p < /usr/local/clas/mysql_dump/backup-file.sql

To copy database from one server to another:

 mysqldump --opt db_name | mysql --host=remote_host -C db_name

To dump all databases:

 mysqldump --all-databases > /usr/local/clas/mysql_dump/all_databases.sql

NOTE: there is a script '/database/dumpdb' (obtained from Bryan Hess and adjusted) which is running from cron job, it is dumping all databases from clondb1 to /mysql/clondb1_dump/ directory on fileserver. See usage inside the script.

To creating new table (example):

 create table clonalarm(
      alarm_id integer not null,
      alarm_status integer default NULL,
      alarm_time date default NULL,
      system char(20) default NULL,
      alarm_text varchar(120) default NULL,
      help_id integer default NULL
 )\g

To import data from ASCII file into existing table use 'mysqlimport' (example):

 mysqlimport -u root -p clasprod ./clonalarm.txt

where 'clasprod' is database name and 'clonalarm.txt' is file in /database directory. File name must coinside with table name !

To autorize 'clasrun' access from clon10.jlab.org to 'clas_online' database do following:

 mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP ON clas_online.* TO clasrun@clon10.jlab.org;

Other example:

 mysql> grant all on clas_online.* to clasrun@clon10.jlab.org identified by 'your_mysql_user_password';

To revoke access, do following:

 revoke all on clas_online.* from clasrun@clon10.jlab.org;

For example if 'clasrun' want to assess 'test' database on 'clondb1' from remote host, it should use following commands:

 mysql -h clondb1 -u clasrun
 mysql> use clasprod;
 mysql> show tables;
 mysql> describe clonalarm;
 mysql> select * from clonalarm;
 mysql> quit