1. 更新系统 在开始安装前,先更新一下系统。命令如下: sudo apt updatesudo apt upgrade 2. 使用APT自动安装Mysql8 使用APT方式安装mysql8时,通常会安装Mysql的最新版本,且能
在开始安装前,先更新一下系统。命令如下:
sudo apt updatesudo apt upgrade
使用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已经安装成功。
使用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!
systemctl stop mysql
sudo mkdir /data
注意:mysql用户应有data文件夹的读写权限。
创建data文件夹后,将/var/lib/mysql文件夹复制到/data下。为确保文件完整复制,使用rsync复制并检查文件完整性。其中:参数-a表示修改时间/链接等元信息一同复制。如下所示:
sudo rsync -a /var/lib/mysql /data/
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
修改属性datadir到指定位置:
datadir = /data/mysql
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数据目录修改成功。
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;
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
修改bind-address属性,或者直接注释掉该属性:
bind-address = *
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
--结束END--
本文标题: Ubuntu 22.04安装、配置和删除MySQL 8
本文链接: https://lsjlt.com/news/375123.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-10-23
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0