[Linux study notes 22] basic management of mysql database

1. Introduction to the database

  1. What is a database
  • Advanced form software
  • Stored together in a certain way, can be shared by multiple users, has the smallest redundancy characteristics, and is a data collection independent of the application
  • It is a collection of data organized and stored in the secondary storage according to a certain data model
  1. Common database
MySQL Oracle MongoDB
DB2 sqlite SqlServer
  1. mysql introduction

MySQL is a relational database management system developed by the Swedish company MySQL AB and currently belongs to Oracle's products. MySQL is one of the most popular relational database management systems (RDBMS).

  1. mariadb

The MariaDB database management system is a branch of MySQL and is mainly maintained by the open source community. The purpose of using GPL license MariaDB is to be fully compatible with MySQL, including API and command lines, so that it can easily become a substitute for MySQL. In terms of storage engine, XtraDB (English: XtraDB) is used instead of MySQL's InnoDB. MariaDB was developed by Michael Widenius (English: Michael Widenius), the founder of MySQL. He sold the company MySQL AB he created to SUN for $1 billion. Since then, as SUN was acquired by Oracle, MySQL Ownership also falls into the hands of Oracle. The MariaDB name comes from the name of Maria, the daughter of Michael Widenius.

2. Install mysql and enable

  1. installation

dnf search mariadb: Find the database

dnf install mariadb-server.x86_64 -y: Install the database

  1. View configuration file

rpm -qc mariadb-server

  1. Enable database service

systemctl enable --now mariadb

Insert picture description here
Insert picture description here
Insert picture description here

3. Basic software information

  1. Start the service:mariadb.service
  2. Default port number:3306
  3. Main configuration file:
    /etc/my.cnf.d/mariadb-server.cnf
  4. Data directory:/ var / lib / mysql
    (Data directory, when mariab needs to be reinstalled, this directory needs to be cleaned up or backed up)

4. Secure initialization of the database

4.1 Close the database open port

  1. vim /etc/my.cnf.d/mariadb-server.cnf: Edit the main configuration file
skip-networking=1
#关闭数据库开放端口
  1. systemctl restart mariadb.service: Restart the database service
  2. netstat -antlupe | grep mysql: Query the database port (this command cannot query the port
netstat parameters
parameter Explanation parameter Explanation
-a Show all options
(listen related is not displayed by default)
-t Show only tcp options
-u Show only udp options -n Refuse to display aliases
(all numbers that can be displayed are converted into numbers)
-l Only list the service status in Listen -p Display the name of the program that established the related link
-r Display routing information, routing table -e Show extended information
-s Statistics according to each agreement -c At regular intervals, execute the netstat command

Insert picture description here
Insert picture description here

4.2 Execute security initialization script

  1. mysql_secure_installation:initialization
  2. mysql -u用户名 -p密码:Log in to the database (you can enter the password in plain text)
#登陆数据库方式
mysql -u用户名 -p
mysql -u用户名 -p -e "数据库命令"
mysql -u用户名 -p 数据库名 -e "数据库命令"

Insert picture description here
Insert picture description here
Insert picture description here

5. Basic database management

5.1 Database View

  1. SHOW DATABASES;: Display database name
  2. USE 数据库名;: Enter the database
  3. SHOW TABLES;: Display the tables in the database
  4. SELECT * FROM 数据库名.表名;:Query all data in the table (you can not add the database name if you have entered the database)
  5. SELECT 字段1,字段2 FROM 数据库名.表名;:Query the specified field (you can not add the database name if you have entered the database)
  6. DESC 表名;: Show table structure

Insert picture description here
Insert picture description here
Insert picture description here

5.2 New database

  1. CREATE DATABASE 数据库名;: New database

  2. New table

USE westos;	进入数据库
CREATE TABLE 表名(
	-> 字段 类型 是否为空,
    -> Username varchar(10) not null,
    -> Password varchar(10) not null
    -> );
# '->'不是sql语句的一部分,表示一个新行
# ';'表示指令结束
  1. DESC linux;: Show table structure`
  2. Insert data
#给所有字段添加
INSERT INTO 表名 VALUES('值1','值2',...,'值n');
INSERT INTO 表名 VALUES ('值1','值2',...,'值n'),('值1','值2',...,'值n')...;#添加多组值
#给指定字段添加
INSERT INTO 表名 (字段1,字段2) VALUES('值1','值2');
INSERT INTO 表名 (字段1,字段2) VALUES ('值1','值2'),('值1','值2')...;#添加多组值
  1. FLUSH PRIVIEGES;: Refresh the database

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

5.3 Database changes

  1. ALTER TABLE 原表名 RENAME 新表名;: Change the table name
  2. ALTER TABLE 表名 ADD 字段 类型;: Add field
  3. ALTER TABLE 表名 ADD 字段 类型 AFTER 已有字段;: Add a field (position after the existing field)
  4. UPDATE 表名 SET字段1=‘值1’,字段2=‘值2’ WHERE 字段=‘值’;: Change data
  5. ALTER TABLE 表名 DROP 字段名;: Delete field

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

5.4 Database deletion

  1. DELETE FROM 表名 WHERE 字段=‘值’;:Delete specified data
#指定多个条件
DELETE FROM 表名 WHERE 字段1='值' AND 字段2='值';
  1. DROP TABLE 数据库名.表名;: Delete table
  2. DROP DATABASE 数据库名;: Delete the database

Insert picture description here
Insert picture description here
Insert picture description here

6. Data password management

6.1 Database password change

mysqladmin -uroot -p password 新密码

Insert picture description here

6.2 Database password cracking

  1. systemctl stop mariadb.service: Turn off the database service
  2. mysqld_safe --skip-grant-tables &: Skip the database authorization table when starting the database
  3. mysql: Login
#修改密码(rhel8中)
UPDATE mysql.user SET authentication_string=password('新密码') WHERE User='root';
#修改密码(rhel7中)
UPDATE mysql.user Password=password('新密码') WHERE User='root';
#########
password()是为了防止密码裸漏,进行加密处理
  1. ps aux | grep mysql: Query the mysql process
  2. kill -9 进程号: Close the mysql process
  3. systemctl enable --now mariadb: Turn on service

Insert picture description here
Insert picture description here

7. User authorization

7.1 Create a local user

  1. CREATE USER 用户名@localhost identified by ‘该用户密码’;: Only log in with lcalhost
  2. SELECT User FROM mysql.user;: View users

Insert picture description here
Insert picture description here

7.2 Manage user permissions

  1. SHOW GRANTS FOR 用户名@localhost;: View user permissions
  2. GRANT 权限1,权限2 ON 数据库名.* TO 用户名@localhost;: Grant user permissions (* represents all tables under the library)
  3. REVOKE 权限1,权限2 ON 数据库名.* FROM 用户名@localhost;: Withdraw user rights

Insert picture description here

7.3 Delete user

DROP USER 用户名@localhost;:delete users

Insert picture description here

7.4 Create a network user (not recommended)

  1. CREATE USER 用户名@’%’ identified by ‘该用户密码’;:Create user (can log in via network or localhost)
#直接执行创建命令
mysql -uroot -p -e "CREATE USER 用户名@'%' identified by '该用户密码';"
  1. vim /etc/my.cnf.d/mariadb-server.cnf: Modify the main configuration file (open the database open port)
#skip-networking=1	注释掉(打开端口)
  1. systemctl restart mariadb.service: Restart service
  2. netstat -antlupe | grep mysql:Query database port
  3. mysql -u用户名 -p密码 -h访问的数据库主机IP: Remote or local access to the database
  • Create network user zynet in node1

Insert picture description here
Insert picture description here

  • Access node1 database in node2

Insert picture description here

8. Database backup

8.1 Database backup method

  1. mysqldump -u用户名 -p --all-databases: Back up all data
  2. mysqldump -u用户名t -p --all-databases --no-data: Back up all data, but do not back up data content (only database name tables and fields)
  3. mysqldump -u用户名t -p 数据库名 > /mnt/数据库名.sql:Back up data to the specified location

Insert picture description here
Insert picture description here
Insert picture description here

8.2 Data recovery method 1

  1. mysql -u root -p -e “CREATE DATABASE westos;”: Create a database
  2. mysql -u root -p westos < /mnt/westos.sql: Restore data to the specified database

Insert picture description here

8.2 Data recovery method 2

  1. vim /mnt/westos.sql: Modify the backup database file (add create and enter the operation database statement)
CREATE DATABASE westos;
USE westos;
  1. mysql -u root -p < /mnt/westos.sql:Data recovery

Insert picture description here
Insert picture description here

9. Phpmyadmin graphical database management method web

phpMyAdmin is a MySQL database management tool based on PHP and web-base architecture on the website host, allowing administrators to manage MySQL databases with a web interface. With this Web interface, it can be a better way to enter complex SQL syntax in a simple way, especially for the import and export of large amounts of data. One of the greater advantages is that phpMyAdmin runs on the web server like other PHP programs, but you can use the HTML pages generated by these programs anywhere, that is, remotely manage the MySQL database for easy creation, modification, and deletion Database and information table. You can also use phpMyAdmin to create common php grammars to facilitate the correctness of sql grammar needed when writing web pages.

9.1. Installation

  1. dnf install httpd -y: Install Apache
  2. dnf install php -y: Install php
  3. dnf install php-mysqlnd.x86_64 -y: Install php built-in mysql connection driver
  4. Download phpMyAdmin to the default publishing directory /var/www/html/

Insert picture description here

9.2 Configuration

  1. tar jxf /var/www/html/phpMyAdmin-3.4.0-all-languages.tar.bz2:unzip
  2. cd /var/www/html/: Enter the directory
    mv phpMyAdmin-3.4.0-all-languages myadmin: rename
  3. cd myadmin/: Enter the directory
    cp config.sample.inc.php config.inc.php: copy the template file
  4. systemctl enable --now httpd: Open httpd service
    systemctl restart httpd: restart httpd service

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

9.3 Access

firefox accesshttp://192.168.43.101/myadmin/

Insert picture description here
Insert picture description here
Insert picture description here

10. Reinstall the local database

  1. systemctl stop mariadb: Turn off the database service
  2. rm -fr /var/lib/mysql/: Delete data directory
  3. dnf reinstall mariadb-server.x86_64 -y: Reinstall the database

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_46069582/article/details/110295061