草庐IT

Ubuntu 22.04安装、配置和删除MySQL 8

Houor 2023-10-25 原文

1. 更新系统

在开始安装前,先更新一下系统。命令如下:

sudo apt update
sudo apt upgrade

2. 使用APT自动安装MySQL8

使用APT方式安装MySQL8时,通常会安装MySQL的最新版本,且能够自动配置服务和环境变量。

sudo apt install mysql-server

运行命令后,在询问是否安装时选择“Y”。
安装完成后,MySQL会自动启动,可以使用以下命令测试MySQL安装情况:

houor@IIP03:~$ systemctl status mysql
● mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2022-09-03 12:14:00 CST; 28s ago
    Process: 5862 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
   Main PID: 5870 (mysqld)
     Status: "Server is operational"
      Tasks: 38 (limit: 18988)
     Memory: 358.3M
        CPU: 685ms
     CGroup: /system.slice/mysql.service
             └─5870 /usr/sbin/mysqld

9月 03 12:14:00 IIP03 systemd[1]: Starting MySQL Community Server...
9月 03 12:14:00 IIP03 systemd[1]: Started MySQL Community Server.

可以确认MySQL已经安装成功。

3. 设置MySQL安全选项

使用MySQL安全配置向导mysql_secure_installation配置MySQL安全选项。其中的设置如下:

houor@IIP03:~$ sudo mysql_secure_installation

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

# 为root用户设置密码
VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?

Press y|Y for Yes, any other key for No: y

# 可以设置三种密码验证策略
There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2
Please set the password for root here.

# 输入密码
New password: 
Re-enter new password: 

Estimated strength of the password: 100 
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
# 是否删除匿名用户
# 生产环境中一般要删除匿名用户
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL 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? (Press y|Y for Yes, any other key for No) : y
Success.

# 是否运行root用户远程登录
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? (Press y|Y for Yes, any other key for No) : 

 ... skipping.
By default, MySQL 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.

# 是否删除test数据库
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : 

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

# 开始刷新授权表,使设置生效
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done! 

4. 迁移MySQL数据文件到指定位置

  1. 关闭MySQL服务
systemctl stop mysql
  1. 创建data文件夹并复制文件
sudo mkdir /data

注意:mysql用户应有data文件夹的读写权限。
创建data文件夹后,将/var/lib/mysql文件夹复制到/data下。为确保文件完整复制,使用rsync复制并检查文件完整性。其中:参数-a表示修改时间/链接等元信息一同复制。如下所示:

sudo rsync -a /var/lib/mysql /data/
  1. 修改配置文件(/etc/mysql/mysql.conf.d/mysqld.cnf)中数据文件信息
    在MySQL8中,配置文件是/etc/mysql/mysql.conf.d/mysqld.cnf。使用vim或nano打开该配置文件,将datadir设置为修改后的数据文件位置。
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

修改属性datadir到指定位置:

datadir = /data/mysql
  1. 修改服务配置文件
    MySQL的服务配置文件位于/etc/apparmor.d/usr.sbin.mysqld。
    打开配置文件:
sudo nano /etc/apparmor.d/usr.sbin.mysqld

打开文件后,修改以下属性:

# Allow data dir access
  /data/mysql/ r,
  /data/mysql/** rwk,

修改控制文件:

sudo nano /etc/apparmor.d/abstractions/mysql

修改访问控制如下:

/data/mysql{,d}/mysql{,d}.sock rw,

重新启动apparmor:

systemctl restart apparmor

然后启动MySQL:

systemctl start mysql

MySQL数据目录修改成功。

5. 配置远程root用户访问

  1. 修改或添加root用户的远程连接Host
    执行以下SQL语句,添加延迟访问权限:
mysql> use mysql;
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your-pass-word';
mysql> update mysql.user set host='%' where user='root';
mysql> flush privileges;
  1. 开启访问权限
    修改配置文件,取消IP限制:
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf 

修改bind-address属性,或者直接注释掉该属性:

bind-address            = * 

6. 卸载MySQL

  1. 关闭MySQL服务
systemctl stop mysql
  1. 卸载相关的依赖
sudo apt remove --purge mysql-*
sudo apt autoremove

在删除过程中,根据提示确认即可。
3. 清理残余文件
查询是否还存在相关的依赖组件:

dpkg --list | grep mysql

如果还存在一些依赖,则继续用“apt remove 依赖包名称”命令删除;确认删除完整后,清理残余文件:

dpkg -l |grep ^rc|awk '{print $2}' |sudo xargs dpkg -P
sudo rm -rf /etc/mysql
sudo rm -rf /var/lib/mysql

有关Ubuntu 22.04安装、配置和删除MySQL 8的更多相关文章

  1. ruby - 在 64 位 Snow Leopard 上使用 rvm、postgres 9.0、ruby 1.9.2-p136 安装 pg gem 时出现问题 - 2

    我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po

  2. ruby-on-rails - 如何从 format.xml 中删除 <hash></hash> - 2

    我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为

  3. ruby - 我可以使用 Ruby 从 CSV 中删除列吗? - 2

    查看Ruby的CSV库的文档,我非常确定这是可能且简单的。我只需要使用Ruby删除CSV文件的前三列,但我没有成功运行它。 最佳答案 csv_table=CSV.read(file_path_in,:headers=>true)csv_table.delete("header_name")csv_table.to_csv#=>ThenewCSVinstringformat检查CSV::Table文档:http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV/Table.html

  4. ruby - 完全离线安装RVM - 2

    我打算为ruby​​脚本创建一个安装程序,但我希望能够确保机器安装了RVM。有没有一种方法可以完全离线安装RVM并且不引人注目(通过不引人注目,就像创建一个可以做所有事情的脚本而不是要求用户向他们的bash_profile或bashrc添加一些东西)我不是要脚本本身,只是一个关于如何走这条路的快速指针(如果可能的话)。我们还研究了这个很有帮助的问题:RVM-isthereawayforsimpleofflineinstall?但有点误导,因为答案只向我们展示了如何离线在RVM中安装ruby。我们需要能够离线安装RVM本身,并查看脚本https://raw.github.com/wayn

  5. ruby-on-rails - rails 目前在重启后没有安装 - 2

    我有一个奇怪的问题:我在rvm上安装了ruby​​onrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(

  6. ruby - 如何为 emacs 安装 ruby​​-mode - 2

    我刚刚为fedora安装了emacs。我想用emacs编写ruby。为ruby​​提供代码提示、代码完成类型功能所需的工具、扩展是什么? 最佳答案 ruby-mode已经包含在Emacs23之后的版本中。不过,它也可以通过ELPA获得。您可能感兴趣的其他一些事情是集成RVM、feature-mode(Cucumber)、rspec-mode、ruby-electric、inf-ruby、rinari(用于Rails)等。这是我当前用于Ruby开发的Emacs配置:https://github.com/citizen428/emacs

  7. ruby-on-rails - 无法在centos上安装therubyracer(V8和GCC出错) - 2

    我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e

  8. ruby-on-rails - 独立 ruby​​ 脚本的配置文件 - 2

    我有一个在Linux服务器上运行的ruby​​脚本。它不使用rails或任何东西。它基本上是一个命令行ruby​​脚本,可以像这样传递参数:./ruby_script.rbarg1arg2如何将参数抽象到配置文件(例如yaml文件或其他文件)中?您能否举例说明如何做到这一点?提前谢谢你。 最佳答案 首先,您可以运行一个写入YAML配置文件的独立脚本:require"yaml"File.write("path_to_yaml_file",[arg1,arg2].to_yaml)然后,在您的应用中阅读它:require"yaml"arg

  9. ruby - 通过 RVM (OSX Mountain Lion) 安装 Ruby 2.0.0-p247 时遇到问题 - 2

    我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search

  10. ruby - 我可以使用 aws-sdk-ruby 在 AWS S3 上使用事务性文件删除/上传吗? - 2

    我发现ActiveRecord::Base.transaction在复杂方法中非常有效。我想知道是否可以在如下事务中从AWSS3上传/删除文件:S3Object.transactiondo#writeintofiles#raiseanexceptionend引发异常后,每个操作都应在S3上回滚。S3Object这可能吗?? 最佳答案 虽然S3API具有批量删除功能,但它不支持事务,因为每个删除操作都可以独立于其他操作成功/失败。该API不提供任何批量上传功能(通过PUT或POST),因此每个上传操作都是通过一个独立的API调用完成的

随机推荐