文章目录
- 1 windows
- 安装MySQL5.7
- 1 下载mysql
- 2 设置环境变量
- 3 配置
- 安装MySQL8.0
- 1 下载mysql
- 2 设置环境变量
- 3 配置
- 2 Linux
- Generic Binaries
- Ubuntu
- deepin v20
- centos7
- debian11
- mariadb
- mysql 8
- Oracle源安装
- 国内源安装
- linux系统彻底卸载mysql/mariadb
1 windows
安装MySQL5.7
1 下载mysql
将压缩包解压到任意一个文件夹,进入到mysql文件夹根目录,创建图中箭头所示文件my.ini
my.ini文件内容
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8
[mysqld]
#设置3306端口
port =3306
# 设置mysql的安装目录
basedir=D:\Edge browser\mysql-5.7.25-winx64
# 设置mysql数据库的数据的存放目录
datadir=D:\Edge browser\mysql-5.7.25-winx64\data
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
以“#”开头的是注释,不加也行。此处有两点需要注意的:
- basedir=D:\Edge browser\mysql-5.7.25-winx64 这是安装mysql的目录,“=”后面的值根据情况自行修改;
- datadir=D:\Edge browser\mysql-5.7.25-winx64\data 以后新建的数据库都存放在data目录下。data可以改成其他名称
注意:my.ini文件是需要自己配置的,解压后里面没有。另,上有一名为data的文件夹,这个不需要自己建,下面会说它是怎样出现的。
2 设置环境变量
右键点击我的电脑,我的电脑-->属性-->高级系统设置-->环境变量-->Path
(系统变量、用户变量皆可)
3 配置
管理员身份运行cmd,依次执行下列命令
# 1 进入MySQL的bin目录
C:\WINDOWS\system32>cd d:\edge browser\mysql-5.7.25-winx64\bin
C:\WINDOWS\system32>d:
# 2 安装mysql服务
D:\Edge browser\mysql-5.7.25-winx64\bin>mysqld --install
# 3 初始化mysql
D:\Edge browser\mysql-5.7.25-winx64\bin>mysqld --initialize-insecure --user=mysql
# 4 启动mysql服务
D:\Edge browser\mysql-5.7.25-winx64\bin>net start mysql
MySQL 服务正在启动 .
MySQL 服务已经启动成功。
# 5 设置登录密码。输入 mysql -u root -p ,刚安装完是默认没有密码的,直接回车即可
mysql>use mysql;
mysql>update user set authentication_string=password("***") where user="root";
mysql>flush privileges;
配置完成后,再使用mysql按照下图即可:
安装MySQL8.0
1 下载mysql
mysql: error while loading shared libraries: libtinfo.so.5
sudo ln -s /lib/x86_64-linux-gnu/libtinfo.so.6.4 /lib/x86_64-linux-gnu/libtinfo.so.5
2 设置环境变量
将下载好的压缩包解压到任意一个目录,然后设置环境变量
3 配置
# 1 进入相应bin目录
C:\Windows\system32>cd g:\ProgramFiles\mysql-8.0.21-winx64\bin
C:\Windows\system32>g:
# 2 安装mysql服务(删除已存在的mysql服务使用 mysqld remove )
g:\ProgramFiles\mysql-8.0.21-winx64\bin>mysqld install
Service successfully installed.
# 3 初始化MySQL,设置初始登录密码为空
g:\ProgramFiles\mysql-8.0.21-winx64\bin>mysqld --initialize-insecure
# 4 启动MySQL服务
g:\ProgramFiles\mysql-8.0.21-winx64\bin>net start mysql
MySQL 服务正在启动 ..
MySQL 服务已经启动成功。
# 5 设置mysql登录用户密码
g:\ProgramFiles\mysql-8.0.21-winx64\bin>mysql -uroot -p
Enter password:
...
mysql> use mysql;
Database changed
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.32 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql>
注:若想在没有安装mysql服务的情况下使用mysql,可遵循如下步骤
1、首先打开一个命令行窗口,启动mysqld
PS C:\Users\fy\Desktop> mysqld --console
2022-02-21T11:16:39.222326Z 0 [System] [MY-010116] [Server] C:\Users\fy\scoop\apps\mysql\current\bin\mysqld.exe (mysqld 8.0.28) starting as process 12064
2022-02-21T11:16:39.252727Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2022-02-21T11:16:39.498091Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2022-02-21T11:16:39.748124Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2022-02-21T11:16:39.748516Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2022-02-21T11:16:39.793608Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060
2022-02-21T11:16:39.793999Z 0 [System] [MY-010931] [Server] C:\Users\fy\scoop\apps\mysql\current\bin\mysqld.exe: ready for connections. Version: '8.0.28' socket: '' port: 3306 MySQL Community Server - GPL.
2、再打开一个命令行窗口(无需输入密码)
PS C:\Users\fy\Desktop> mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.28 MySQL Community Server - GPL
Copyright (c) 2000, 2022, 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>
2 Linux
Generic Binaries
https://dev.mysql.com/doc/refman/8.0/en/binary-installation.html
Ubuntu
rorschach@rorschach-HP:~$ sudo apt install mysql-server
rorschach@rorschach-HP:~$ systemctl start mysql
rorschach@rorschach-HP:~$ sudo cat /etc/mysql/debian.cnf
[sudo] rorschach 的密码:
# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host = localhost
user = debian-sys-maint
password = M3arusCdWI1TXaYl
socket = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host = localhost
user = debian-sys-maint
password = M3arusCdWI1TXaYl
socket = /var/run/mysqld/mysqld.sock
rorschach@rorschach-HP:~$ sudo mysql -uroot -pM3arusCdWI1TXaYl
...省略若干字...
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> ALTER USER 'root'@'localhost' IDENTIFIED BY '***';
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
deepin v20
- 先下载对应的压缩包,deepin是基于debian的,按如图选择,下载下面两个东西
- 依次执行下列命令即可
首先解压下载好的压缩包
$ tar -xvf mysql-server_8.0.22-1debian10_amd64.deb-bundle.tar
$ sudo apt-get install libaio1
$ sudo dpkg-preconfigure mysql-community-server_*.deb
$ sudo apt install libmecab2
$ sudo dpkg -i mysql-community-client-plugins_8.0.22-1debian10_amd64.deb
进入解压缩的目录,安装mysql-community-server-core和mysql-community-client-core
$ sudo dpkg -i mysql-community-server-core***.deb
$ sudo dpkg -i mysql-community-client-core***.deb
$ sudo dpkg -i mysql-{common,community-client,client,community-server,server}_*.deb
mysql-{common,community-client,client,community-server,server}_*.deb依赖于包mysql-community-server-core和mysql-community-client-core;
而mysql-community-server-core又依赖libmecab2, mysql-community-client-core又依赖mysql-community-client-plugin;
或者如下安装:
首先解压下载好的压缩包
$ tar -xvf mysql-server_8.0.22-1debian10_amd64.deb-bundle.tar
$ sudo apt-get install libaio1
$ sudo dpkg-preconfigure mysql-community-server_*.deb
$ sudo apt install libmecab2
$ sudo dpkg -i mysql-community-client-plugins_8.0.22-1debian10_amd64.deb
$ sudo dpkg -i mysql-{common,community-client,community-client-core,client,community-server-core,community-server,server}_*.deb
centos7
参考链接:
https://dev.mysql.com/doc/refman/8.0/en/linux-installation-yum-repo.htmlhttps://cloud.tencent.com/developer/article/1965230
1、首先添加mysql的yum源,打开链接https://dev.mysql.com/downloads/repo/yum/,选择合适的源,由于我用的是centos7,因此这里选择
将此文件下载到本地并安装
[fy@localhost ~]$ wget https://repo.mysql.com//mysql80-community-release-el7-6.noarch.rpm
[fy@localhost ~]$ sudo yum install mysql80-community-release-el7-6.noarch.rpm
2、安装mysql
# 不添加参数 --nogpgcheck 可能会由于密钥问题报错。或者参考链接
# https://dev.mysql.com/doc/refman/8.0/en/checking-gpg-signature.html 添加对应的公钥进行安装
[fy@localhost ~]$ sudo yum -y install mysql-community-server --nogpgcheck
# 启动mysqld守护进程
[fy@localhost ~]$ sudo systemctl start mysqld
# 获取mysql的初始密码
[fy@localhost ~]$ sudo grep password /var/log/mysqld.log
2022-05-19T10:29:42.290212Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: saZ.PepqQ0gg
# 使用初始密码登录mysql
[fy@localhost ~]$ mysql -uroot -p
Enter password:
...
# 修改mysql密码。新密码设置不能过于简单,否则报错,这是由于mysql策略决定的
mysql> alter user 'root'@'localhost' identified by 'abC111#666';
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql>
#再次登录即可使用新密码
[fy@localhost ~]$ mysql -uroot -p
Enter password:
3、修改mysql策略,允许设置简单密码
mysql> show variables like 'validate_password%';
+--------------------------------------+--------+
| Variable_name | Value |
+--------------------------------------+--------+
| validate_password.check_user_name | ON |
| validate_password.dictionary_file | |
| validate_password.length | 8 |
| validate_password.mixed_case_count | 1 |
| validate_password.number_count | 1 |
| validate_password.policy | MEDIUM |
| validate_password.special_char_count | 1 |
+--------------------------------------+--------+
7 rows in set (0.00 sec)
# validate_password.length 合法的密码长度
# validate_password.policy 密码策略
mysql> set global validate_password.policy=0;
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password.length=3;
Query OK, 0 rows affected (0.00 sec)
# 再次修改密码
mysql> alter user 'root'@'localhost' identified by '111111';
Query OK, 0 rows affected (0.01 sec)
debian11
https://dev.mysql.com/doc/refman/8.0/en/linux-installation-debian.html
mariadb
https://mariadb.com/kb/en/set-password/#authentication-plugin-support https://mariadb.com/kb/en/authentication-from-mariadb-104/
$ sudo apt install mariadb-server
#刚安装完毕默认密码为空,不使用密码即可登录
$ sudo mysql
...
# 设置登录时强制使用密码
MariaDB [mysql]> ALTER USER root@localhost IDENTIFIED VIA mysql_native_password USING PASSWORD("fanyi");
Query OK, 0 rows affected (0.009 sec)
MariaDB [mysql]> quit
Bye
$ sudo systemctl restart myql
$ sudo mysql -uroot -p
Enter password:
mysql 8
Oracle源安装
1、安装教程
https://dev.mysql.com/doc/mysql-apt-repo-quick-guide/en/
2、设置教程
Mysql8.0默认加密连接方式修改
#初始安装完成后默认没有密码,可使用如下方式登录
$ sudo mysql -u root
#查看密码策略
#mysql> select host,user,plugin,authentication_string from mysql.user;
mysql> select user,plugin from mysql.user;
+------------------+-----------------------+
| user | plugin |
+------------------+-----------------------+
| mysql.infoschema | caching_sha2_password |
| mysql.session | caching_sha2_password |
| mysql.sys | caching_sha2_password |
| root | auth_socket |
+------------------+-----------------------+
4 rows in set (0.01 sec)
#设置密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'fanyi';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.02 sec)
mysql> select user,plugin from mysql.user;
+------------------+-----------------------+
| user | plugin |
+------------------+-----------------------+
| mysql.infoschema | caching_sha2_password |
| mysql.session | caching_sha2_password |
| mysql.sys | caching_sha2_password |
| root | mysql_native_password |
+------------------+-----------------------+
4 rows in set (0.00 sec)
国内源安装
https://mirrors.bfsu.edu.cn/help/mysql/ 添加源之后
$ sudo apt update
$ sudo apt install mysql-server
linux系统彻底卸载mysql/mariadb
#sudo apt purge mariadb
$ sudo apt purge mysql-server
#dpkg -l | grep mariadb
$ dpkg -l | grep mysql
ii mysql-apt-config 0.8.24-1 all Auto configuration for MySQL APT Repo.
ii mysql-client 8.0.32-1debian11 amd64 MySQL Client meta package depending on latest version
ii mysql-common 8.0.32-1debian11 amd64 Common files shared between packages
ii mysql-community-client 8.0.32-1debian11 amd64 MySQL Client
ii mysql-community-client-core 8.0.32-1debian11 amd64 MySQL Client Core Binaries
ii mysql-community-client-plugins 8.0.32-1debian11 amd64 MySQL Client plugin
ii mysql-community-server 8.0.32-1debian11 amd64 MySQL Server
ii mysql-community-server-core 8.0.32-1debian11 amd64 MySQL Server Core Binaires
ii python3-pymysql 0.9.3-2 all Pure-Python MySQL Driver - Python 3.x
#将输出列表第二列所有软件按名字卸载即可
$ sudo apt purge mysql-apt-config mysql-client mysql-common mysql-community-client mysql-community-client-core mysql-community-client-plugins mysql-community-server mysql-community-server-core python3-pymysql
$ sudo apt autoremove && sudo apt autoclean