2021-12-23 乐帮网
ubuntu mysql笔记 mysql
以下是Ubuntu 20.04中安装MySQL 8.0.27的过程,适用于生产环境,不是网上到处可见的测试尝鲜文章,手动安装支持配置各种数据库参数,比如可以设置数据表名称的不再区分大小写。全程使用命令行,标准的服务流程。
首先系统安装的是ubuntu-20.04.3-live-server-amd64.iso,目前mysql 最新的版本是8.0.27。写在最前面,一般安装前都需要升级一下系统 看一看有什么补丁自动安装一下。下面开始我们的安装过程。以一台天翼云服务器为例,其它的云平台大体相同,默认情况下root是不能远程,命令行是在网页控制台执行。
1、安装准备,首先下载mysql 的安装包,mysql 的下载地址:(当然是社区版本)https://dev.mysql.com/downloads/mysql/8.0.html
选择如下图,后面的步骤就是点击 No Thanks 继续下载,不再多述:
2、系统准备,这里按自己的实际情况操作,我这里做了两件事情1)首先安装系统安全更新 , 2)建立了一个账号方便在xshell等ssh工具中远程执行命令行,同时可以使用xftp上传mysql安装包。下面开始我的表演:
安全更新升级:
# sudo apt update && sudo apt upgrade
建立一个远程账号 lebang2020
# adduser lebang2020
Adding user `lebang2020' ...
Adding new group `lebang2020' (1000) ...
Adding new user `vktadmin' (1000) with group `lebang2020' ...
Creating home directory `/home/lebang2020' ...
Copying files from `/etc/skel' ...
New password:
Retype new password:
passwd: password updated successfully
Changing the user information for vktadmin
Enter the new value, or press ENTER for the default
Full Name []:
Room Number []:
Work Phone []:
Home Phone []:
Other []:
Is the information correct? [Y/n] y
设置密码然后不断回车直到跳到 Is the information correct? [Y/n] 然后输入一下y 回车。然后使用xshell登录lebang2020,发现可以正常使用。
3、把下载到的mysql安装包传到ubuntu系统中。此处我是使用的xftp从本地上传压缩包到远程服务器。我使用lebang2020登录上传的目录 /home/lebang2020,上传的文件名:mysql-server_8.0.27-1ubuntu20.04_amd64.deb-bundle.tar
4、整理文件的位置和结构,首先使用xshell登录,然后切换到root账号如下:
lebang2020@vkt:~$ su root
Password:
root@vkt:/home/lebang2020#
下面我们把压缩包移到一个合适目录,首先得新建目录如下:
root@vkt:/home/lebang2020# mkdir /usr/soft
root@vkt:/home/lebang2020# mv /home/lebang2020/mysql-server_8.0.27-1ubuntu20.04_amd64.deb-bundle.tar /usr/soft
root@vkt:/home/lebang2020# cd /usr/soft
root@vkt:/usr/soft# ls
mysql-server_8.0.27-1ubuntu20.04_amd64.deb-bundle.tar
此时我把文件解压,执行以下命令:
root@vkt:/usr/soft# mkdir /usr/soft/mysql-server_8.0.27-1ubuntu20.04_amd64.deb-bundle
root@vkt:/usr/soft# tar -xvf mysql-server_8.0.27-1ubuntu20.04_amd64.deb-bundle.tar -C ./mysql-server_8.0.27-1ubuntu20.04_amd64.deb-bundle
libmysqlclient21_8.0.27-1ubuntu20.04_amd64.deb
libmysqlclient-dev_8.0.27-1ubuntu20.04_amd64.deb
mysql-client_8.0.27-1ubuntu20.04_amd64.deb
mysql-common_8.0.27-1ubuntu20.04_amd64.deb
mysql-community-client_8.0.27-1ubuntu20.04_amd64.deb
mysql-community-client-core_8.0.27-1ubuntu20.04_amd64.deb
mysql-community-client-plugins_8.0.27-1ubuntu20.04_amd64.deb
mysql-community-server_8.0.27-1ubuntu20.04_amd64.deb
mysql-community-server-core_8.0.27-1ubuntu20.04_amd64.deb
mysql-community-server-debug_8.0.27-1ubuntu20.04_amd64.deb
mysql-community-test_8.0.27-1ubuntu20.04_amd64.deb
mysql-community-test-debug_8.0.27-1ubuntu20.04_amd64.deb
mysql-server_8.0.27-1ubuntu20.04_amd64.deb
mysql-testsuite_8.0.27-1ubuntu20.04_amd64.deb
这样我们必须的安装文件都有了。进入目录后可以看到所有的文件如下:
root@vkt:/usr/soft# cd mysql-server_8.0.27-1ubuntu20.04_amd64.deb-bundle
root@vkt:/usr/soft/mysql-server_8.0.27-1ubuntu20.04_amd64.deb-bundle# ls
libmysqlclient21_8.0.27-1ubuntu20.04_amd64.deb mysql-community-server_8.0.27-1ubuntu20.04_amd64.deb
libmysqlclient-dev_8.0.27-1ubuntu20.04_amd64.deb mysql-community-server-core_8.0.27-1ubuntu20.04_amd64.deb
mysql-client_8.0.27-1ubuntu20.04_amd64.deb mysql-community-server-debug_8.0.27-1ubuntu20.04_amd64.deb
mysql-common_8.0.27-1ubuntu20.04_amd64.deb mysql-community-test_8.0.27-1ubuntu20.04_amd64.deb
mysql-community-client_8.0.27-1ubuntu20.04_amd64.deb mysql-community-test-debug_8.0.27-1ubuntu20.04_amd64.deb
mysql-community-client-core_8.0.27-1ubuntu20.04_amd64.deb mysql-server_8.0.27-1ubuntu20.04_amd64.deb
mysql-community-client-plugins_8.0.27-1ubuntu20.04_amd64.deb mysql-testsuite_8.0.27-1ubuntu20.04_amd64.deb
5、安装离线文件,请按照以下顺序执行
root@vkt:/usr/soft/mysql-server_8.0.27-1ubuntu20.04_amd64.deb-bundle# apt install ./mysql-common_8.0.27-1ubuntu20.04_amd64.deb
root@vkt:/usr/soft/mysql-server_8.0.27-1ubuntu20.04_amd64.deb-bundle# apt install /usr/soft/mysql-server_8.0.27-1ubuntu20.04_amd64.deb-bundle/mysql-common_8.0.27-1ubuntu20.04_amd64.deb
root@vkt:/usr/soft/mysql-server_8.0.27-1ubuntu20.04_amd64.deb-bundle# apt install /usr/soft/mysql-server_8.0.27-1ubuntu20.04_amd64.deb-bundle/mysql-community-client-plugins_8.0.27-1ubuntu20.04_amd64.deb
root@vkt:/usr/soft/mysql-server_8.0.27-1ubuntu20.04_amd64.deb-bundle# apt install /usr/soft/mysql-server_8.0.27-1ubuntu20.04_amd64.deb-bundle/libmysqlclient21_8.0.27-1ubuntu20.04_amd64.deb
root@vkt:/usr/soft/mysql-server_8.0.27-1ubuntu20.04_amd64.deb-bundle# apt install /usr/soft/mysql-server_8.0.27-1ubuntu20.04_amd64.deb-bundle/mysql-community-client-core_8.0.27-1ubuntu20.04_amd64.deb
root@vkt:/usr/soft/mysql-server_8.0.27-1ubuntu20.04_amd64.deb-bundle# apt install /usr/soft/mysql-server_8.0.27-1ubuntu20.04_amd64.deb-bundle/mysql-community-client_8.0.27-1ubuntu20.04_amd64.deb
root@vkt:/usr/soft/mysql-server_8.0.27-1ubuntu20.04_amd64.deb-bundle# apt install /usr/soft/mysql-server_8.0.27-1ubuntu20.04_amd64.deb-bundle/mysql-client_8.0.27-1ubuntu20.04_amd64.deb
root@vkt:/usr/soft/mysql-server_8.0.27-1ubuntu20.04_amd64.deb-bundle# apt install /usr/soft/mysql-server_8.0.27-1ubuntu20.04_amd64.deb-bundle/mysql-community-server-core_8.0.27-1ubuntu20.04_amd64.deb
root@vkt:/usr/soft/mysql-server_8.0.27-1ubuntu20.04_amd64.deb-bundle# apt install /usr/soft/mysql-server_8.0.27-1ubuntu20.04_amd64.deb-bundle/mysql-community-server_8.0.27-1ubuntu20.04_amd64.deb
倒数第二个会要求输入一个y然后回车,安装最后一个时会弹出窗口要求设置数据库root的密码,我们此时要记住自己的密码哦。输入两后一直回国就安装完成了,至于后面的test和debug工具包我不需要也没有安装。需要的可自动安装 。
6、数据库配置,重点来了,似乎这个版本的配置和8.0.19差别挺大。详细信息可参考官方的配置项,这里我只设置一个“不区分表名的大小写”,配置文件所在目录:
root@vkt:/etc/mysql# ls
conf.d my.cnf my.cnf.fallback mysql.cnf mysql.conf.d
root@vkt:/etc/mysql# cd /etc/mysql/mysql.conf.d
root@vkt:/etc/mysql/mysql.conf.d# ls
mysqld.cnf
设置mysqld.cnf文件
root@vkt:/etc/mysql/mysql.conf.d# vim mysqld.cnf
设置mysqld.cnf的内容如下:
[mysqld]
#服务端口号 默认3306
port=3306
#注意这个配置是时区的东八区
default-time-zone='+8:00'
# 允许访问的IP网段
bind-address=0.0.0.0
# 跳过密码登录
#skip-grant-tables
#数据库默认字符集,主流字符集支持一些特殊表情符号(特殊表情符占用4个字节) 这个在新版中不再需要设置
#character-set-server=utf8mb4
#是否对sql语句大小写敏感,1表示不敏感
lower-case-table-names=1
#最大连接数
max-connections=400
#最大错误连接数
max-connect-errors=1000
#mysql binlog日志文件保存的过期时间,过期后自动删除
#expire_logs_days=5
binlog-expire-logs-seconds=604800
至此配置完成。
7、初始化数据库,使用命令:
root@vkt:/etc/mysql/conf.d# mysqld --initialize --lower-case-table-names=1
8、建立数据库用户,主要建立数据库和用户然后分配权限和设置允许远程登录
root@vkt:/etc/mysql/conf.d# service mysql start
root@vkt:/etc/mysql/conf.d# service mysql status
● mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2021-12-23 01:28:17 UTC; 11min ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 21003 ExecStartPre=/usr/share/mysql-8.0/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
Main PID: 21042 (mysqld)
Status: "Server is operational"
Tasks: 37 (limit: 994)
Memory: 350.7M
CGroup: /system.slice/mysql.service
└─21042 /usr/sbin/mysqld
Dec 23 01:28:11 vkt systemd[1]: Starting MySQL Community Server...
Dec 23 01:28:17 vkt systemd[1]: Started MySQL Community Server.
root@vkt:/etc/mysql/conf.d#
建立用户和数据命令行如下:
root@vkt:/etc/mysql/conf.d# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.27 MySQL Community Server - GPL
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> CREATE DATABASE IF NOT EXISTS lebangdb default charset utf8 COLLATE utf8_general_ci;
Query OK, 1 row affected, 2 warnings (0.12 sec)
mysql> create user 'vkt'@'%' identified WITH mysql_native_password by '123456';
Query OK, 0 rows affected (0.54 sec)
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> update user set host='%' where user='vkt';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0
mysql> use lebangdb;
Database changed
mysql> grant all privileges on lebangdb.* to 'vkt'@'%';
Query OK, 0 rows affected (0.15 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.12 sec)
至些我们的数据库lebangdb 和用户vkt都建立了,试一试使用mysql客户端就可以直觉访问数据了。
8、设置mysql服务自动启动命令行如下:
systemctl enable mysql
写在最后
可能安装过程中会出错,需要卸载数据库,相反操作就可以了如下:
apt remove libmysqlclient21:amd64
apt remove mysql-client
apt remove mysql-common
apt remove mysql-community-client
apt remove mysql-community-client-core
apt remove mysql-community-client-plugins
apt remove mysql-community-server
apt remove mysql-community-server-core
apt remove mysql-server
个人原创,禁止转载。
关注我的微信公众号
在公众号里留言交流
投稿邮箱:1052839972@qq.com
庭院深深深几许?杨柳堆烟,帘幕无重数。
玉勒雕鞍游冶处,楼高不见章台路。
雨横风狂三月暮。门掩黄昏,无计留春住。
泪眼问花花不语,乱红飞过秋千去。
如果感觉对您有帮助
欢迎向作者提供捐赠
这将是创作的最大动力