Ubuntu20.04安装Mysql5.7.9

继上一次虚拟机安装ubuntu20.04系统后,本次使用tar包安装Mysql5.7.9,并完成Navicat远程连接Mysql

ps:白框中mysql部分代码最好不要直接复制,可能会有点问题,出现问题手敲就好了


目录

1.Mysql数据库下载

2.安装Mysql

 创建mysql用户组

 创建mysql用户

 解压tar包到/usr/lcoal/目录下

创建软链接到/usr/lcoal/mysql

编辑/etc/my.cnf配置文件

初始化数据库

 将mysql服务加入到系统服务中

3.创建用户并设置远程连接

2、然后创建新的test用户、并创建本地连接密码

3、给test用户赋予本地操作权限(我这里给的所有权限,并对所有库所有表)

4、 给test用户赋予远程操作权限(并设置远程密码)

 4.使用navicat进行远程连接

 1、安装navicat


1.Mysql数据库下载


我选择的是Mysql5.7.9版本,mysql-5.7.9-linux-glibc2.5-x86_64.tar.gz

mysql数据库下载地址icon-default.png?t=N7T8https://downloads.mysql.com/archives/community/


2.安装Mysql

上传刚才下载好的tar包到ubuntu中


 创建mysql用户组

sudo groupadd mysql

 创建mysql用户

sudo useradd -r -g mysql mysql


 解压tar包到/usr/lcoal/目录下

sudo tar -xzvf /home/test/mysql-5.7.9-linux-glibc2.5-x86_64.tar.gz -C /usr/local/

 解压后得到


创建软链接到/usr/lcoal/mysql

sudo ln -s /usr/local/mysql-5.7.9-linux-glibc2.5-x86_64 /usr/local/mysql

 当前所属用户组和用户为root,我们要修改为mysql

sudo chown -R mysql:mysql /usr/local/mysql/


编辑/etc/my.cnf配置文件

sudo cp /usr/lcoal/mysql/support-files/my-default.cnf  /etc/my.cnf

sudo vim /etc/my.cnf

以下为配置文件内件内容,存放目录、端口根据需要修改

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.

[client]
port = 3306
socket = /usr/local/mysql/mysql.sock

[mysqld]

# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M

# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin

# These are commonly set, remove the # and set as required.
character_set_server = utf8
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
pid-file = /usr/local/mysql/data/mysql.pid
server_id = 1
socket = /usr/local/mysql/mysql.sock
log-error=/usr/local/mysql/error.log
#general_log=1
#general_log_file=/usr/local/mysql/mysql.log

# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M 

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 

初始化数据库前需要安装依赖libaio1,否则报错 

sudo apt install libaio1


初始化数据库

cd /usr/local/mysql/bin/

sudo ./mysqld --defaults-file=/etc/my.cnf --initialize --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ --user=mysql


初始化密码记载在错误日志中,刚才/etc/my.cnf已经配置了error-log的路径,输入以下命令也可以查看密码

sudo cat /usr/local/mysql/error.log | grep -o "root@localhost: .*" | cut -d " " -f 2

 将mysql服务加入到系统服务中

sudo cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql

重新加载系统服务

sudo systemctl daemon-reload 

 启动数据库,并查看数据库服务状态;启动成功

sudo systemctl start mysql  #启动数据库

sudo systemctl status mysql  #查看数据库运行状态

 将数据库设置为开机自启

sudo systemctl enable mysql


 将mysql执行文件软链接到/usr/bin下

sudo ln -s /usr/local/mysql/bin/mysql /usr/bin/

进入数据库前需要安装依赖libncurses.so.5,不安装报错

sudo apt install libncurses5  


安装完依赖后,进入数据库 mysql -uroot -p ,然后输入初始化密码(刚才查看的)

安装成功!


3.创建用户并设置远程连接


1、进入数据库,进行操作前先修改默认密码

set password='password';

2、然后创建新的test用户、并创建本地连接密码

CREATE USER 'test'@'localhost' IDENTIFIED BY 'PASSWORD';

3、给test用户赋予本地操作权限(我这里给的所有权限,并对所有库所有表)

GRANT ALL ON *.* TO 'test'@'localhost' WITH GRANT OPTION; 

4、 给test用户赋予远程操作权限(并设置远程密码)

GRANT ALL ON *.* TO 'test'@'%' IDENTIFIED BY 'PASSWORD' WITH GRANT OPTION;

5、重新加载授权表,确保权限或账户相关的更改能够立即生效

FLUSH PRIVILEGES;

 6、查看test用户本地和远程的操作权限,设置权限成功

mysql> SHOW GRANTS FOR 'test'@'localhost';
+---------------------------------------------------------------------+
| Grants for test@localhost                                           |
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'test'@'localhost' WITH GRANT OPTION |
+---------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> SHOW GRANTS FOR 'test'@'%';
+-------------------------------------------------------------+
| Grants for test@%                                           |
+-------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'test'@'%' WITH GRANT OPTION |
+-------------------------------------------------------------+
1 row in set (0.00 sec)

mysql>

7、测试test本地登录,本地登录成功

test@ubuntu:~$ mysql -utest -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.9 MySQL Community Server (GPL)

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

 4.使用navicat进行远程连接


 1、安装navicat

下载地址:Navicat | 产品

破解版navicat参考其他博客

2、安装完成后我们打开navicat,点击连接选择MySQL

输入我们新建的用户名和密码.端口


点击连接测试,连接成功


到此,安装数据库并使用navicat远程连接操作完成。


下面是我自己将以上命令写成了一个脚本,用来自己运行并完成数据库的安装(有网,需要安装依赖)

#!/bin/bash
#和一步一步安装是一样的,只不过方便自己运行
#创建mysql用户组
sudo groupadd mysql
wait
#创建mysql用户
sudo useradd -r -g mysql mysql
wait
#解压tar包
sudo tar -xzvf /home/test/mysql-5.7.9-linux-glibc2.5-x86_64.tar.gz -C /usr/local/
wait
#创建软链接
sudo ln -s /usr/local/mysql-5.7.9-linux-glibc2.5-x86_64 /usr/local/mysql
#修改用户、用户组
sudo chown -R mysql:mysql /usr/local/mysql
sudo chown -R mysql:mysql /usr/local/mysql/

sleep 2
sudo rm -f /etc/my.cnf
#这里是我cp的文件并修改了成了mycon.cnf,直接cp到my.cnf了,后面会附上
sudo cp /home/test/mycon.cnf /etc/my.cnf

#准备数据库初始化
cd /usr/local/mysql/bin
second=3;
for((i=$second;i>=1;i--));do
	echo -n "正在准备数据库初始化,$i秒..."
	sleep 1.5
	echo -ne "\r\033[K"  # 清除当前行
done
#安装依赖
sudo apt install libaio1
wait
#初始化
sudo ./mysqld --defaults-file=/etc/my.cnf --initialize --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ --user=mysql
sleep 3
#获取初始化密码,echo "你的虚拟机密码"
passwd=$(echo "123" | sudo -S sudo cat /usr/local/mysql/error.log | grep -o "root@localhost: .*" | cut -d " " -f 2)
#添加服务
sudo cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
wait

#重载系统服务
sudo systemctl daemon-reload
#启动数据库
sudo systemctl start mysql
wait

#创建二进制文件软链接
sudo ln -s /usr/local/mysql/bin/mysql /usr/bin/

#安装依赖
sudo apt install libncurses5
for((i=2;i>=1;i--));do
        echo -n "打开数据库,$i秒..."
        sleep 1.5
        echo -ne "\r\033[K"  # 清除当前行
done
#打开数据库,自动获取密码
mysql -uroot -p${passwd}

mycon.cnf文件,根据需要自己修改配置

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.

[client]
port = 3306
socket = /usr/local/mysql/mysql.sock

[mysqld]

# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M

# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin

# These are commonly set, remove the # and set as required.
character_set_server = utf8
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
pid-file = /usr/local/mysql/data/mysql.pid
server_id = 1
socket = /usr/local/mysql/mysql.sock
log-error=/usr/local/mysql/error.log
#general_log=1
#general_log_file=/usr/local/mysql/mysql.log

# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M 

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

希望对大家有帮助喔,多多点赞!!

  • 68
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Ubuntu 20.04安装MySQL,你可以按照以下步骤进行操作: 1. 首先,更新软件源列表,确保你使用的是最新的软件包信息: ``` sudo apt update ``` 2. 接下来,使用apt命令安装MySQL服务器: ``` sudo apt install mysql-server ``` 3. 在安装过程中,你将会被要求设置MySQL的root用户密码。请根据提示输入并确认密码。 4. 安装完成后,你可以使用以下命令验证MySQL服务是否已经成功启动: ``` sudo systemctl status mysql ``` 如果MySQL服务正在运行,你将会看到类似于"active (running)"的状态信息。 现在,你已经成功在Ubuntu 20.04安装MySQL。你可以使用以下命令登录到MySQL服务器: ``` mysql -u root -p ``` 请注意,这里的"-p"选项表示需要输入密码。输入你在安装过程中设置的root密码即可。 如果你需要卸载MySQL,可以使用以下命令: ``` sudo apt-get remove --purge mysql-server mysql-client mysql-common sudo apt-get autoremove sudo rm -rf /var/lib/mysql sudo rm -rf /etc/mysql ``` 请确保在执行卸载命令之前备份你的数据库,以免数据丢失。 希望这些信息对你有帮助! #### 引用[.reference_title] - *1* *2* [Ubuntu20.04安装Mysql](https://blog.csdn.net/m0_67393686/article/details/123702186)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Ubuntu20.04安装MySQL](https://blog.csdn.net/weixin_44166819/article/details/122598525)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值