1+X 云计算运维与开发(中级)案例实战——msyql服务器运维与优化

本文详述了如何配置本地yum源,安装并启动MySQL服务,创建与管理数据库,授权root用户,以及通过编辑my.cnf文件进行数据库优化。实践步骤包括主机名修改、数据库的创建、删除、导入导出,以及权限设置和性能调优。
摘要由CSDN通过智能技术生成


前言

学而不思则罔,思而不学则殆。


思路

1.配置yum源
2.安装mysql
3.启动mysql,初始化数据库
4.使用命令在shell界面创建数据库,删除数据库,导入和导出数据库
5.授予root用户权限
6.编辑 my.cnf文件,优化数据库


实操

1.修改主机名,并配置本地yum源

示例代码如下:

[root@localhost ~]# hostnamectl set-hostname mysql
[root@localhost ~]# logout
[root@mysql ~]# rm -rf /etc/yum.repos.d/*
[root@mysql ~]# cat > /etc/yum.repos.d/local.repo << eof 
> [mysql]
> name=mysql
> baseurl=file:///root/gpmall-single/gpmall-repo
> gpgcheck=0
> enabled=1
> eof
[root@mysql ~]# yum repolist
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
mysql                            | 2.9 kB     00:00     
mysql/primary_db                   | 158 kB   00:00     
repo id                   repo name               status
mysql                     mysql                   191
repolist: 191

/root/gpmall-single/gpmall-repo:这个文件需要自己准备

2.安装mysql服务,启动并且初始化

示例代码如下:

[root@mysql ~]# yum -y install mariadb mariadb-server
[root@mysql ~]# systemctl start mariadb
[root@mysql ~]# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n]   #输入y
New password: #密码123456
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] #输入y或者直接回车
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] #输入n
 ... skipping.

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] #输入y或者直接回车
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] #输入y或者直接回车
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

3.使用命令在shell界面创建数据库,删除数据库,导入和导出数据库

示例代码如下:

3.1 使用命令创建test数据库

[root@mysql ~]# mysqladmin -uroot -p123456 create test
[root@mysql ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.68-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)
3.2 使用命令删除**test**数据库
[root@mysql ~]# mysqladmin -uroot -p123456 drop test
Dropping the database is potentially a very bad thing to do.
Any data stored in the database will be destroyed.

Do you really want to drop the 'test' database [y/N] y
Database "test" dropped
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

3.3 在mysql界面创建test数据库,在test数据库中创建tables数据表

MariaDB [(none)]> create database test;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> use test;
Database changed
MariaDB [test]> CREATE TABLE IF NOT EXISTS `tables`(
    -> `tables_id` INT UNSIGNED AUTO_INCREMENT,
    ->              `tables_title` VARCHAR(100) NOT NULL,
    ->              `tables_author` VARCHAR(40) NOT NULL,
    ->              `tables_date` DATE,
    ->              PRIMARY KEY ( `tables_id` )
    -> )ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.00 sec)

3.4 使用命令导出数据库和数据表

[root@mysql ~]# mysqldump -uroot -p123456 test > test.sql
[root@mysql ~]# mysqldump -uroot -p123456 test tables > test_tables.sql
[root@mysql ~]# ll
total 13016
-rw-r--r--  1 root root     1518 Jan 21  2021 aliases
-rw-r--r--  1 root root    12288 Jan 21  2021 aliases.db
-rw-------. 1 root root     3302 Jun  1  2018 anaconda-ks.cfg
drwxr-xr-x  3 root root     4096 Jan 26  2021 bind-9.9.4
-rw-r--r--  1 root root 13287936 Oct 14  2019 cirros-0.3.3-x86_64-disk.img
drwxr-xr-x  4 root root     4096 Jan 26  2021 gpmall-single
drwxr-xr-x  3 root root       42 Jan 26  2021 kvm_packages
drwxr-xr-x  4 root root       35 Jan 26  2021 mdadm_yum
-rw-r--r--  1 root root     1943 Mar 20 10:02 test.sql
-rw-r--r--  1 root root     1943 Mar 20 10:03 test_tables.sql

3.5 使用命令导入数据库
方法一:

[root@mysql ~]# mysqladmin -uroot -p123456 drop test
Dropping the database is potentially a very bad thing to do.
Any data stored in the database will be destroyed.

Do you really want to drop the 'test' database [y/N] y
Database "test" dropped
[root@mysql ~]# mysqladmin -uroot -p123456 create test
[root@mysql ~]# mysqldump -uroot -p123456 test < test.sql
-- MySQL dump 10.14  Distrib 5.5.68-MariaDB, for Linux (x86_64)
--
-- Host: localhost    Database: test
-- ------------------------------------------------------
-- Server version       5.5.68-MariaDB

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_K
EY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZE
RO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2022-03-20 10:07:51

方法二:

[root@mysql ~]# mysqladmin -uroot -p123456 drop test
Dropping the database is potentially a very bad thing to do.
Any data stored in the database will be destroyed.

Do you really want to drop the 'test' database [y/N] y
Database "test" dropped
[root@mysql ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 19
Server version: 5.5.68-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> create database test;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> use test
Database changed
MariaDB [test]> source /root/test.sql;
Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

4.授予root用户权限

示例代码如下:

MariaDB [test]> grant all privileges on test.* to root@localhost identified by '123456';
Query OK, 0 rows affected (0.00 sec)

MariaDB [test]> grant all privileges on test.* to root@'%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

grant all privileges on:赋予所有权限
test.*:数据库 test里的所有数据表的权限,*为通配符
to root@localhost:本地计算机的root用户,当localhost变为 ‘%’,表示所有计算机的root用户
identified by:密码验证

5.编辑 my.cnf 文件,优化数据库

示例代码如下:

[root@mysql ~]# vi /etc/my.cnf
##在my.cnf文件中添加以下内容
[mysqld]
thread_concurrency = 64		#CPU核数 * 2
max_connections=1500			#最大连接(用户)数。每个连接MySQL的用户均算作一个连接
max_connect_errors=30			#最大失败连接限制
bulk_insert_buffer_size = 32M	#批量插入数据缓存大小
query_cache_type=1			#查询缓存  (0 = off、1 = on、2 = demand)
query_cache_size = 64M		#指定mysql查询缓冲区大小
max_allowed_packet = 128M	#通信缓冲大小
read_buffer_size = 8M			#顺序读取数据缓冲区使用内存
read_rnd_buffer_size = 32M		#随机读取数据缓冲区使用内存
命令解析
thread_concurrency并发线程数,建议为CPU核心数乘以2
max_connections最大连接(用户)数。每个连接MySQL的用户均算作一个连接
max_connect_errors最大失败连接限制
bulk_insert_buffer_size批量插入数据缓存大小,可以有效提高写入效率,默认为8 MB
query_cache_type控制着查询缓存功能的开启的关闭。0时表示关闭,1时表示打开,2表示只要select 中明确指定SQL_CACHE才缓存
query_cache_size指定MySQL查询缓冲区的大小,用来缓冲select的结果,并在下一次同样查询的时候不再执行查询而直接返回结果,根据Qcache_lowmem_prunes的大小,来查看当前的负载是否足够高,在数据库写入量或是更新量也比较大的系统,该参数不适合分配过大。而且在高并发,写入量大的系统,建议把该功能禁掉。属重点优化参数(主库增删改-MyISAM)
max_allowed_packet指定MySQL查询缓冲区的大小,用来缓冲select的结果,并在下一次同样查询的时候不再执行查询而直接返回结果,根据Qcache_lowmem_prunes的大小,来查看当前的负载是否足够高,在数据库写入量或是更新量也比较大的系统,该参数不适合分配过大。而且在高并发,写入量大的系统,建议把该功能禁掉。属重点优化参数(主库增删改-MyISAM)
read_buffer_size来做MYISAM表全表扫描的缓冲大小,对表进行顺序扫描的请求将分配一个读入缓冲区,MySQL会为它分配一段内存缓冲区。read_buffer_size变量控制这一缓冲区的大小。如果对表的顺序扫描请求非常频繁,并且用户认为频繁扫描进行得太慢,可以通过增加该变量值以及内存缓冲区大小提高其性能
read_rnd_buffer_size随机读(查询操作)缓冲区大小。当按任意顺序读取行时(例如,按照排序顺序),将分配一个随机读缓存区。进行排序查询时,MySQL会首先扫描一遍该缓冲,以避免磁盘搜索,提高查询速度,如果需要排序大量数据,可适当调高该值。但MySQL会为每个客户连接发放该缓冲空间,所以应尽量适当设置该值,以避免内存开销过大

总结

多读书,多看报,少吃零食,好好睡觉
努力就完啦!

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值