下载
-
从 MySQL 官网 下载文件 mysql-server_5.7.37-1ubuntu18.04_amd64.deb-bundle.tar :
wget https://cdn.mysql.com/archives/mysql-5.7/mysql-server_5.7.37-1ubuntu18.04_amd64.deb-bundle.tar
-
将下载好的文件解压到相应的目录,这里是解压到当前目录
tar xvf mysql-server_5.7.37-1ubuntu18.04_amd64.deb-bundle.tar
-
解压后的文件如下:
➜ mysql-deb ll total 153M -rwxrwxrwx 1 root root 2.1M Nov 30 2021 libmysqlclient-dev_5.7.37-1ubuntu18.04_amd64.deb -rwxrwxrwx 1 root root 1.6M Nov 30 2021 libmysqlclient20_5.7.37-1ubuntu18.04_amd64.deb -rwxrwxrwx 1 root root 19M Nov 30 2021 libmysqld-dev_5.7.37-1ubuntu18.04_amd64.deb -rwxrwxrwx 1 root root 69K Nov 30 2021 mysql-client_5.7.37-1ubuntu18.04_amd64.deb -rwxrwxrwx 1 root root 72K Nov 30 2021 mysql-common_5.7.37-1ubuntu18.04_amd64.deb -rwxrwxrwx 1 root root 14M Nov 30 2021 mysql-community-client_5.7.37-1ubuntu18.04_amd64.deb -rwxrwxrwx 1 root root 35M Nov 30 2021 mysql-community-server_5.7.37-1ubuntu18.04_amd64.deb -rwxrwxrwx 1 root root 51M Nov 30 2021 mysql-community-source_5.7.37-1ubuntu18.04_amd64.deb -rwxrwxrwx 1 root root 32M Nov 30 2021 mysql-community-test_5.7.37-1ubuntu18.04_amd64.deb -rwxrwxrwx 1 root root 69K Nov 30 2021 mysql-server_5.7.37-1ubuntu18.04_amd64.deb -rwxrwxrwx 1 root root 69K Nov 30 2021 mysql-testsuite_5.7.37-1ubuntu18.04_amd64.deb
-
NOTE: 如果不需要安装测试相关的包,可以删除其中两个带 test 名称的 deb 文件:
- mysql-community-test_5.7.37-1ubuntu18.04_amd64.deb
- mysql-testsuite_5.7.37-1ubuntu18.04_amd64.deb
-
删除命令:
rm -f mysql-community-test_5.7.37-1ubuntu18.04_amd64.deb mysql-testsuite_5.7.37-1ubuntu18.04_amd64.deb
安装
-
进入目录,执行
dpkg -i mysql-*.deb
命令,开始安装 MySQL:dpkg -i mysql-*.deb
-
如果安装过程中,缺少相关依赖包,会导致安装失败。
-
如以下提示未安装依赖包 libtinfo5 ,libmecab2:
➜ mysql-deb dpkg -i mysql-*.deb Selecting previously unselected package mysql-client. (Reading database ... 49086 files and directories currently installed.) Preparing to unpack mysql-client_5.7.37-1ubuntu18.04_amd64.deb ... Unpacking mysql-client (5.7.37-1ubuntu18.04) ... Selecting previously unselected package mysql-common. Preparing to unpack mysql-common_5.7.37-1ubuntu18.04_amd64.deb ... Unpacking mysql-common (5.7.37-1ubuntu18.04) ... Selecting previously unselected package mysql-community-client. Preparing to unpack mysql-community-client_5.7.37-1ubuntu18.04_amd64.deb ... Unpacking mysql-community-client (5.7.37-1ubuntu18.04) ... Selecting previously unselected package mysql-community-server. Preparing to unpack mysql-community-server_5.7.37-1ubuntu18.04_amd64.deb ... Unpacking mysql-community-server (5.7.37-1ubuntu18.04) ... Selecting previously unselected package mysql-community-source. Preparing to unpack mysql-community-source_5.7.37-1ubuntu18.04_amd64.deb ... Unpacking mysql-community-source (5.7.37-1ubuntu18.04) ... Selecting previously unselected package mysql-server. Preparing to unpack mysql-server_5.7.37-1ubuntu18.04_amd64.deb ... Unpacking mysql-server (5.7.37-1ubuntu18.04) ... Setting up mysql-common (5.7.37-1ubuntu18.04) ... update-alternatives: using /etc/mysql/my.cnf.fallback to provide /etc/mysql/my.cnf (my.cnf) in auto mode dpkg: dependency problems prevent configuration of mysql-community-client: mysql-community-client depends on libtinfo5 (>= 6); however: **Package libtinfo5 is not installed.** dpkg: error processing package mysql-community-client (--install): dependency problems - leaving unconfigured dpkg: dependency problems prevent configuration of mysql-community-server: mysql-community-server depends on libmecab2; however: **Package libmecab2 is not installed.** dpkg: error processing package mysql-community-server (--install): dependency problems - leaving unconfigured Setting up mysql-community-source (5.7.37-1ubuntu18.04) ... dpkg: dependency problems prevent configuration of mysql-server: mysql-server depends on mysql-community-server (= 5.7.37-1ubuntu18.04); however: Package mysql-community-server is not configured yet. dpkg: error processing package mysql-server (--install): dependency problems - leaving unconfigured dpkg: dependency problems prevent configuration of mysql-client: mysql-client depends on mysql-community-client (= 5.7.37-1ubuntu18.04); however: Package mysql-community-client is not configured yet. dpkg: error processing package mysql-client (--install): dependency problems - leaving unconfigured Processing triggers for man-db (2.9.1-1) ... Processing triggers for systemd (245.4-4ubuntu3.15) ... Errors were encountered while processing: mysql-community-client mysql-community-server mysql-server mysql-client
-
解决办法是通过命令
apt install
把相关缺失的包装上即可,如:apt install libtinfo5 libmecab2
- 之后再重新执行
dpkg -i mysql-*.deb
命令,安装过程中,会提示让你输入 root 用户密码。
- 之后再重新执行
启动 MySQL
service mysql start
停止mysql
service mysql stop
重启mysql
service mysql restart
访问mysql
mysql -u root -p
更改mysql默认端口
-
vim /etc/mysql/mysql.conf.d/mysqld.cnf
- 添加一行
port = 33060
-
查看开放的端口:
show variables like 'port';
允许远程访问
-
vim /etc/mysql/mysql.conf.d/mysqld.cnf
- 修改bind-address 为 0.0.0.0
-
向user添加授权
#登录数据库 mysql -u root -p #输入密码 mysql> use mysql; #查询host mysql> select user,host from user; #创建host #如果没有"%"这个host值,就执行下面这两句: mysql> update user set host='%' where user='root'; mysql> flush privileges; #授权用户 #任意主机以用户root和密码mypwd连接到mysql服务器 mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '密码' WITH GRANT OPTION; mysql> flush privileges;
定时备份数据
-
#!/bin/bash #NAME:数据库备份 #DATE:*/*/* #USER:*** #设置本机数据库登录信息 mysql_user="user" mysql_password="passwd" mysql_host="localhost" mysql_port="3306" mysql_charset="utf8mb4" date_time=
date +%Y-%m-%d-%H-%M
#保存目录中的文件个数 count=10 #备份路径 path=/***/ #备份数据库sql文件并指定目录 mysqldump --all-databases --single-transaction --flush-logs --master-data=2 -h$mysql_host -u$mysql_user -p$mysql_password > $path_$(date +%Y%m%d_%H:%M).sql [ $? -eq 0 ] && echo "-----------------数据备份成功_$date_time-----------------" || echo "-----------------数据备份失败-----------------" #找出需要删除的备份 delfile=ls -l -crt $path/*.sql | awk '{print $9 }' | head -1
#判断现在的备份数量是否大于阈值 number=ls -l -crt $path/*.sql | awk '{print $9 }' | wc -l
if [ $number -gt $count ] then rm $delfile #删除最早生成的备份,只保留count数量的备份 #更新删除文件日志 echo "-----------------已删除过去备份sql $delfile-----------------" fi -
如果提示一下错误:
-
mysqldump: [Warning] Using a password on the command line interface can be insecure. mysqldump: Error: Binlogging on server not active -----------------数据备份失败-----------------
-
解决:在
/etc/mysql/conf.d
路径下编辑mysql.cnf
-
新增配置
[mysqld] log-bin=mysql-bin # 指定二进制日志文件的基本名称 server-id=1 # 必须为非零值,用于复制时标识服务器ID
-
修改后,需要重新启动
systemctl restart mysql # 或者 service mysql restart
-
-
增加定时备份
crontab -e * * * * * - - - - - | | | | | | | | | +----------星期中星期几 (0 - 6) (星期天 为0) | | | +---------------月份 (1 - 12) | | +--------------------一个月中的第几天 (1 - 31) | +-------------------------小时 (0 - 23) +------------------------------分钟 (0 - 59) 添加定时任务(每天12:50以及23:50执行备份操作) 50 12,23 * * * cd /home/;sh backup.sh >> log.txt
-
查看任务
crontab -l
文章评论