Ubuntu 22.04安装、配置和删除MySQL 8
1. 更新系统
在开始安装前,先更新一下系统。命令如下:
sudo apt updatesudo 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/mysqld9月 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_installationSecuring the MySQL server deployment.Connecting to MySQL using a blank password.# 为root用户设置密码VALIDATE PASSWORD COMPONENT can be used to test passwordsand improve security. It checks the strength of passwordand allows the users to set only those passwords which aresecure 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 >= 8MEDIUM Length >= 8, numeric, mixed case, and special charactersSTRONG Length >= 8, numeric, mixed case, special characters and dictionary filePlease enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2Please 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 havea user account created for them. This is intended only fortesting, and to make the installation go a bit smoother.You should remove them before moving into a productionenvironment.Remove anonymous users? (Press y|Y for Yes, any other key for No) : ySuccess.# 是否运行root用户远程登录Normally, root should only be allowed to connect from'localhost'. This ensures that someone cannot guess atthe 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' thatanyone can access. This is also intended only for testing,and should be removed before moving into a productionenvironment.# 是否删除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 changesmade so far will take effect immediately.# 开始刷新授权表,使设置生效Reload privilege tables now? (Press y|Y for Yes, any other key for No) : ySuccess.All done!
4. 迁移MySQL数据文件到指定位置
- 关闭MySQL服务
systemctl stop mysql
- 创建data文件夹并复制文件
sudo mkdir /data
注意:mysql用户应有data文件夹的读写权限。
创建data文件夹后,将/var/lib/mysql文件夹复制到/data下。为确保文件完整复制,使用rsync复制并检查文件完整性。其中:参数-a表示修改时间/链接等元信息一同复制。如下所示:
sudo rsync -a /var/lib/mysql /data/
- 修改配置文件(/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
- 修改服务配置文件
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用户访问
- 修改或添加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;
- 开启访问权限
修改配置文件,取消IP限制:
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
修改bind-address属性,或者直接注释掉该属性:
bind-address = *
6. 卸载MySQL
- 关闭MySQL服务
systemctl stop mysql
- 卸载相关的依赖
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 -Psudo rm -rf /etc/mysqlsudo rm -rf /var/lib/mysql
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341