最近重新安装了mysql。8.0版本一些客户端支持的不好,所以安装8.0之后又安装了5.7版本

一、使用apt repository安装

https://dev.mysql.com/doc/mysql-apt-repo-quick-guide/en/

1.Go to the download page for the MySQL APT repository at https://dev.mysql.com/downloads/repo/apt/. 下载deb文件
wget https://dev.mysql.com/get/mysql-apt-config_0.8.11-1_all.deb

2.sudo dpkg -i mysql-apt-config_0.8.11-1_all.deb
这一步里选择要安装的版本等信息

3.sudo apt-get update

4.sudo apt-get install mysql-server

5.安装完成后,检查 sudo service mysql status
sudo service mysql stop
sudo service mysql start

6.如果忘记root密码,需要重置密码

mysqld_safe –skip-grant-tables &

如果报错

mkdir -p /var/run/mysqld

chown mysql:mysql /var/run/mysqld

也可以修改my.cnf(/etc/mysql/my.cnf)

在[mysqld]中添加

skip-grant-tables

以无需认证方式登录
mysql -u root

UPDATE mysql.user SET authentication_string=null WHERE User=’root’;

FLUSH PRIVILEGES;

exit;

ALTER USER ‘root’@’localhost’ IDENTIFIED WITH caching_sha2_password BY ‘yourpasswd’;

CREATE USER ‘zhk’@’%’ IDENTIFIED BY ‘Password123′;

grant all privileges on . to zhk@’%’;

flush privileges;

mysql> alter user zhk identified WITH mysql_native_password BY ‘Password123’;

Query OK, 0 rows affected (0.10 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)
注意,mysql 8.0的密码加密方式有了修改,区分 WITH caching_sha2_password (WITH mysql_native_password),客户端需要正常连接,使用第二种加密方式比较保险

二 、在oracle官网下载deb

https://dev.mysql.com/doc/refman/8.0/en/linux-installation-debian.html

1.wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-server_5.7.24-1ubuntu18.04_amd64.deb-bundle.tar

2.shell> tar -xvf mysql-server_MVER-DVER_CPU.deb-bundle.tar

3.shell> sudo apt-get install libaio1

4.shell> sudo dpkg-preconfigure mysql-community-server_.deb

5.shell> sudo dpkg -i mysql-{common,community-client,client,community-server,server}_.deb

6.可能需要安装 sudo apt-get install libmecab2

远程无法连接的话,可以增加监听

执行命令netstat -tulpen
netstat -tulpen

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State User Inode PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 0 16801 1507/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 0 17222 1970/master
tcp6 0 0 :::3306 :::* LISTEN 27 46396 22054/mysqld
tcp6 0 0 :::22 :::* LISTEN 0 16803 1507/sshd
tcp6 0 0 ::1:25 :::* LISTEN 0 17223 1970/master
udp 0 0 0.0.0.0:68 0.0.0.0:* 0 49600 22999/dhclient
udp 0 0 0.0.0.0:43504 0.0.0.0:* 0 48850 22999/dhclient
udp6 0 0 :::47875 :::* 0 48851 22999/dhclient
这里如果没有监听3306端口或者只监听了localhost(0.0.0.0表示监听所有),则在my.cnf添加下面这一行
bind-address = 0.0.0.0