?print-pdf' Created for
These concepts will be discussed further.
systemctl status mysql
# ● mariadb.service - MariaDB 10.2.38 database server
# Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
# Drop-In: /etc/systemd/system/mariadb.service.d
# └─migrated-from-my.cnf-settings.conf
mycli: A Terminal Client for MySQL with AutoCompletion and Syntax Highlighting.
Requires python
### the client:
mysql --version
# mysql Ver 14.14 Distrib 5.7.11, for Linux (x86_64) using EditLine wrapper
### the server:
mysqld --version
# mysqld Ver 5.7.11 for Linux on x86_64 (MySQL Community Server (GPL))
C:\> "C:\Program Files\MySQL\MySQL Server 8.0\bin\mysqld"
C:\> "C:\Program Files\MySQL\MySQL Server 8.0\bin\mysqladmin" -u root shutdown
service mysql start
service mysql stop
service mysql restart
/etc/init.d/mysqld start
/etc/init.d/mysqld stop
/etc/init.d/mysqld restart
# connect to local mysql, using the root account:
mysql -u root -p
# Enter password:
# ...
# You should see something like:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.35-0ubuntu0.22.04.1 (Ubuntu)
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
mariadb root@localhost:(none)> select @@datadir
+-----------------+
| @@datadir |
|-----------------|
| /var/lib/mysql/ |
+-----------------+
1 row in set
Time: 0.002s
show databases; and SHOW DATABASES; are the same.desc mysql.user; will work but desc mysql.USER; will return an error ((1146, "Table 'mysql.USER' doesn't exist")) on Linux/MacOSSELECT, UPDATE, DELETE
INSERT INTO, CREATE DATABASE, ALTER DATABASE
CREATE TABLE, ALTER TABLE, DROP TABLE
CREATE INDEX, DROP INDEX
### List all databases
## only those databases for which the user have some kind of privilege (or if the user have the global SHOW DATABASES privilege)
SHOW DATABASES
### Create a database
CREATE DATABASE employees;
### Delete a database
DROP DATABASE employees;
### Selecting a Database
## Before performing any table manipulations, you need to select the database on which they will be performed
USE employees;
git clone https://github.com/datacharmer/test_db/
cd test.db
mysql -u root -p < employees.sql
# Enter password:
# INFO
# CREATING DATABASE STRUCTURE
# INFO
# ...
mysqldump -u username -p databasename > filename.sql
mysql.user table
SELECT user,host FROM mysql.user;
CREATE USER 'employees_db_admin'@'localhost' IDENTIFIED BY '1234';
If user exists, an error will be thrown. To prevent this error we can use:
CREATE USER IF NOT EXISTS 'employees_db_admin'@'localhost' IDENTIFIED BY '1234';
DROP USER 'employees_db_admin'@'localhost';
If user did not exists, an error will be thrown. To prevent this error we can use:
DROP USER IF EXISTS 'employees_db_admin'@'localhost';
GRANT [type of permission]
ON [database name].[table name]
TO ‘[username]’@'host’;
GRANT ALL PRIVILEGES ON employees.* TO 'employees_db_admin'@'localhost';
Note, that asterisk (*) means all (tables, databases, ...)
REVOKE [type of permission]
ON [database name].[table name]
FROM ‘[username]’@'host’;
REVOKE [type of permission] ON [database name].[table name] FROM ‘[username]’@‘localhost’;
Always be sure to reload all the privileges.
FLUSH PRIVILEGES;
Reset the Root Password on Ubuntu based linux, step-by-step:
# stop mysql service
sudo systemctl stop mysql
# if you don't have '/var/run/mysqld' create it with proper user wrights:
sudo mkdir -p /var/run/mysqld
sudo chown mysql:mysql /var/run/mysqld
# start the server, passwordless
sudo /usr/sbin/mysqld --skip-grant-tables --skip-networking &
# connect as root without password:
mysql -u root
# now in mysql shell, run next commands
# do not forget to change the password '1234' with yours
> FLUSH PRIVILEGES;
> USE mysql;
> ALTER USER 'root'@'localhost' IDENTIFIED BY '1234';
> quit
CREATE TABLE table_name(
column_name1 data_type,
column_name2 data_type,
....
);
CREATE TABLE artist (
artist_id SMALLINT(5) NOT NULL DEFAULT 0,
fname VARCHAR(20) DEFAULT NULL,
lname VARCHAR(20) NOT NULL,
PRIMARY KEY (artist_id)
);
Results in:
Query OK, 0 rows affected
Time: 0.280s
If table exists an error will be thrown!
# '\G' modifier is used instead of ';' to display wide results in vertical form
show CREATE TABLE artist\G
Results in:
***************************[ 1. row ]***************************
Table | artist
Create Table | CREATE TABLE `artist` (
`artist_id` smallint(5) NOT NULL DEFAULT '0',
`fname` varchar(20) DEFAULT NULL,
`lname` varchar(20) NOT NULL,
PRIMARY KEY (`artist_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
This allows us to see the create statement that MySQL engine had used. It shows even the default options, which we did not state explicitly.
SHOW TABLES;
Results in:
+----------------------+
| Tables_in_music_db |
|----------------------|
| artist |
+----------------------+
DESC artist;
Results in:
+-----------+-------------+--------+-------+-----------+---------+
| Field | Type | Null | Key | Default | Extra |
|-----------+-------------+--------+-------+-----------+---------|
| artist_id | smallint(5) | NO | PRI | 0 | |
| fname | varchar(20) | YES | | <null> | |
| lname | varchar(20) | NO | | <null> | |
+-----------+-------------+--------+-------+-----------+---------+
DROP TABLE artist;
If table do not exists, an error is thrown:
ERROR 1051 (42S02): Unknown table 'music_db.artist'
ALTER TABLE table_name
ADD new_column_name column_definition
[ FIRST | AFTER column_name ]; # optional
ALTER TABLE artist
ADD birth_date TINYINT
AFTER lname;
ALTER TABLE artist DROP birth_date;