Section 1. Installation
Section 2. Account management
mysql -u user -p password dbname
select host, user, password from mysql.user;
create user 'username'@'host'
create user 'admin'@'localhost' identified by 'password';
grant all on *.* to 'admin'@'localhost';
create user 'custom'@'localhost' identified by 'password';
grant all on dbname.* to 'custom'@'localhost';
show grants for 'custom'@'localhost';
pt-show-grants (install percona-toolkit first)
alter user 'custom'@'localhost' identified by 'new-password';
drop user 'custom'@'localhost';
helpful knowledge base related user management :
https://dev.mysql.com/doc/refman/8.0/en/privileges-provided.html
https://dev.mysql.com/doc/refman/8.0/en/connection-access.html
https://dev.mysql.com/doc/refman/8.0/en/user-resources.html
https://dev.mysql.com/doc/refman/8.0/en/problems-connecting.html
Section 3. Help or man page Some usefull things are :
\G=display-vertical
\e=edit query from text editor.
tee=write-everything, example : tee /var/tmp/mysql_tee.out
notee=stop-writing
pager=set reading mode, example : pager less
nopager=set reading mode to default
status=get information from the server.
system=execute shell
source=execute script file
Section 4. Backup database
mysqldump -u root -p[root_password] [database_name] > dumpfilename.sql
mysqldump -u root -p[root_password] [database1] [database2] [database-etc] > dumpfilename.sql
mysqldump -u root -p[root_password] –all-databases > dumpfilename.sql
mysqldump -u root -p[root_password] [database] [table-of-database] > dumpdatabase_table.sql
Section 5. Restore database
mysql -u root -p [password]
create database [database-name];
mysql -u root -p [root_password] [database_name] < dumpfilename.sql
Section 6. Quering data
show tables;
desc tables_1;
select column1, column2, columnX from tables_1;
select column1, column2, columnX from tables_1 where id='X';
select column1, column2, columnX from tables_1 where id=X AND/OR status=X;
select * from members where membership_number IN (1,2,3);
select * from members where membership_number NOT IN (2);
select * from members where membership_number > 5;
select * from members where membership_number < 5;
select * from members order by firstname;
select * from members order by field(age, gender);
select * from members where membership_number BETWEEN 1 AND 5;
select distinct * from member; #ignoring duplicate
select distinct count(distinct age) from students where age='18';
select * from students where Name like 'A%'
select * from students where Name like '%N'
select firstname, lastname from employees
where officecode in (select officecode from offices where country ='USA');
Section 7. Modifying data
#Create table first
mysql> create table datamahasiswa (
-> id Int(3),
-> name Varchar(15),
-> email Varchar(20)
-> );
Query OK, 0 rows affected (0.08 sec)
mysql> show tables;
mysql> desc datamahasiswa;
#add new column last_name
mysql> alter table datamahasiswa add last_name varchar (20) after name;
#insert values to table
mysql> insert into datamahasiswa values
('1', 'darin', 'doang', '[email protected]'),
('2', 'lxa', 'poetri', '[email protected]'),
('3', 'gita', 'putri', '[email protected]');
mysql> insert into datamahasiswa(id, name) values(4,'keisha');
mysql> select * from datamahasiswa;
+------+--------+-----------+----------------------+
| id | name | last_name | email |
+------+--------+-----------+----------------------+
| 1 | darin | doang | [email protected] |
| 2 | lxa | poetri | [email protected] |
| 3 | gita | putri | [email protected] |
| 4 | keisha | null | null |
+------+--------+-----------+----------------------+
#delete values
mysql> delete from datamahasiswa where id='2';
#update values
mysql> update datamahasiswa set name='noye' where id ='1' and last_name ='doang';
#delete column
mysql> alter table datamahasiswa drop last_name;
#rename table
mysql> rename table datamahasiswa to tbl_datamhs;
For further reference : http://www.mysqltutorial.org/basic-mysql-tutorial.aspx https://www.tecmint.com/gliding-through-database-mysql-in-a-nutshell-part-i/ https://www.tecmint.com/learn-mysql-mariadb-advance-functions-sql-queries/