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/Ubuntu20.04安装Mysql5.7.9_第1张图片


2.安装Mysql

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

Ubuntu20.04安装Mysql5.7.9_第2张图片


 创建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/

 解压后得到

Ubuntu20.04安装Mysql5.7.9_第3张图片


创建软链接到/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

Ubuntu20.04安装Mysql5.7.9_第4张图片


初始化数据库

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  #查看数据库运行状态

Ubuntu20.04安装Mysql5.7.9_第5张图片

 将数据库设置为开机自启

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 ,然后输入初始化密码(刚才查看的)

Ubuntu20.04安装Mysql5.7.9_第6张图片

安装成功!


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

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

Ubuntu20.04安装Mysql5.7.9_第7张图片


点击连接测试,连接成功

Ubuntu20.04安装Mysql5.7.9_第8张图片


到此,安装数据库并使用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

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

你可能感兴趣的:(mysql,ubuntu,linux,database)