Guest User

Untitled

a guest
Nov 26th, 2018
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.24 KB | None | 0 0
  1.  
  2. #restore .sql file to the database
  3. mysql target-db-name < sql-file-name.sql -uuser -p
  4.  
  5. # [mysql dir]/bin/mysql -h hostname -u root -p
  6. Create a database on the sql server.
  7. mysql> create database [databasename];
  8.  
  9. List all databases on the sql server.
  10. mysql> show databases;
  11.  
  12. Switch to a database.
  13. mysql> use [db name];
  14.  
  15. To see all the tables in the db.
  16. mysql> show tables;
  17.  
  18. To see database's field formats.
  19. mysql> describe [table name];
  20.  
  21. To delete a db.
  22. mysql> drop database [database name];
  23.  
  24. To delete a table.
  25. mysql> drop table [table name];
  26.  
  27. Show all data in a table.
  28. mysql> SELECT * FROM [table name];
  29.  
  30. Returns the columns and column information pertaining to the designated table.
  31. mysql> show columns from [table name];
  32.  
  33. Show certain selected rows with the value "whatever".
  34. mysql> SELECT * FROM [table name] WHERE [field name] = "whatever";
  35.  
  36. Show all records containing the name "Bob" AND the phone number '3444444'.
  37. mysql> SELECT * FROM [table name] WHERE name = "Bob" AND phone_number = '3444444';
  38.  
  39. Show all records not containing the name "Bob" AND the phone number '3444444' order by the phone_number field.
  40. mysql> SELECT * FROM [table name] WHERE name != "Bob" AND phone_number = '3444444' order by phone_number;
  41.  
  42. Show all records starting with the letters 'bob' AND the phone number '3444444'.
  43. mysql> SELECT * FROM [table name] WHERE name like "Bob%" AND phone_number = '3444444';
  44.  
  45. Show all records starting with the letters 'bob' AND the phone number '3444444' limit to records 1 through 5.
  46. mysql> SELECT * FROM [table name] WHERE name like "Bob%" AND phone_number = '3444444' limit 1,5;
  47.  
  48. Use a regular expression to find records. Use "REGEXP BINARY" to force case-sensitivity. This finds any record beginning with a.
  49. mysql> SELECT * FROM [table name] WHERE rec RLIKE "^a";
  50.  
  51. Show unique records.
  52. mysql> SELECT DISTINCT [column name] FROM [table name];
  53.  
  54. Show selected records sorted in an ascending (asc) or descending (desc).
  55. mysql> SELECT [col1],[col2] FROM [table name] ORDER BY [col2] DESC;
  56.  
  57. Return number of rows.
  58. mysql> SELECT COUNT(*) FROM [table name];
  59.  
  60. Sum column.
  61. mysql> SELECT SUM(*) FROM [table name];
  62.  
  63. Join tables on common columns.
  64. mysql> select lookup.illustrationid, lookup.personid,person.birthday from lookup left join person on lookup.personid=person.personid=statement to join birthday in person table with primary illustration id;
  65.  
  66. Creating a new user. Login as root. Switch to the MySQL db. Make the user. Update privs.
  67. # mysql -u root -p
  68. mysql> use mysql;
  69. mysql> INSERT INTO user (Host,User,Password) VALUES('%','username',PASSWORD('password'));
  70. mysql> flush privileges;
  71.  
  72. Change a users password from unix shell.
  73. # [mysql dir]/bin/mysqladmin -u username -h hostname.blah.org -p password 'new-password'
  74.  
  75. Change a users password from MySQL prompt. Login as root. Set the password. Update privs.
  76. # mysql -u root -p
  77. mysql> SET PASSWORD FOR 'user'@'hostname' = PASSWORD('passwordhere');
  78. mysql> flush privileges;
  79.  
  80. Recover a MySQL root password. Stop the MySQL server process. Start again with no grant tables. Login to MySQL as root. Set new password. Exit MySQL and restart MySQL server.
  81. # /etc/init.d/mysql stop
  82. # mysqld_safe --skip-grant-tables &
  83. # mysql -u root
  84. mysql> use mysql;
  85. mysql> update user set password=PASSWORD("newrootpassword") where User='root';
  86. mysql> flush privileges;
  87. mysql> quit
  88. # /etc/init.d/mysql stop
  89. # /etc/init.d/mysql start
  90.  
  91. Set a root password if there is on root password.
  92. # mysqladmin -u root password newpassword
  93.  
  94. Update a root password.
  95. # mysqladmin -u root -p oldpassword newpassword
  96.  
  97. Allow the user "bob" to connect to the server from localhost using the password "passwd". Login as root. Switch to the MySQL db. Give privs. Update privs.
  98. # mysql -u root -p
  99. mysql> use mysql;
  100. mysql> grant usage on *.* to bob@localhost identified by 'passwd';
  101. mysql> flush privileges;
  102.  
  103. Give user privilages for a db. Login as root. Switch to the MySQL db. Grant privs. Update privs.
  104. # mysql -u root -p
  105. mysql> use mysql;
  106. mysql> INSERT INTO db (Host,Db,User,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv) VALUES ('%','databasename','username','Y','Y','Y','Y','Y','N');
  107. mysql> flush privileges;
  108.  
  109. or
  110.  
  111. mysql> grant all privileges on databasename.* to username@localhost;
  112. mysql> flush privileges;
  113.  
  114. To update info already in a table.
  115. mysql> UPDATE [table name] SET Select_priv = 'Y',Insert_priv = 'Y',Update_priv = 'Y' where [field name] = 'user';
  116.  
  117. Delete a row(s) from a table.
  118. mysql> DELETE from [table name] where [field name] = 'whatever';
  119.  
  120. Update database permissions/privilages.
  121. mysql> flush privileges;
  122.  
  123. Delete a column.
  124. mysql> alter table [table name] drop column [column name];
  125.  
  126. Add a new column to db.
  127. mysql> alter table [table name] add column [new column name] varchar (20);
  128.  
  129. Change column name.
  130. mysql> alter table [table name] change [old column name] [new column name] varchar (50);
  131.  
  132. Make a unique column so you get no dupes.
  133. mysql> alter table [table name] add unique ([column name]);
  134.  
  135. Make a column bigger.
  136. mysql> alter table [table name] modify [column name] VARCHAR(3);
  137.  
  138. Delete unique from table.
  139. mysql> alter table [table name] drop index [colmn name];
  140.  
  141. Load a CSV file into a table.
  142. mysql> LOAD DATA INFILE '/tmp/filename.csv' replace INTO TABLE [table name] FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' (field1,field2,field3);
  143.  
  144. Dump all databases for backup. Backup file is sql commands to recreate all db's.
  145. # [mysql dir]/bin/mysqldump -u root -ppassword --opt >/tmp/alldatabases.sql
  146.  
  147. Dump one database for backup.
  148. # [mysql dir]/bin/mysqldump -u username -ppassword --databases databasename >/tmp/databasename.sql
  149.  
  150. Dump a table from a database.
  151. # [mysql dir]/bin/mysqldump -c -u username -ppassword databasename tablename > /tmp/databasename.tablename.sql
  152.  
  153. Restore database (or database table) from backup.
  154. # [mysql dir]/bin/mysql -u username -ppassword databasename < /tmp/databasename.sql
  155. Create Table Example 1.
  156.  
  157. mysql> CREATE TABLE [table name] (firstname VARCHAR(20), middleinitial VARCHAR(3), lastname VARCHAR(35),suffix VARCHAR(3),officeid VARCHAR(10),userid VARCHAR(15),username VARCHAR(8),email VARCHAR(35),phone VARCHAR(25), groups VARCHAR(15),datestamp DATE,timestamp time,pgpemail VARCHAR(255));
  158. Create Table Example 2.
  159.  
  160. mysql> create table [table name] (personid int(50) not null auto_increment primary key,firstname varchar(35),middlename varchar(50),lastnamevarchar(50) default 'bato');
Add Comment
Please, Sign In to add comment