赞
踩
Mysql:版本选择
rpm -qa | grep mysql
Mysql官网:https://www.mysql.com/
下载网址:https://downloads.mysql.com/archives/community/
cat /etc/redhat-release
首先使用第三方工具上传
# 创建一个文件夹,存放mysql
mkdir -p /usr/mysql
cd /usr/mysql
# 在当前目录下(mysql)下创建一个 mysql-5.7.41 文件夹
mkdir mysql-5.7.41
# 解压安装包到该目录下
tar -xvf mysql-5.7.41-1.el7.x86_64.rpm-bundle.tar -C mysql-5.7.41
[root@localhost mysql-5.7.41]# ls
mysql-community-client-5.7.41-1.el7.x86_64.rpm
mysql-community-embedded-compat-5.7.41-1.el7.x86_64.rpm
mysql-community-server-5.7.41-1.el7.x86_64.rpm
mysql-community-common-5.7.41-1.el7.x86_64.rpm
mysql-community-embedded-devel-5.7.41-1.el7.x86_64.rpm
mysql-community-test-5.7.41-1.el7.x86_64.rpm
mysql-community-devel-5.7.41-1.el7.x86_64.rpm
mysql-community-libs-5.7.41-1.el7.x86_64.rpm
mysql-community-embedded-5.7.41-1.el7.x86_64.rpm
mysql-community-libs-compat-5.7.41-1.el7.x86_64.rpm
解压后的文件都是 rpm 文件,因此需要用到 rpm 包资源管理器相关的指令安装这些 rpm 的安装包
yum install openssl-devel
安装完该插件之后,
依次执行
以下命令安装这些 rpm 包
yum install openssl-devel rpm -ivh mysql-community-common-5.7.41-1.el7.x86_64.rpm rpm -ivh mysql-community-libs-5.7.41-1.el7.x86_64.rpm yum remove mysql-libs rpm -ivh mysql-community-libs-compat-5.7.41-1.el7.x86_64.rpm rpm -ivh mysql-community-devel-5.7.41-1.el7.x86_64.rpm # 至此所有依赖已安装完毕,下面安装客户端和服务端 rpm -ivh mysql-community-client-5.7.41-1.el7.x86_64.rpm yum -y install net-tools rpm -ivh mysql-community-server-5.7.41-1.el7.x86_64.rpm # 查询 MySQL 的安装文件 rpm -qa | grep -i mysql # yum remove openssl-devel # yum remove net-tools
问题1
[root@localhost mysql-5.7.41]# rpm -ivh mysql-community-libs-5.7.41-1.el7.x86_64.rpm
警告:mysql-community-libs-5.7.41-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
错误:依赖检测失败:
mysql-community-common(x86-64) >= 5.7.9 被 mysql-community-libs-5.7.41-1.el7.x86_64 需要
mariadb-libs 被 mysql-community-libs-5.7.41-1.el7.x86_64 取代
解决方案1:清除之前安装过的依赖即可,最后重新安装下。
[root@localhost mysql-5.7.41]# yum remove mysql-libs
问题2
[root@localhost mysql-5.7.41]# rpm -ivh mysql-community-server-5.7.41-1.el7.x86_64.rpm
警告:mysql-community-server-5.7.41-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
错误:依赖检测失败:
net-tools 被 mysql-community-server-5.7.41-1.el7.x86_64 需要
解决方案2:
yum -y install net-tools
[root@localhost mysql-5.7.41]# rpm -ivh mysql-community-common-5.7.41-1.el7.x86_64.rpm 警告:mysql-community-common-5.7.41-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY 准备中... ################################# [100%] 正在升级/安装... 1:mysql-community-common-5.7.41-1.e################################# [100%] [root@localhost mysql-5.7.41]# [root@localhost mysql-5.7.41]# rpm -ivh mysql-community-libs-5.7.41-1.el7.x86_64.rpm 警告:mysql-community-libs-5.7.41-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY 准备中... ################################# [100%] 正在升级/安装... 1:mysql-community-libs-5.7.41-1.el7################################# [100%] [root@localhost mysql-5.7.41]# [root@localhost mysql-5.7.41]# rpm -ivh mysql-community-libs-compat-5.7.41-1.el7.x86_64.rpm 警告:mysql-community-libs-compat-5.7.41-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY 准备中... ################################# [100%] 正在升级/安装... 1:mysql-community-libs-compat-5.7.4################################# [100%] [root@localhost mysql-5.7.41]# [root@localhost mysql-5.7.41]# rpm -ivh mysql-community-devel-5.7.41-1.el7.x86_64.rpm 警告:mysql-community-devel-5.7.41-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY 准备中... ################################# [100%] 正在升级/安装... 1:mysql-community-devel-5.7.41-1.el################################# [100%] [root@localhost mysql-5.7.41]# [root@localhost mysql-5.7.41]# rpm -ivh mysql-community-client-5.7.41-1.el7.x86_64.rpm 警告:mysql-community-client-5.7.41-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY 准备中... ################################# [100%] 正在升级/安装... 1:mysql-community-client-5.7.41-1.e################################# [100%] [root@localhost mysql-5.7.41]# [root@localhost mysql-5.7.41]# rpm -ivh mysql-community-server-5.7.41-1.el7.x86_64.rpm 警告:mysql-community-server-5.7.41-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY 准备中... ################################# [100%] 正在升级/安装... 1:mysql-community-server-5.7.41-1.e################################# [100%]
# 启动 MySQL 服务
systemctl start mysqld
# 重启 MySQL 服务
systemctl restart mysqld
# 关闭 MySQL 服务
systemctl stop mysqld
#设置开机自启
systemctl enable mysqld
# 查看状态
systemctl status mysqld
rpm 安装 MySQL 会自动生成一个随机密码
grep 'temporary password' /var/log/mysqld.log
cat /var/log/mysqld.log
A temporary password is generated for root@localhost: 2yiNb5Pvum<4
账号:root
随机密码:2yiNb5Pvum<4
# 登录 mysql -u root -p 2yiNb5Pvum<4 # 修改密码 ALTER USER 'root'@'localhost' IDENTIFIED BY '123456'; ALTER USER 'root'@'localhost' IDENTIFIED BY 'Mysql.123456'; # 设置密码的验证强度等级 #Mysql 5.7 版本 # 将密码复杂度校验调整简单类型 # set global validate_password_policy=MEDIUM; set global validate_password_policy=LOW; # 设置密码最少位数限制为 4 位 set global validate_password_length=4; # 查看 mysql 初始的密码策略 SHOW VARIABLES LIKE 'validate_password%';
# 查看 mysql5.7 初始的密码策略
mysql> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+--------+
| Variable_name | Value |
+--------------------------------------+--------+
| validate_password_check_user_name | OFF |
| 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 |
+--------------------------------------+--------+
自己安装在本地的mysql默认时不允许进行远程登陆,如果想要远程访问本地的数据库需要修改访问权限,将’root‘访问权限修改为’%‘
# 连接服务器 mysql -u root -p # 看当前所有数据库 show databases; # 进入mysql数据库 use mysql; # 查看mysql数据库中所有的表 show tables; # 执行更新权限语句 # 其中“%”代表所有的地址,意味着所有的地址都可以访问“root” update user set Host='%' where User='root'; # 查看权限是否修改成功 select host,user from user; select host,user,plugin,authentication_string from user; # 刷新服务器配置 FLUSH PRIVILEGES;
# 停止Mysql systemctl stop mysqld # 查询 MySQL 的安装文件 rpm -qa | grep -i mysql # 卸载上述查询的所有安装包 rpm -e mysql-community-libs-5.7.41-1.el7.x86_64 --nodeps rpm -e mysql-community-devel-5.7.41-1.el7.x86_64 --nodeps rpm -e mysql-community-common-5.7.41-1.el7.x86_64 --nodeps rpm -e mysql-community-libs-compat-5.7.41-1.el7.x86_64 --nodeps rpm -e mysql-community-client-5.7.41-1.el7.x86_64 --nodeps rpm -e mysql-community-server-5.7.41-1.el7.x86_64 --nodeps # 再次查询 MySQL 的安装文件 rpm -qa | grep -i mysql # 删除MySQL的数据存放目录 rm -rf /var/lib/mysql # 找出服务器中分散的mysql文件夹,查询残留的文件和相关依赖,并删除 find / -name mysql rm -rf [显示的文件夹路径,每个绝对路径中间用空格隔开] # MySQL配置文件通常位于/etc/my.cnf或/etc/mysql/my.cnf下,MySQL日志文件通常位于/var/log/mysql下 # 删除mysql日志 rm -rf /var/log/mysqld.log # 删除MySQL的配置文件备份 rm -rf /etc/my.cnf.rpmsave # 删除mysql配置文件 rm -rf /etc/mysql/my.cnf rm -rf /etc/my.cnf # 清除MySQL用户和组 userdel mysql groupdel mysql
rpm -qa | grep mysql
Mysql官网:https://www.mysql.com/
下载网址:https://downloads.mysql.com/archives/community/
cat /etc/redhat-release
首先使用第三方工具上传
# 创建一个文件夹,存放mysql
mkdir -p /usr/mysql
cd /usr/mysql
# 在当前目录下(mysql)下创建一个 mysql-5.7.41 文件夹
mkdir mysql-8.0.34
# 解压安装包到该目录下
tar -xvf mysql-8.0.34-1.el7.x86_64.rpm-bundle.tar -C mysql-8.0.34
[root@localhost mysql-8.0.34]# ls
mysql-community-client-8.0.34-1.el7.x86_64.rpm
mysql-community-debuginfo-8.0.34-1.el7.x86_64.rpm
mysql-community-icu-data-files-8.0.34-1.el7.x86_64.rpm
mysql-community-server-8.0.34-1.el7.x86_64.rpm
mysql-community-client-plugins-8.0.34-1.el7.x86_64.rpm
mysql-community-devel-8.0.34-1.el7.x86_64.rpm
mysql-community-libs-8.0.34-1.el7.x86_64.rpm
mysql-community-server-debug-8.0.34-1.el7.x86_64.rpm
mysql-community-common-8.0.34-1.el7.x86_64.rpm
mysql-community-embedded-compat-8.0.34-1.el7.x86_64.rpm
mysql-community-libs-compat-8.0.34-1.el7.x86_64.rpm
mysql-community-test-8.0.34-1.el7.x86_64.rpm
解压后的文件都是 rpm 文件,因此需要用到 rpm 包资源管理器相关的指令安装这些 rpm 的安装包
yum install openssl-devel
安装完该插件之后,依次执行以下命令安装这些 rpm 包
yum install openssl-devel # yum install net-tools rpm -ivh mysql-community-common-8.0.34-1.el7.x86_64.rpm rpm -ivh mysql-community-client-plugins-8.0.34-1.el7.x86_64.rpm rpm -ivh mysql-community-libs-8.0.34-1.el7.x86_64.rpm rpm -ivh mysql-community-libs-compat-8.0.34-1.el7.x86_64.rpm rpm -ivh mysql-community-devel-8.0.34-1.el7.x86_64.rpm # 至此所有依赖已安装完毕,下面安装客户端和服务端 rpm -ivh mysql-community-client-8.0.34-1.el7.x86_64.rpm rpm -ivh mysql-community-icu-data-files-8.0.34-1.el7.x86_64.rpm rpm -ivh mysql-community-server-8.0.34-1.el7.x86_64.rpm # 查询 MySQL 的安装文件 rpm -qa | grep -i mysql # yum remove openssl-devel # yum remove net-tools
# 启动 MySQL 服务
systemctl start mysqld
# 重启 MySQL 服务
systemctl restart mysqld
# 关闭 MySQL 服务
systemctl stop mysqld
#设置开机自启
systemctl enable mysqld
# 查看状态
systemctl status mysqld
rpm 安装 MySQL 会自动生成一个随机密码
grep 'temporary password' /var/log/mysqld.log
cat /var/log/mysqld.log
A temporary password is generated for root@localhost: ij9uN+hmchw2
账号:root
随机密码:ij9uN+hmchw2
# 登录 mysql -u root -p ij9uN+hmchw2 # 修改密码 ALTER USER 'root'@'localhost' IDENTIFIED BY '123456'; ALTER USER 'root'@'localhost' IDENTIFIED BY 'Mysql.123456'; # 设置密码的验证强度等级 #Mysql 8.0 版本 # 将密码复杂度校验调整简单类型 # set global validate_password.policy=MEDIUM; set global validate_password.policy=LOW; # 设置密码最少位数限制为 4 位 set global validate_password.length=4; # 查看 mysql 初始的密码策略 SHOW VARIABLES LIKE 'validate_password%';
mysql> SHOW VARIABLES LIKE 'validate_password%';
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
提示ERROR 1820,让我们先修改密码后才能查看规则。
# 查看 mysql8.0 初始的密码策略 mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Mysql.123456'; Query OK, 0 rows affected (0.01 sec) mysql> SHOW VARIABLES LIKE 'validate_password%'; +-------------------------------------------------+--------+ | Variable_name | Value | +-------------------------------------------------+--------+ | validate_password.changed_characters_percentage | 0 | | 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 | +-------------------------------------------------+--------+ 8 rows in set (0.01 sec)
自己安装在本地的mysql默认时不允许进行远程登陆,如果想要远程访问本地的数据库需要修改访问权限,将’root‘访问权限修改为’%‘
# 连接服务器 mysql -u root -p # 看当前所有数据库 show databases; # 进入mysql数据库 use mysql; # 查看mysql数据库中所有的表 show tables; # 执行更新权限语句 # 其中“%”代表所有的地址,意味着所有的地址都可以访问“root” update user set Host='%' where User='root'; # 查看权限是否修改成功 select host,user from user; select host,user,plugin,authentication_string from user; # 刷新服务器配置 FLUSH PRIVILEGES;
方法一:
# 登录MySQL后输入
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'YourPassword';
FLUSH PRIVILEGES;
方法二:
# 编辑my.cnf文件,更改默认的身份认证插件。
vi /etc/my.cnf
# 在[mysqld]中添加下边的代码
default_authentication_plugin=mysql_native_password
# 然后重启MySQL
service mysqld restart
# 停止Mysql systemctl stop mysqld # 查询 MySQL 的安装文件 rpm -qa | grep -i mysql # 卸载上述查询的所有安装包 rpm -e mysql-community-client-plugins-8.0.34-1.el7.x86_64 --nodeps rpm -e mysql-community-libs-compat-8.0.34-1.el7.x86_64 --nodeps rpm -e mysql-community-client-8.0.34-1.el7.x86_64 --nodeps rpm -e mysql-community-server-8.0.34-1.el7.x86_64 --nodeps rpm -e mysql-community-common-8.0.34-1.el7.x86_64 --nodeps rpm -e mysql-community-libs-8.0.34-1.el7.x86_64 --nodeps rpm -e mysql-community-devel-8.0.34-1.el7.x86_64 --nodeps rpm -e mysql-community-icu-data-files-8.0.34-1.el7.x86_64 --nodeps # 再次查询 MySQL 的安装文件 rpm -qa | grep -i mysql # 找出服务器中分散的mysql文件夹,查询残留的文件和相关依赖,并删除 find / -name mysql rm -rf [显示的文件夹路径,每个绝对路径中间用空格隔开] # MySQL配置文件通常位于/etc/my.cnf或/etc/mysql/my.cnf下,MySQL日志文件通常位于/var/log/mysql下 # 删除mysql日志 rm -rf /var/log/mysqld.log # 删除MySQL的配置文件备份 rm -rf /etc/my.cnf.rpmsave # 删除mysql配置文件 rm -rf /etc/mysql/my.cnf rm -rf /etc/my.cnf # 清除MySQL用户和组 userdel mysql groupdel mysql
# linux8 yum源
https://dev.mysql.com/get/mysql80-community-release-el8-9.noarch.rpm
# linux7 yum源
https://dev.mysql.com/get/mysql80-community-release-el7-11.noarch.rpm
# linux6 yum源
https://dev.mysql.com/get/mysql80-community-release-el6-10.noarch.rpm
yum list | grep mysql*
yum list mysql*
# 确保系统中已经安装了wget和rpm工具
sudo yum install wget rpm
# linux8 yum源
wget https://dev.mysql.com/get/mysql80-community-release-el8-9.noarch.rpm
# linux7 yum源
wget https://dev.mysql.com/get/mysql80-community-release-el7-11.noarch.rpm
# linux6 yum源
wget https://dev.mysql.com/get/mysql80-community-release-el6-10.noarch.rpm
cd /etc/yum.repos.d
rpm -ivh mysql80-community-release-el7-11.noarch.rpm
vi mysql-community.repo
# 安装方式一
yum -y install mysql-community-server
# 安装方式二
# 安装MySQL服务器、MySQL客户端和MySQL依赖库
yum -y install mysql-server mysql mysql-libs
# 安装方式三
# 只想安装MySQL服务器
yum -y install mysql-server
# 查看Mysql版本信息
mysql --version
# 启动mysql
systemctl start mysqld
# 查看mysql状态信息
systemctl status mysqld
grep 'temporary password' /var/log/mysqld.log
cat /var/log/mysqld.log
# 停止MySQL服务 systemctl stop mysqld # 查询 MySQL 的安装文件 rpm -qa | grep -i mysql # 删除已安装mysql yum -y remove mysql80-community-release-el7-11.noarch yum -y remove mysql-community-common-5.7.44-1.el7.x86_64 yum -y remove mysql-community-libs-compat-5.7.44-1.el7.x86_64 yum -y remove mysql-community-server-5.7.44-1.el7.x86_64 yum -y remove mysql-community-libs-5.7.44-1.el7.x86_64 yum -y remove mysql-community-client-5.7.44-1.el7.x86_64 # 查询 MySQL 的安装文件 rpm -qa | grep -i mysql # 查看mysql相关的文件目录,并删除 find / -name mysql rm -rf [显示的文件夹路径,每个绝对路径中间用空格隔开] # MySQL配置文件通常位于/etc/my.cnf或/etc/mysql/my.cnf下,MySQL日志文件通常位于/var/log/mysql下 # 删除mysql日志 rm -rf /var/log/mysqld.log # 删除MySQL的配置文件备份 rm -rf /etc/my.cnf.rpmsave # 删除mysql配置文件 rm -rf /etc/mysql/my.cnf rm -rf /etc/my.cnf # 清除MySQL用户和组 userdel mysql groupdel mysql cd ~ find / -name mysql*
mysql_secure_installation 主要做了以下工作
为 root 用户设置密码
删除匿名账号
取消 root 用户远程登录
删除 test 库和对 test 库的访问权限
刷新授权表使修改生效
mysql_secure_installation
新密码,长度为8至30个字符,必须同时包含大小写英文字母、数字和特殊符号。特殊符号可以是()` ~!@#$%^&*-+=|{}[]:;‘<>,.?/
linux中mysql5.7默认的字符集是latin1,在插入中文时会报错,通常是由于数据库字符集和字符串编码不匹配导致的。
所以一般在配置好mysql时需要修改字符集为utf8【又叫utfmb3,一般开发够用,一个字符用3个字节表示】
或者utfmb4【一个字符用4个字节表示,如果存储emoji表情,必须用utfmb4】
show databases; CREATE DATABASE IF NOT EXISTS school CHARACTER SET utf8mb4; use school; DROP TABLE IF EXISTS user; CREATE TABLE `user` ( `id` int(11) PRIMARY KEY NOT NULL AUTO_INCREMENT COMMENT '主键', `name` varchar(50) NOT NULL COMMENT '姓名' ) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='用户表'; -- ALTER TABLE user AUTO_INCREMENT = 1; INSERT INTO user (name) VALUES('张三'),('李四'),('lily'); select * from user; -- 查询表结构 desc user; -- 表级 show table status from school like '%use%'; -- 列级 show full columns from user; -- 查询指定表的建表语句 show create table user;
不
指定字符集DROP TABLE IF EXISTS customer; CREATE TABLE customer( id INT AUTO_INCREMENT, name VARCHAR(200), PRIMARY KEY(id) ); INSERT INTO customer (id,name) VALUES(1,'张三'),(2,'李四'),(3,'lily'); # ERROR 1366 (HY000): Incorrect string value: '\xE5\xBC\xA0\xE4\xB8\x89' for column 'NAME' at row 1 -- 表级 show table status from school like '%customer%'; -- 列级 show full columns from customer; -- 查询指定表的建表语句 show create table customer;
show charset;
show char set;
mysql> show char set; +----------+---------------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+---------------------------------+---------------------+--------+ | big5 | Big5 Traditional Chinese | big5_chinese_ci | 2 | | dec8 | DEC West European | dec8_swedish_ci | 1 | | cp850 | DOS West European | cp850_general_ci | 1 | | hp8 | HP West European | hp8_english_ci | 1 | | koi8r | KOI8-R Relcom Russian | koi8r_general_ci | 1 | | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | latin2 | ISO 8859-2 Central European | latin2_general_ci | 1 | | swe7 | 7bit Swedish | swe7_swedish_ci | 1 | | ascii | US ASCII | ascii_general_ci | 1 | | ujis | EUC-JP Japanese | ujis_japanese_ci | 3 | | sjis | Shift-JIS Japanese | sjis_japanese_ci | 2 | | hebrew | ISO 8859-8 Hebrew | hebrew_general_ci | 1 | | tis620 | TIS620 Thai | tis620_thai_ci | 1 | | euckr | EUC-KR Korean | euckr_korean_ci | 2 | | koi8u | KOI8-U Ukrainian | koi8u_general_ci | 1 | | gb2312 | GB2312 Simplified Chinese | gb2312_chinese_ci | 2 | | greek | ISO 8859-7 Greek | greek_general_ci | 1 | | cp1250 | Windows Central European | cp1250_general_ci | 1 | | gbk | GBK Simplified Chinese | gbk_chinese_ci | 2 | | latin5 | ISO 8859-9 Turkish | latin5_turkish_ci | 1 | | armscii8 | ARMSCII-8 Armenian | armscii8_general_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | cp866 | DOS Russian | cp866_general_ci | 1 | | keybcs2 | DOS Kamenicky Czech-Slovak | keybcs2_general_ci | 1 | | macce | Mac Central European | macce_general_ci | 1 | | macroman | Mac West European | macroman_general_ci | 1 | | cp852 | DOS Central European | cp852_general_ci | 1 | | latin7 | ISO 8859-13 Baltic | latin7_general_ci | 1 | | utf8mb4 | UTF-8 Unicode | utf8mb4_general_ci | 4 | | cp1251 | Windows Cyrillic | cp1251_general_ci | 1 | | utf16 | UTF-16 Unicode | utf16_general_ci | 4 | | utf16le | UTF-16LE Unicode | utf16le_general_ci | 4 | | cp1256 | Windows Arabic | cp1256_general_ci | 1 | | cp1257 | Windows Baltic | cp1257_general_ci | 1 | | utf32 | UTF-32 Unicode | utf32_general_ci | 4 | | binary | Binary pseudo charset | binary | 1 | | geostd8 | GEOSTD8 Georgian | geostd8_general_ci | 1 | | cp932 | SJIS for Windows Japanese | cp932_japanese_ci | 2 | | eucjpms | UJIS for Windows Japanese | eucjpms_japanese_ci | 3 | | gb18030 | China National Standard GB18030 | gb18030_chinese_ci | 4 | +----------+---------------------------------+---------------------+--------+ 41 rows in set (0.00 sec)
# 对新建表、列,新插入数据有效
alter database 数据库名 character set utf8 collate utf8_bin;
#将表字符集设置为utf8
alter table 表名 default character set utf8 collate utf8_bin;
alter table customer character set utf8;
#将想要插入中文的字段的字符集改为utf8
alter table 表名 change 列名 新列名 char(20) character set utf8;
alter table customer change name nikname char(20) character set utf8;
INSERT INTO customer (id,nikname) VALUES(1,'张三'),(2,'李四'),(3,'lily');
#查看表的字符集和字段的字符集
show create table customer;
status;
show variables like "%server%";
show variables like '%char%';
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.02 sec) mysql> show variables like "%server%"; +---------------------------------+--------------------------------------+ | Variable_name | Value | +---------------------------------+--------------------------------------+ | character_set_server | latin1 | | collation_server | latin1_swedish_ci | | innodb_ft_server_stopword_table | | | server_id | 1 | | server_id_bits | 32 | | server_uuid | 72c696aa-cee7-11ee-9fe1-000c29c833e8 | +---------------------------------+--------------------------------------+ 6 rows in set (0.00 sec) # 默认是latin1
# 查找my.cnf配置文件
find / -name my.cnf
vi /etc/my.cnf
my.cnf内容添加
[client]
default_character_set=utf8
[mysqld]
default-storage-engine=INNODB
collation_server = utf8_general_ci
character_set_server = utf8
systemctl restart mysqld
show variables like "%server%";
show variables like '%char%';
ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated
column 'company.emp.ename' which is not functionally dependent on columns in GROUP BY clause;
this is incompatible with sql_mode=only_full_group_by
mysql 5.7以后默认启用sql_mode=only_full_group_by模式特性
对于GROUP BY聚合操作,如果在SELECT中的列,没有在GROUP BY中出现,那么这个SQL是不合法的,因为列不在GROUP BY从句中,
也就是说查出来的列必须在group by后面出现否则就会报错,或者这个字段出现在聚合函数里面。
--1.查看模式中是否有only_full_group_by
select @@global.sql_mode;
--2.删除only_full_group_by
set @@global.sql_mode
='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
mysql> select @@global.sql_mode;
+-------------------------------------------------------------------------------------------------------------------------------------------+
| @@global.sql_mode |
+-------------------------------------------------------------------------------------------------------------------------------------------+
| ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+-------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
在尾部添加以下内容,重新启动 mysql 即可
#Linux 用户
find / -name my.cnf
vi /etc/my.cnf
service restart mysqld
# 配置my.cnf或my.ini
[mysqld]
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
[client]
default_character_set=utf8
[mysqld]
# 表示永久表(permanent tables)的默认存储引擎
default-storage-engine=INNODB
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
collation_server = utf8_general_ci
character_set_server = utf8
# mysql8.0
default_authentication_plugin=mysql_native_password
ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)
MySQL服务器停止则重新启动即可
密码输入错误
#Linux 用户
find / -name my.cnf
vi /etc/my.cnf
#在[mysqld]中添加skip-grant-tables(登录时跳过权限检查)
skip-grant-tables
# 重启数据库 systemctl restart mysqld # 登录mysql,回车即可 mysql -uroot -p # 进入mysql数据库 use mysql; # 查看权限是否修改成功 select host,user,plugin,authentication_string from user; # 刷新服务器配置 FLUSH PRIVILEGES; # 没有用户创建用户 可能需要刷新 create user 'root'@'localhost' identified by 'Mysql.123456'; # 执行更新权限语句 # 其中“%”代表所有的地址,意味着所有的地址都可以访问“root” update user set Host='%' where User='root'; # 更改密码 update mysql.user set authentication_string ='Mysql.123456' where user="root"; # 更改密码 alter user 'root'@'localhost' identified by 'Mysql.123456'; # 刷新服务器配置 FLUSH PRIVILEGES; exit # 将my.cnf中的skip-grant-tables去除 vi /etc/my.cnf # 重启数据库 systemctl restart mysqld
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。