# 1.检查/tmp临时目录权限(必不可少)
# 由于mysql安装过程中,会通过mysql用户在/tmp目录下新建tmp_db文件,所以请给/tmp较大的权限。执
行
[root@mysql8 opt]# chmod -R 777 /tmp
# 2.安装前,检查依赖
[root@mysql8 opt]# rpm -qa|grep libaio
libaio-0.3.109-13.el7.x86_64
[root@mysql8 opt]# rpm -qa|grep net-tools
net-tools-2.0-0.25.20131004git.el7.x86_64
[root@mysql8 opt]# rpm -qa|grep net-tools
rpm -ivh mysql-community-common-8.0.25-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-plugins-8.0.25-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-8.0.25-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-8.0.25-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-8.0.25-1.el7.x86_64.rpm
[root@mysql8 opt]# rpm -ivh mysql-community-common-8.0.25-1.el7.x86_64.rpm
警告:mysql-community-common-8.0.25-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:mysql-community-common-8.0.25-1.e################################# [100%]
[root@mysql8 opt]# rpm -ivh mysql-community-client-plugins-8.0.25-1.el7.x86_64.rpm
警告:mysql-community-client-plugins-8.0.25-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:mysql-community-client-plugins-8.################################# [100%]
[root@mysql8 opt]# rpm -ivh mysql-community-libs-8.0.25-1.el7.x86_64.rpm
警告:mysql-community-libs-8.0.25-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
错误:依赖检测失败:
mariadb-libs 被 mysql-community-libs-8.0.25-1.el7.x86_64 取代
[root@mysql8 opt]# yum remove mysql-libs
已加载插件:fastestmirror, langpacks
正在解决依赖关系
--> 正在检查事务
---> 软件包 mariadb-libs.x86_64.1.5.5.65-1.el7 将被 删除
--> 正在处理依赖关系 libmysqlclient.so.18()(64bit),它被软件包 2:postfix-2.10.1-9.el7.x86_64 需要
--> 正在处理依赖关系 libmysqlclient.so.18(libmysqlclient_18)(64bit),它被软件包 2:postfix-2.10.1-9.el7.x86_64 需要
--> 正在检查事务
---> 软件包 postfix.x86_64.2.2.10.1-9.el7 将被 删除
--> 解决依赖关系完成
依赖关系解决
=================================================================================================================================================================================================================
Package 架构 版本 源 大小
=================================================================================================================================================================================================================
正在删除:
mariadb-libs x86_64 1:5.5.65-1.el7 @anaconda 4.4 M
为依赖而移除:
postfix x86_64 2:2.10.1-9.el7 @anaconda 12 M
事务概要
=================================================================================================================================================================================================================
移除 1 软件包 (+1 依赖软件包)
安装大小:17 M
是否继续?[y/N]:y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
警告:RPM 数据库已被非 yum 程序修改。
正在删除 : 2:postfix-2.10.1-9.el7.x86_64 1/2
正在删除 : 1:mariadb-libs-5.5.65-1.el7.x86_64 2/2
验证中 : 1:mariadb-libs-5.5.65-1.el7.x86_64 1/2
验证中 : 2:postfix-2.10.1-9.el7.x86_64 2/2
删除:
mariadb-libs.x86_64 1:5.5.65-1.el7
作为依赖被删除:
postfix.x86_64 2:2.10.1-9.el7
完毕!
[root@mysql8 opt]# rpm -ivh mysql-community-libs-8.0.25-1.el7.x86_64.rpm
警告:mysql-community-libs-8.0.25-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:mysql-community-libs-8.0.25-1.el7################################# [100%]
[root@mysql8 opt]# rpm -ivh mysql-community-client-8.0.25-1.el7.x86_64.rpm
警告:mysql-community-client-8.0.25-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:mysql-community-client-8.0.25-1.e################################# [100%]
[root@mysql8 opt]# rpm -ivh mysql-community-server-8.0.25-1.el7.x86_64.rpm
警告:mysql-community-server-8.0.25-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:mysql-community-server-8.0.25-1.e################################# [100%]
[root@mysql8 opt]# mysql --version
mysql Ver 8.0.25 for Linux on x86_64 (MySQL Community Server - GPL)
# 执行如下命令,查看是否安装成功。需要增加 -i 不用去区分大小写,否则搜索不到
[root@mysql8 opt]# rpm -qa|grep -i mysql
mysql-community-client-plugins-8.0.25-1.el7.x86_64
mysql-community-client-8.0.25-1.el7.x86_64
mysql-community-server-8.0.25-1.el7.x86_64
mysql-community-common-8.0.25-1.el7.x86_64
mysql-community-libs-8.0.25-1.el7.x86_64
[root@mysql8 opt]# mysqld --initialize --user=mysql
[root@mysql8 opt]# cat /var/log/mysqld.log
2022-11-22T13:42:43.403732Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.25) initializing of server in progress as process 3121
2022-11-22T13:42:43.418041Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2022-11-22T13:42:43.751344Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2022-11-22T13:42:44.754290Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: woyp-8=tyvJD
#加不加.service后缀都可以
启动:systemctl start mysqld.service
关闭:systemctl stop mysqld.service
重启:systemctl restart mysqld.service
查看状态:systemctl status mysqld.service
mysqld 这个可执行文件就代表着 MySQL 服务器程序,运行这个可执行文件就可以直接启动一个
服务器进程。
[root@mysql8 opt]# systemctl list-unit-files|grep mysqld.service
mysqld.service enabled
# 如果希望不进行自启动,运行如下命令设置
systemctl disable mysqld.service
[root@mysql8 opt]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.25 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.
方式一:关闭防火墙
systemctl start firewalld.service
systemctl status firewalld.service
systemctl stop firewalld.service
#设置开机启用防火墙
systemctl enable firewalld.service
#设置开机禁用防火墙
systemctl disable firewalld.service
方式二:开放端口
# 查看开放的端口号
firewall-cmd --list-all
# 设置开放的端口号
firewall-cmd --add-service=http --permanent
firewall-cmd --add-port=3306/tcp --permanent
# 重启防火墙
firewall-cmd --reload
# 可以看到root用户的当前主机配置信息为localhost。
use mysql;
select Host,User from user;
配置新连接报错:错误号码 2058,分析是 mysql 密码加密方法变了。
解决方法:Linux下 mysql -u root -p 登录你的 mysql 数据库,然后 执行这条SQL:
然后在重新配置SQLyog的连接,则可连接成功了,OK
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '9602111022yxTZ@';
Query OK, 0 rows affected (0.01 sec)
①. 修改MySQL5.7字符集
在MySQL 8.0版本之前,默认字符集为latin1,utf8字符集指向的是utf8mb3网站开发人员在数据库设计的时候往往会将编码修改为utf8字符集。如果遗忘修改默认的编码,就会出现乱码的问题。从MySQL8.0开始,数据库的默认编码将改为utf8mb4,从而避免上述乱码的问题
②. 查看默认使用的字符集
mysql> show variables like '%character%';
+--------------------------+--------------------------------+
| Variable_name | Value |
+--------------------------+--------------------------------+
| character_set_client | utf8mb4 |
| character_set_connection | utf8mb4 |
| character_set_database | utf8mb4 |
| character_set_filesystem | binary |
| character_set_results | utf8mb4 |
| character_set_server | utf8mb4 |
| character_set_system | utf8mb3 |
| character_sets_dir | /usr/share/mysql-8.0/charsets/ |
+--------------------------+--------------------------------+
8 rows in set (0.01 sec)
mysql> create database dbtest01;
Query OK, 1 row affected (0.01 sec)
mysql> use dbtest01;
Database changed
mysql> create table emp01(id int, name varchar(15));
Query OK, 0 rows affected (0.02 sec)
mysql> insert into emp01(id, name) values(10, 'Tom');
Query OK, 1 row affected (0.03 sec)
mysql> insert into emp01(id, name) values(12, '数据库');
Query OK, 1 row affected (0.00 sec)
mysql> select * from emp01;
+------+-----------+
| id | name |
+------+-----------+
| 10 | Tom |
| 12 | 数据库 |
+------+-----------+
2 rows in set (0.00 sec)
mysql> show create table emp01;
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------+
| emp01 | CREATE TABLE `emp01` (
`id` int DEFAULT NULL,
`name` varchar(15) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> show create database dbtest01;
+----------+------------------------------------------------------------------------------------------------------------------------------------+
| Database | Create Database |
+----------+------------------------------------------------------------------------------------------------------------------------------------+
| dbtest01 | CREATE DATABASE `dbtest01` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='N' */ |
+----------+------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> show variables like '%character%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.01 sec)
mysql> create database dbtest01;
Query OK, 1 row affected (0.00 sec)
mysql> use dbtest01;
Database changed
mysql> create table emp01(id int, name varchar(15));
Query OK, 0 rows affected (0.01 sec)
mysql> insert into emp01(id, name) values(10, 'Tom');
Query OK, 1 row affected (0.00 sec)
mysql> insert into emp01(id, name) values(12, '数据库');
ERROR 1366 (HY000): Incorrect string value: '\xE6\x95\xB0\xE6\x8D\xAE...' for column 'name' at row 1
mysql> show create table emp01;
+-------+------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+------------------------------------------------------------------------------------------------------------------------------+
| emp01 | CREATE TABLE `emp01` (
`id` int(11) DEFAULT NULL,
`name` varchar(15) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> show create database dbtest01;
+----------+---------------------------------------------------------------------+
| Database | Create Database |
+----------+---------------------------------------------------------------------+
| dbtest01 | CREATE DATABASE `dbtest01` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+----------+---------------------------------------------------------------------+
1 row in set (0.00 sec)
vi /etc/my.cnf
# 在[mysqld]下添加:
character_set_server=utf8
# 重启MySQL服务:
systemctl restart mysqld
# 现在MySQL5的字符集就是:
mysql> show variables like '%character%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
# 之后再创建新的数据库、新的表,在不指定字符集的情况下,就会使用默认的utf8编码
mysql> show variables like '%character%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.01 sec)
mysql> use dbtest01;
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> show create database dbtest01;
+----------+---------------------------------------------------------------------+
| Database | Create Database |
+----------+---------------------------------------------------------------------+
| dbtest01 | CREATE DATABASE `dbtest01` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+----------+---------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> show create table emp01;
+-------+------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+------------------------------------------------------------------------------------------------------------------------------+
| emp01 | CREATE TABLE `emp01` (
`id` int(11) DEFAULT NULL,
`name` varchar(15) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> use dbtest01;
Database changed
mysql> alter database dbtest01 character set 'utf8';
Query OK, 1 row affected (0.01 sec)
mysql> show create database dbtest01;
+----------+-------------------------------------------------------------------+
| Database | Create Database |
+----------+-------------------------------------------------------------------+
| dbtest01 | CREATE DATABASE `dbtest01` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+-------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> alter table emp01 convert to character set 'utf8';
Query OK, 1 row affected (0.02 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> show create table emp01;
+-------+----------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+----------------------------------------------------------------------------------------------------------------------------+
| emp01 | CREATE TABLE `emp01` (
`id` int(11) DEFAULT NULL,
`name` varchar(15) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+----------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> insert into emp01(id, name) values(33, '数据库');
Query OK, 1 row affected (0.00 sec)
mysql> select * from emp01;
+------+-----------+
| id | name |
+------+-----------+
| 10 | Tom |
| 33 | 数据库 |
+------+-----------+
2 rows in set (0.00 sec)
①. MySQL有4个级别的字符集和比较规则,分别是:
服务器级别、数据库级别、表级别、列级别
②. show variables like ‘character%’
[server]
character_set_server=gbk # 默认字符集
collation_server=gbk_chinese_ci #对应的默认的比较规则
# 当服务器启动的时候读取这个配置文件后这两个系统变量的值便修改了
CREATE DATABASE 数据库名
[[DEFAULT] CHARACTER SET 字符集名称]
[[DEFAULT] COLLATE 比较规则名称];
ALTER DATABASE 数据库名
[[DEFAULT] CHARACTER SET 字符集名称]
[[DEFAULT] COLLATE 比较规则名称];
CREATE TABLE 表名 (列的信息)
[[DEFAULT] CHARACTER SET 字符集名称]
[COLLATE 比较规则名称]]
ALTER TABLE 表名
[[DEFAULT] CHARACTER SET 字符集名称]
[COLLATE 比较规则名称]
CREATE TABLE 表名(
列名 字符串类型 [CHARACTER SET 字符集名称] [COLLATE 比较规则名称],
其他列...
);
ALTER TABLE 表名 MODIFY 列名 字符串类型 [CHARACTER SET 字符集名称] [COLLATE 比较规则名称];
⑦. 提示:在转换列的字符集时需要注意,如果转换前列中存储的数据不能用转换后的字符集进行表示会发生错误。比方说原先列使用的字符集是utf8,列中存储了一些汉字,现在把列的字符集转换为ascii的话就会出错,因为ascii字符集并不能表示汉字字符
⑧. 我们介绍的这4个级别字符集和比较规则的联系如下:
mysql> show charset;
+----------+---------------------------------+---------------------+--------+
| Charset | Description | Default collation | Maxlen |
+----------+---------------------------------+---------------------+--------+
| armscii8 | ARMSCII-8 Armenian | armscii8_general_ci | 1 |
| ascii | US ASCII | ascii_general_ci | 1 |
| big5 | Big5 Traditional Chinese | big5_chinese_ci | 2 |
| binary | Binary pseudo charset | binary | 1 |
| cp1250 | Windows Central European | cp1250_general_ci | 1 |
| cp1251 | Windows Cyrillic | cp1251_general_ci | 1 |
| cp1256 | Windows Arabic | cp1256_general_ci | 1 |
| cp1257 | Windows Baltic | cp1257_general_ci | 1 |
| cp850 | DOS West European | cp850_general_ci | 1 |
| cp852 | DOS Central European | cp852_general_ci | 1 |
| cp866 | DOS Russian | cp866_general_ci | 1 |
| cp932 | SJIS for Windows Japanese | cp932_japanese_ci | 2 |
| dec8 | DEC West European | dec8_swedish_ci | 1 |
| eucjpms | UJIS for Windows Japanese | eucjpms_japanese_ci | 3 |
| euckr | EUC-KR Korean | euckr_korean_ci | 2 |
| gb18030 | China National Standard GB18030 | gb18030_chinese_ci | 4 |
| gb2312 | GB2312 Simplified Chinese | gb2312_chinese_ci | 2 |
| gbk | GBK Simplified Chinese | gbk_chinese_ci | 2 |
| geostd8 | GEOSTD8 Georgian | geostd8_general_ci | 1 |
| greek | ISO 8859-7 Greek | greek_general_ci | 1 |
| hebrew | ISO 8859-8 Hebrew | hebrew_general_ci | 1 |
| hp8 | HP West European | hp8_english_ci | 1 |
| keybcs2 | DOS Kamenicky Czech-Slovak | keybcs2_general_ci | 1 |
| koi8r | KOI8-R Relcom Russian | koi8r_general_ci | 1 |
| koi8u | KOI8-U Ukrainian | koi8u_general_ci | 1 |
| latin1 | cp1252 West European | latin1_swedish_ci | 1 |
| latin2 | ISO 8859-2 Central European | latin2_general_ci | 1 |
| latin5 | ISO 8859-9 Turkish | latin5_turkish_ci | 1 |
| latin7 | ISO 8859-13 Baltic | latin7_general_ci | 1 |
| macce | Mac Central European | macce_general_ci | 1 |
| macroman | Mac West European | macroman_general_ci | 1 |
| sjis | Shift-JIS Japanese | sjis_japanese_ci | 2 |
| swe7 | 7bit Swedish | swe7_swedish_ci | 1 |
| tis620 | TIS620 Thai | tis620_thai_ci | 1 |
| ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 |
| ujis | EUC-JP Japanese | ujis_japanese_ci | 3 |
| utf16 | UTF-16 Unicode | utf16_general_ci | 4 |
| utf16le | UTF-16LE Unicode | utf16le_general_ci | 4 |
| utf32 | UTF-32 Unicode | utf32_general_ci | 4 |
| utf8 | UTF-8 Unicode | utf8_general_ci | 3 |
| utf8mb4 | UTF-8 Unicode | utf8mb4_0900_ai_ci | 4 |
+----------+---------------------------------+---------------------+--------+
41 rows in set (0.01 sec)
#查看GBK字符集的比较规则
SHOW COLLATION LIKE 'gbk%';
#查看UTF-8字符集的比较规则
SHOW COLLATION LIKE 'utf8%'
#查看服务器的字符集和比较规则
SHOW VARIABLES LIKE '%_server';
#查看数据库的字符集和比较规则
SHOW VARIABLES LIKE '%_database';
#查看具体数据库的字符集
SHOW CREATE DATABASE dbtest1;
#修改具体数据库的字符集
ALTER DATABASE dbtest1 DEFAULT CHARACTER SET 'utf8' COLLATE 'utf8_general_ci';
#查看表的字符集
show create table employees;
#查看表的比较规则
show table status from atguigudb like 'employees';
#修改表的字符集和比较规则
ALTER TABLE emp1 DEFAULT CHARACTER SET 'utf8' COLLATE 'utf8_general_ci';
# 为了体现出字符集在请求处理过程中的变化,我们这里特意修改一个系统变量的值:
mysql> set character_set_connection = gbk;
Query OK, 0 rows affected (0.00 sec)
# 现在假设我们客户端发送的请求是下边这个字符串:
SELECT * FROM t WHERE s = '我';
SET NAMES 字符集名;
# 这一条语句产生的效果和我们执行这3条的效果是一样的
SET character_set_client = 字符集名;
SET character_set_connetion = 字符集名;
SET character_set_results = 字符集名;
# 比如说我的客户端使用的是utf8字符集,所以需要把这几个系统变量的值设置位utf8;
myslq>SET NAME utf8;
# 它起到的效果和执行一遍SET NAMES utf8是一样一样的,都会将这三个系统变量的值设置成utf8
[client]
default-character-set=utf8
select @@session.sql_mode
select @@global.sql_mode
#或者
show variables like 'sql_mode';
SET GLOBAL sql_mode = 'modes...'; #全局
SET SESSION sql_mode = 'modes...'; #当前会话
#改为严格模式。此方法只在当前会话中生效,关闭当前会话就不生效了。
set SESSION sql_mode='STRICT_TRANS_TABLES';
#改为严格模式。此方法在当前服务中生效,重启MySQL服务后失效。
set GLOBAL sql_mode='STRICT_TRANS_TABLES';
[mysqld]
sql_mode=ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR
_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION