Basic management of mysql database

1. The basic concept of the
database The database is an advanced form management

Common databases: mysql, oracle, SQL server

mysql
SUN------------>oracle

Common nouns in the database:
field: the header in the table
table: table
library: the directory where the table is stored
Query: view the specified content in the table

2. Installation of mariadb
dnf search mariadb
Insert picture description here
dnf install mariadb-server.x86_64 -y
Insert picture description here
rpm -ql mariadb #View
Insert picture description here
/var/lib/mysql
#Data directory /etc/my.cnf.d/ mariadb-server.cnf #Main configuration file
/usr/lib/systemd/system/mariadb.service #Service startup script

systemctl enable --now mariadb #Start the service and
Insert picture description here
initialize the database securely
mysal-secure-installation
Insert picture description here
Insert picture description here

Log in to
mysql -uroot -p #enter and then enter the password.
Insert picture description here
By default, the database is open to the outside world. Generally, in the enterprise, you need to close the
netstat -antulpe | grep mysql to
Insert picture description here
close the database network port. You need to edit the configuration file
vim /etc/my.cnf. d/mariadb-server.cnf
Insert picture description here
Insert picture description here
systemctl restart mariadb #Restart the service
Insert picture description here
At this point the port has been closed:
Insert picture description here
3. Basic management of the database

#Note: A semicolon should be marked after each command
1) View
SHOW DATABASES;
Insert picture description here
#Display the name of the library USE mysql; #Enter the mysql library
Insert picture description here
SHOW TABLES; #Display all tables in the library
Insert picture description here
SHOW TABLES FROM mysql; #Display the mysql library In all tables, this command does not need to enter the mysql database
Insert picture description here
SELECT * FROM user; #Query all data
Insert picture description here
SELECT Host,User,Password FROM user;
Insert picture description here
#Query the specified field SELECT Host FROM mysql.user WHERE User='root';
Insert picture description here
#Query CREATE DATABASE westos; #Create a library westos
CREATE TABLE westos.linux (username varchar(6) not null, password varchar(30) not null ); #Create a linux table in westos, the entries are username and password and are not empty
Insert picture description here
DESC westos. linux; #Display the linux table structure in the westos library
Insert picture description here
INSERT INTO westos.linux VALUES ('lee',123);
Insert picture description here
Insert picture description here
INSERT INTO westos.linux VALUES ('lee1',123),('lee2',123);
Insert picture description here
change the library Name, but it may cause data loss, it is not recommended to do this
cd /var/lib/mysql/
mv westos lee
systemctl restart mariadb
Insert picture description here
Insert picture description here
USE westos;
ALTER TABLE linux RENAME userlist; #Change the name of the linux table to userlist
Insert picture description here
ALTER TABLE linux ADD age varchar(4); #Add the age field to the linux table
Insert picture description here
ALTER TABLE linux DROP age; #Delete the age field
Insert picture description here
ALTER TABLE linux ADD age varchar(4) AFTER username; #After adding the age field to the username field
Insert picture description here
UPDATE linux SET age='123';
Insert picture description here
#Set all age to 123 UPDATE linux SET age='18' WHERE username='lee1' ; #Set the age of lee1 to 18
Insert picture description here
DELETE FROM linux WHERE username='lee' and age='123'; #Delete the table entry whose username is lee and age is equal to 123
Insert picture description here
DROP TABLE linux; #Delete linux table
Insert picture description here
4, database password management
1 ) Change the password (you already know the password)
mysqladmin -uroot -pwestos password The lee
Insert picture description here
password is unsafe, you can use another method:
mysqladmin -uroot -p password
Insert picture description here
2) Crack the database password (use if you forget it)
systemctl stop mariadb
mysqld_safe --skip-grant-tables &You
Insert picture description here
Insert picture description here
can enter directly without entering a password at this time:
mysql -uroot
Insert picture description here
UPDATE mysql.user SET authentication_string=password('123') WHERE User='root';
exit
Insert picture description here
ps aux | grep mysql #Check the mysql process
kill -9 xxxx
#Close the related process systemctl restart mariadb.
Insert picture description here
At this time, you can enter the password you changed.
Insert picture description here
5. User authorization
CREATE USER westos@localhost identified by'westos';
localhost means that this user can only log in to the database on this machine
Insert picture description here
CREATE USER westos@'%' identified by'westos';
% means that this user can log in to the database through the network, but the premise of the network login is that the database must open the network login interface.
Insert picture description here
Insert picture description here
Create an experimental environment:
CREATE DATABASE westostest;
CREATE TABLE westostest.userlist (
-> username varchar(10) not null,
-> password varchar(30) not null
-> );
INSERT INTO westostest.userlist VALUES('test','123');
Insert picture description here
GRANT SELECT ON westostest.* TO westos@localhost; #Grant westos@localhost user query power
SHOW GRANTS FOR westos@localhost; #Show grant westo@localhost user authority
Insert picture description here
. GRANT iNSERT ON westostest * TO westos @ localhost; # empowerment westos @ localhost user inserted
Insert picture description here
at this time westos user can insert
Insert picture description here
Insert picture description here
Insert picture description here
REVOKE iNSERT ON westostest * FROM westos @ localhost;. # cancel empowerment westos @ localhost user inserted
Insert picture description here
Insert picture description here
DROP user westos@'%'; #Delete user
SELECT Host,User FROM mysql.user; #At this time, you can see that there is no such user.
Insert picture description here
6. Database backup and recovery
Database backup:
mysqldump -uroot -pwestos mysql> /mnt /mysql.sql #Back up mysql database
mysqldump -uroot -pwestos --all-databases> /mnt/all.sql #Back up all databases
mysqldump -uroot -pwestos --all-databases --no-data> /mnt/all_nodata. sql #Back up all libraries but do not back up the structure of the database, that is, back up its structure
mysqldump -uroot -pwestos westostest> /mnt/westostest.sql #Back up the westostest database
Insert picture description here
Restore the database:
Insert picture description here
Insert picture description here
Insert picture description here
mysql -uroot -pwestos -e "CREATE DATABASE westostest;" #Create a database named westostest
mysql -uroot -pwestos westostest </mnt/ westostest.sql #Enter the previous backup into the new database.
Insert picture description here
At this point, you can see the database recovery:
Insert picture description here
7. Installation of phpmyadmin

dnf install httpd php php-mysqlnd -y
Insert picture description here
systemctl enable --now httpd
systemctl stop firewalld
Insert picture description here
cd /mnt/
cp phpMyAdmin-3.4.0-all-languages.tar.bz2 /var/www/html/
cd /var/www/html /
tar jxf phpMyAdmin-3.4.0-all-languages.tar.bz2 -C /var/www/html/
mv phpMyAdmin-3.4.0-all-languages/ mysqladmin
cd mysqladmin/
Insert picture description here
Insert picture description here
less Documentation.txt #View the help file
Insert picture description here
cp config .sample.inc.php config.sample.php
Insert picture description here
access and log in at this time, you can see the database you created:
Insert picture description here
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/shanshuyue/article/details/113615186