忘记密码,重置密码
- (管理员方式)打开命令窗口cmd,输入命令:
net stop mysql,停止MySQL服务 - 开启跳过密码验证登录的MySQL服务
输入命令
mysqld --console --skip-grant-tables --shared-memory
或者执行(报错的话 No such file or directory)mysqld --console --skip-grant-tables --shared-memory --datadir="C:\ProgramData\MySQL\MySQL Server 8.0\Data"根据自己实际的MySQL Data(存数据的)文件夹路径设置\ - 再打开一个新的cmd,无密码登录MySQL,输入登录命令:mysql -u root -p
密码置为空,然后输入命令如下:
use mysql
update user set authentication_string='' where user='root';
退出mysql,执行命令:quit
关闭以-console --skip-grant-tables --shared-memory启动的MySQL服务
打开命令框,输入:net start mysql 启动MySQL服务。\ - 密码已经置空,所以无密码状态登录MySQL,输入登录命令:
mysql -u root -p
修改密码 (说明:为什么不直接执行这个改密码呢?当 MySQL 服务器以--skip-grant-tables选项运行时,它启动了在不使用授权系统表的情况下运作的模式。这意味着没有密码验证,任何用户都可以连接并且具有所有权限。但是,这也限制了一些操作,特别是那些修改权限表(例如:mysql.user)的操作。所以执行下面命令会报错The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement)
ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';
或
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
最后刷新
FLUSH PRIVILEGES;
验证更改后密码正确登录
输入quit,退出当前登录,输入登录命令:mysql -u root -p
输入密码,成功登录,到此,重置密码结束。
说明:
UPDATE user SET authentication_string='' WHERE user='root'; 这个命令执行了直接更新 mysql.user 系统表中的 authentication_string 字段,把它设为一个空字符串,这相当于移除了 'root' 用户的密码。直接对用户表执行 UPDATE 是不建议的做法,因为它可能导致权限问题,一般来说需要在执行这样的更新后运行 FLUSH PRIVILEGES; 来重新加载权限表。此外,将密码设为空是危险的,因为这样做会允许任何人不使用密码登录 root 用户。
ALTER USER 'root'@'localhost' IDENTIFIED BY 'root'; 这个命令是 MySQL 提供的官方方式来修改用户密码。它更安全,因为该命令会自动处理如密码散列以及权限表的重载等方面。
MySQL8.0修改max_allowed_packet
原文链接:关于MySQL8.0修改max_allowed_packet
MySQL max_allowed_packet默认值是4MB。
使用show variables like "%max_allowed_packet%";查看当前大小
1.首先找到my.ini文件
MySQL默认的安装路径是C:\Program Files\MySQL\MySQL Server 8.0,但打开后发现并未找到my.ini的文件
2.接下来去到C盘源目录下,查看隐藏的文件
在这里可以看到有个ProgramData的隐藏文件夹
点进去找到MySQL文件夹
再找到MySQL Server 8.0的文件夹
这时候就能看到my.ini文件了
3.打开my.ini文件
找到以下语句:
接下来就是修改成自己需要的数据大小了(比如30M),改完后保存;重启MySQL服务:
这个时候修改max_allowed_packet大小就完成了。
MySQL- 错误 : su: warning: cannot change directory to/nonexistent: No such file or directory
最佳答案
mysql 用户正在寻找 主目录 ,这似乎没有被分配。
为此,您可以执行:
sudo systemctl stop mysql.service
sudo usermod -d /var/lib/mysql/ mysql
sudo systemctl start mysql.service
或者
sudo service mysql stop
sudo usermod -d /var/lib/mysql/ mysql
sudo service mysql start
Can’t connect to MySQL server on ‘localhost’ (10061)
- 有可能是没启动
- 可能是配置文件问题
将下面这两个打上#号注释掉,配置文件文件可能在/etc/mysql/mysql.conf.d/mysqld.cnf
不同版本下载方式不同可能会不同,上面那个是在Ubuntu下直接通过命令install下载的MySQL 8#bind-address = 127.0.0.1
#mysqlx-bind-address = 127.0.0.1
可自建mysqld.cnf替换 /etc/mysql/mysql.conf.d/mysqld.cnf
#
# The MySQL database server configuration file.
#
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
# Here is entries for some specific programs
# The following values assume you have at least 32M ram
[mysqld]
#
# * Basic Settings
#
user = mysql
# pid-file = /var/run/mysqld/mysqld.pid
# socket = /var/run/mysqld/mysqld.sock
# port = 3306
# datadir = /var/lib/mysql
# If MySQL is running as a replication slave, this should be
# changed. Ref https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_tmpdir
# tmpdir = /tmp
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#bind-address = 127.0.0.1
#mysqlx-bind-address = 127.0.0.1
#
# * Fine Tuning
#
key_buffer_size = 16M
# max_allowed_packet = 64M
# thread_stack = 256K
# thread_cache_size = -1
# This replaces the startup script and checks MyISAM tables if needed
# the first time they are touched
myisam-recover-options = BACKUP
# max_connections = 151
# table_open_cache = 4000
#
# * Logging and Replication
#
# Both location gets rotated by the cronjob.
#
# Log all queries
# Be aware that this log type is a performance killer.
# general_log_file = /var/log/mysql/query.log
# general_log = 1
#
# Error log - should be very few entries.
#
log_error = /var/log/mysql/error.log
#
# Here you can see queries with especially long duration
# slow_query_log = 1
# slow_query_log_file = /var/log/mysql/mysql-slow.log
# long_query_time = 2
# log-queries-not-using-indexes
#
# The following can be used as easy to replay backup logs or for replication.
# note: if you are setting up a replication slave, see README.Debian about
# other settings you may need to change.
# server-id = 1
# log_bin = /var/log/mysql/mysql-bin.log
# binlog_expire_logs_seconds = 2592000
max_binlog_size = 100M
# binlog_do_db = include_database_name
# binlog_ignore_db = include_database_name
varchar 最大长度(字符)
MySQL的varchar数据类型最多能存储多少个utf8中文汉字取决于列的最大长度。在默认情况下,MySQL的varchar列的最大长度为65535个字节。
在utf8中,一个中文汉字占用3个字节。因此,在utf8编码下,如果varchar列的最大长度为n个字符,则最多能存储 n/3 21845 个utf8中文汉字。
在utf8mb4中,一个中文汉字占用4个字节。因此,在utf8编码下,如果varchar列的最大长度为n个字符,则最多能存储 n/4 16383 个utf8中文汉字。
举例来说,如果varchar列的最大长度设置为255,那么最多能存储255/3=85个utf8中文汉字。
IF语句
表达式:IF( expr1 , expr2 , expr3 )
expr1条件,条件为true,则值是expr2 ,false,值就是expr3
SELECT a.*,
IF(a.questionType = '1', m.question,
IF(a.questionType = '2', f.question,
IF(a.questionType = '3', j.question, NULL))) AS question
FROM answer_record a
LEFT JOIN multi_question m ON m.questionId = a.questionId AND a.questionType = '1'
LEFT JOIN fill_question f ON f.questionId = a.questionId AND a.questionType = '2'
LEFT JOIN judge_question j ON j.questionId = a.questionId AND a.questionType = '3'
WHERE a.userId = #{userId} and a.answerResult != '1'
ORDER BY a.answerTime DESC
这个语句通过检查a.questionType的值来确定应该返回什么问题。如果a.questionType的值为1,则返回m.question的值;如果a.questionType的值为2,则返回f.question的值;如果a.questionType的值为3,则返回j.question的值;如果a.questionType的值不为1、2或3,则返回NULL。
设置指定IP数访问据库权限
# yourpassword 为你使用root账户登录的密码
CREATE USER 'root'@'113.103.196.212';
ALTER USER 'root'@'113.103.196.212' IDENTIFIED WITH mysql_native_password BY 'yourpassword'; GRANT ALL PRIVILEGES ON *.* TO 'root'@'113.103.196.212' WITH GRANT OPTION;
flush PRIVILEGES;
`CREATE USER 'root'@'113.103.196.212';` 创建了一个用户名为'root',限定只能从IP地址为113.103.196.212的主机进行连接的用户。
`ALTER USER 'root'@'113.103.196.212' IDENTIFIED WITH mysql_native_password BY 'yourpassword';`修改了上面创建的用户的认证方式为mysql_native_password,并设置密码为'yourpassword'。
`GRANT ALL PRIVILEGES ON *.* TO 'root'@'113.103.196.212' WITH GRANT OPTION;`授予该用户在所有数据库和所有表上拥有全权限,并且允许该用户传递(授权)权限给其他用户。
`flush PRIVILEGES;`刷新权限,使修改后的授权立即生效。
#选择使用
授予该用户在所有数据库和所有表上拥有只读权限,并且不允许该用户传递(授权)权限给其他用户的语句如下:
GRANT SELECT ON *.* TO 'root'@'113.103.196.212';
授予该用户在所有数据库和所有表上拥有全权限,并且不允许该用户传递(授权)权限给其他用户的语句如下:
GRANT ALL PRIVILEGES ON *.* TO 'root'@'113.103.196.212';
允许所有远程访问
#进入mysql库
use mysql
#修改账号权限
update user set host='%' where user ='root';
#刷新缓存
FLUSH PRIVILEGES;
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'WITH GRANT OPTION;
select user,host from user;
备份
一、使用 mysqldump 工具备份
#对某些库进行完全备份
mysqldump -uroot -p[密码] [数据库名] > /目录/备份文件名.sql
例:
mysqldump -uroot -p123123 SCHOOL > /opt/test02
#对多个库进行完全备份
mysqldump -uroot -p123123 --databases SCHOOL sys > /opt/test03.sql
#对所有库进行完全备份
mysqldump -uroot -p123123 --all-databases > /opt/test04.sql
#直接备份表结构
mysqldump -uroot -p123123 -d SCHOOL CLASS01 > /opt/test05.sql
ls /opt/
#查看打包命令是否运行成功,已经生成了备份文件
使用 source 命令恢复表 或 导入sql数据
mysql -uroot -p123123
use SCHOOL;
show tables;
drop table CLASS01;
source /opt/test01.sql
#恢复表
show tables;
select * from CLASS01;
#查看