Preface
This document is suitable for deploying mysql5.7 under Ubuntu20.04 system when there is an Internet connection.
It provides two methods of installing mysql5.7, and also explains how to enable mysql remote access permissions to allow remote connections; and how to modify the storage path of database data.
1. Preparatory work before installation
1. Confirm whether MySQL exists on the current server
rpm -qa | grep mysql
As shown in the figure, it prompts that there is no rpm command. We need to install rpm. The command: apt install rpm
Note: If empty information is returned, it means that MySQL is not installed in the current environment; jump directly to step 4 for further operations.
2. View the location of MySQL-related configuration files, source code and help documents
whereis mysql
ps: If there is, it will return the path related to mysql
3. Find the corresponding directory through the above and delete the corresponding mysql related files.
rpm -e --nodeps mysql-xxxx
-- nodeps parameter, skip dependency check
Note: If mysql is not installed in your current environment, please ignore steps 2 and 3.
4. Check whether the current environment has its own mariadb database
rpm -qa | grep mariadb
Note: If empty information is returned, it means that mariadb is not installed in the current environment; skip to the dpkg step to install it.
5. Recommended to uninstall
rpm -e --nodeps mariadb-libs
2. Installation through dpkg -i (Method 1) Recommended
1. Download the deb file source package of mysql5.7
cd /usr/local
mkdir mysql
cd mysql
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-server_5.7.35-1ubuntu18.04_amd64.deb-bundle.tar
ps: The default is to download to the current directory.
2. Unzip the source code package
tar -xvf mysql-server_5.7.35-1ubuntu18.04_amd64.deb-bundle.tar
Unzipped related packages
3. Delete the package whose name is related to test, which is useless.
rm -f mysql-community-test_5.7.35-1ubuntu18.04_amd64.deb
rm -f mysql-testsuite_5.7.35-1ubuntu18.04_amd64.deb
4. Install using dpkg command
dpkg -i mysql-*.deb
Description: dpkg -i is the dpkg command option for installing .deb files.
(Reading database...Currently installed are 78536 files and directories.) Preparing to decompress mysql-client_5.7.35-1ubuntu18.04_amd64.deb. . . Unpacking mysql client (5.7.35-1ubuntu18.04). . . Select the previously unselected package mysql-common. Preparing to decompress mysql-common_5.735-1ubuntu18.04_amd64.deb. . . Unpacking mysql common (5.7.35-1ubuntu18.04). . . Select the previously unselected package mysql community client. Preparing to decompress mysql-community-client5.7.35-1ubuntu18.04_amd64.deb. . . Unpacking mysql community client (5.7.35-1ubuntu18.04). . . Select the previously unselected package mysql community server. Preparing to decompress mysql-community-server_5.7.35-1ubuntu18.04_amd64.deb. . . Unpacking mysql community server (5.7.35-1ubuntu18.04). . . Select the previously unselected package mysql-community-source. Preparing to decompress mysql-community-source_5.7.35-1ubuntu18.04_amd64.deb. . . Unpacking mysql community source code (5.7.35-1ubuntu18.04). . . Select the previously unselected package mysql-server. Preparing to decompress mysql-server_5.7.35-1ubuntu18.04_amd64.deb. . . Unpacking mysql server (5.7.35-1ubuntu18.04). . Setting up mysql-common (5.7.35-1ubuntu18.04). . . Update alternative: Use /etc/mysql/my.cnf.fallback to serve /etc/mysql/my.cnf (my.cnf) in automatic mode dpkg: Dependency issues hinder configuration of mysql community client: The mysql community client relies on libtinfo5 (>=6); however: Package libtinfo5 is not installed. dpkg: error handling package mysql community client (--install): Dependency issues - remain unconfigured dpkg: Dependency issues hinder configuration of mysql community server: mysql community server depends on libmecab2; however: Package libmecab2 is not installed. dpkg: Error processing mysql community server package (--install): Dependency issues - remain unconfigured Setting up mysql community source code (5.7.35-1ubuntu18.04). . . dpkg: Dependency issues block configuration of mysql server: mysql server depends on mysql community server (=5.7.35-1ubuntu18.04); however: Package mysql community server has not been configured yet. dpkg: Error while processing package mysql-server (--install): Dependency issues - remain unconfigured dpkg: Dependency issues hinder configuration of mysql client: The mysql client depends on the mysql community client (=5.7.35-1ubuntu18.04); however: Package mysql community client has not been configured yet. dpkg: Error while processing package mysql client (--install): Dependency issues - remain unconfigured Processing triggers for man database (2.9.1-1). . . Processing triggers for systemd (245.4-4ubuntu3.22). . . An error was encountered while processing: mysql community client mysql community server mysql server mysql client |
The above means that the libtinfo5 and libmecab2 packages are missing, we install them directly at one time
apt install libtinfo5 libmecab2
After running, the following will appear: Set root user password
Confirm Password
5. Check version
mysql -V 或 /usr/bin/mysql -V
6. Check the status of mysql
systemctl status mysql
7. Start mysql
systemctl start mysql
8. Stop the mysql service
systemctl stop mysql
9. Restart the mysql service
systemctl restart mysql
10.Auto start
systemctl enable mysql
3. Install by compressing the package (method 2)
1. Download the mysql installation package you want through the command
cd /usr/local/
wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz
2. 解压
tar -zxvf mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz
3.修改解压后的名称:mysql
mv mysql-5.7.35-linux-glibc2.12-x86_64 mysql
4.创建存放数据的目录
mkdir -p /u01/mysql/data
cd /u01/mysql/data
5.给创建的数据目录赋予权限
chmod -R 777 /u01/mysql/data
6.创建用户用户组,并将创建的用户添加到用户组中
groupadd mysql 创建用户组
useradd -g mysql mysql 创建用户mysql(mysql)并将用户添加到用户组(mysql)中
7.修改mysql配置文件
vim /etc/my.cnf
[mysqld] bind-address=0.0.0.0 port=3306 user=mysql basedir=/usr/local/mysql datadir=/u01/mysql/data socket=/tmp/mysql.sock log-error=/u01/mysql/data/mysql.err pid-file=/u01/mysql/data/mysql.pid #character config character_set_server=utf8mb4 symbolic-links=0 explicit_defaults_for_timestamp=true |
8.安装并初始化mysql
(1)进入mysql安装目录的bin目录
cd bin
(2)执行如下命令
./mysqld --initialize --user=mysql --datadir=/u01/mysql/data/ --basedir=/usr/local/mysql/
9.查看mysql密码
cat /u01/mysql/data/mysql.err
10.修改配置如下所示:
打开文件:
vim /usr/local/mysql/support-files/mysql.server
11.添加软连接
ln -s /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
12.启动mysql
如果使用 service mysql start 出现如下所示,就使用后面的命令启动mysql
上述命令不行,可以尝试命令: sudo /etc/init.d/mysql start
四、开启mysql远程访问权限,允许远程连接
1.登录mysql
mysql -u root -p
2.选择mysql数据库
use mysql
3.修改配置
update user set host='%' where user='root';
4.刷新权限,使其生效
flush privileges;
5.退出
exit
五、修改数据存储路径
1.查看当前mysql的数据存放路径
(1)登录mysql
(2)看目前mysql的数据存放路径
show variables like '%datadir%';
默认的数据存放位置
2.在/etc/mysql/mysql.conf.d目录下,修改mysqld.cnf文件
vim /etc/mysql/mysql.conf.d/mysqld.cnf
如下:
3.编辑usr.sbin.mysqld配置文件
vim /etc/apparmor.d/usr.sbin.mysqld
修改成如下配置
在最后添加两行命令,是为了AppArmor配置以允许MySQL访问所需的文件和目录
4.配置权限
sudo chmod -R 775 /u01/mysql/data 配置文件夹的权限
sudo chown -R mysql:mysql /u01/mysql/data
5.重启服务
service apparmor reload
service apparmor restart
service mysql start
6.再验证一下是否修改成功-重新执行步骤1
遇到问题
问题一:
AppArmor parser error for /etc/apparmor.d/usr.lib.snapd.snap-confine.real in /etc/apparmor.d/usr.lib.snapd.snap-confine.real at line 11: Could not open '/var/lib/snapd/apparmor/snap-confine' Skipping profile in /etc/apparmor.d/disable: usr.sbin.rsyslogd AppArmor parser error for /etc/apparmor.d/usr.sbin.mysqld in /etc/apparmor.d/usr.sbin.mysqld at line 7: Could not open 'abstractions/mysql' |
解决方法:安装snap
apt intasll snap
问题二:
--initialize specified but the data directory has files in it. Aborting.
意思是初始化数据目录时发现目录里面有文件,可能原因是之前运行过命令目录中已生成文
件。
解决方法:只要我们找到MySQL的数据目录并删除里面的文件即可解决。
找到mysql存放的路径上面的第2步就可以看见,原位置是/var/lib/mysql,切换到这个目录下,删除所有文件命令 rm -rf * 再看一下新位置(/u01/mysql/data/)是否存在文件有的话也删除。全部删除完成之后,重新执行mysql启动命令:systemctl status mysql
注:只适用于新安装的数据库,不适用之前安装好的修改存储路径,后者需要先备份数据。
六、MySQL实用操作命令
1.连接mysql命令
mysql -u root -p
enter password
2. Display all databases
show databases;
3.Switch database
use database;
4. Display the table information of a database
show tables;
5.Create database
drop database 数据库名;
6. Delete database
drop database 数据库名;
7.Exit
exit
7. Uninstall mysql
1. Stop the mysql server
systemctl stop mysql
2. Delete the mysql package
sudo apt-get remove --purge mysql-server mysql-client mysql-common
3. Check whether there are mysql related files
whereis mysql
4. If it exists, delete the above directories one by one.
5. Clean up residual dependencies
sudo apt-get autoremove
6. List the "mysql" related software packages installed on the system
sudo dpkg -l | grep mysql
7. After finding it, delete the mysql related software package through the command
sudo apt-get remove --purge mysql相关软件包