分享

Ubuntu: Install and configure a MySQL server

 frank_D_ 2023-04-13 发布于山东

安装

sudo apt install mysql-server

sudo service mysql status

应该提供如下输出:

● mysql.service - MySQL 社区服务器

   已加载:已加载(/lib/systemd/system/mysql.service;已启用;供应商预设:已启用)

  活跃:活跃(运行)自周二 2019-10-08 14:37:38 PDT;2 周 5 天前

  主 PID:2028(mysqld)

  任务:28(限制:4915)

  C组:/system.slice/mysql.service

└─2028 /usr/sbin/mysqld --daemonize --pid-file=/run/mysqld/mysqld.pid

10 月 8 日 14:37:36 db.example.org systemd[1]:启动 MySQL 社区服务器...

10 月 8 日 14:37:38 db.example.org systemd[1]:启动了 MySQL 社区服务器。


也可以通过在终端提示符下运行 ss 命令来检查 MySQL 服务的网络状态:


sudo ss -tap | grep mysql

听 0 70 127.0.0.1:33060 0.0.0.0:* 用户:(("mysqld",pid=161488,fd=21))

听 0 151 127.0.0.1:mysql 0.0.0.0:* 用户:(("mysqld",pid=161488,fd=23))


如果服务器没有正常运行,您可以键入以下命令来启动它:

sudo service mysql restart


解决问题的一个很好的起点是 systemd 日志,可以使用以下命令从终端提示符访问它:

sudo journalctl -u mysql


配置

您可以编辑 /etc/mysql/ 中的文件以配置基本设置 - 日志文件、端口号等。例如,要配置 MySQL 以侦听来自网络主机的连接,在文件 /etc/mysql/mysql.conf 中.d/mysqld.cnf,修改bind-address指令为服务器的IP地址:

绑定地址 = 192.168.0.5

笔记:

将 192.168.0.5 替换为适当的地址,该地址可以通过 ip address show 命令确定。

更改配置后,需要使用以下命令重新启动 MySQL 守护进程:

sudo systemctl 重新启动 mysql.service


数据库引擎

MySQL 旨在允许以不同方式存储数据。这些方法被称为数据库或存储引擎。您会感兴趣的主要存储引擎有两个:InnoDB 和 MyISAM。存储引擎对最终用户是透明的。MySQL 在表面下会以不同的方式处理事情,但无论使用哪种存储引擎,您都将以相同的方式与数据库交互。


每个引擎都有自己的优点和缺点。


While it is possible (and may be advantageous) to mix and match database engines on a table level, doing so reduces the effectiveness of the performance tuning you can do as you’ll be splitting the resources between two engines instead of dedicating them to one.


MyISAM 是两者中较老的一个。在某些情况下,它可以比 InnoDB 更快,并且有利于只读工作负载。一些 web 应用程序已经围绕 MyISAM 进行了调整(尽管这并不意味着它们在 InnoDB 下会变慢)。MyISAM 还支持 FULLTEXT 数据类型,它允许非常快速地搜索大量文本数据。然而 MyISAM 只能锁定整个表以进行写入。这意味着一次只有一个进程可以更新一张表。由于任何使用表格缩放的应用程序,这可能被证明是一个障碍。它还缺少日志功能,这使得崩溃后数据更难恢复。以下链接提供了有关在生产数据库上使用 MyISAM 的一些注意事项。


InnoDB 是一种更现代的数据库引擎,设计为符合 ACID 标准,可确保可靠地处理数据库事务。写锁定可以在表中的行级基础上发生。这意味着多个更新可以同时发生在一个表上。数据缓存也在数据库引擎的内存中处理,允许在更有效的行级基础上而不是文件块上进行缓存。为了满足 ACID 合规性,所有事务都独立于主表记录。由于可以检查数据一致性,因此可以实现更可靠的数据恢复。


从 MySQL 5.5 开始,InnoDB 是默认引擎,强烈推荐使用 MyISAM,除非您对该引擎的独特功能有特定需求。


高级配置

创建调优配置

在 MySQL 的配置文件中有许多参数可以调整。随着时间的推移,这将允许您提高服务器的性能。


许多参数可以根据现有数据库进行调整,但有些参数可能会影响数据布局,因此在应用时需要更加小心。


首先,如果您有现有数据,您首先需要执行 mysqldump 并重新加载:


mysqldump --all-databases --routines -u root -p > ~/fulldump.sql

这将在创建数据副本之前提示您输入 root 密码。建议确保在发生这种情况时没有其他用户或进程使用数据库。根据数据库中的数据量,这可能需要一段时间。在此过程中,您不会在屏幕上看到任何内容。


转储完成后,关闭 MySQL:


sudo service mysql stop

备份原始配置也是一个好主意:


sudo rsync -avz /etc/mysql /root/mysql-backup

接下来,进行任何所需的配置更改。然后,删除并重新初始化在重新启动 MySQL 之前确定数据库空间并确保所有权正确:


sudo rm -rf /var/lib/mysql/*

sudo mysqld --initialize

sudo chown -R mysql: /var/lib/mysql

sudo service mysql start

最后一步是通过将 SQL 命令传输到数据库来重新导入数据。


cat ~/fulldump.sql | database

对于大型数据导入,“管道查看器”实用程序可用于跟踪导入进度。忽略 pv 产生的任何 ETA 时间;它们基于处理文件每一行所花费的平均时间,但是使用 mysqldumps 插入的速度可能因行而异:


sudo apt install  pv

pv ~/fulldump.sql | database

Once this step is complete, you are good to go!


Note:

This is not necessary for all my.cnf changes. Most of the variables you can change to improve performance are adjustable even whilst the server is running. As with anything, make sure to have a good backup copy of your config files and data before making changes.


MySQL Tuner

MySQL Tuner is a Perl script that connects to a running MySQL instance and offers configuration suggestions for optimising the database for your workload. The longer the server has been running, the better the advice mysqltuner can provide. In a production environment, consider waiting for at least 24 hours before running the tool. You can install mysqltuner from the Ubuntu repositories:


sudo apt install mysqltuner

Then once it has been installed, simply run: mysqltuner – and wait for its final report.


The top section provides general information about the database server, and the bottom section provides tuning suggestions to alter in your my.cnf. Most of these can be altered live on the server without restarting; look through the official MySQL documentation (link in Resources section at the bottom of this page) for the relevant variables to change in production. The following example is part of a report from a production database showing potential benefits from increasing the query cache:


-------- Recommendations -----------------------------------------------------

General recommendations:

Run OPTIMIZE TABLE to defragment tables for better performance

Increase table_cache gradually to avoid file descriptor limits

Variables to adjust:

key_buffer_size (> 1.4G)

query_cache_size (> 32M)

table_cache (> 64)

innodb_buffer_pool_size (>= 22G)

It goes without saying that performance optimisation strategies vary from application to application. So for example, what works best for WordPress might not be the best for Drupal or Joomla. Performance can be dependent on the types of queries, use of indexes, how efficient the database design is and so on.


You may find it useful to spend some time searching for database tuning tips based on what applications you’re using. Once you’ve reached the point of diminishing returns from database configuration adjustments, look to the application itself for improvements, or invest in more powerful hardware and/or scaling up the database environment.

1. login with root

2. #mysql -u root -p

3. mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'newPassword';

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多