一、安装包路径
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.37-el7-x86_64.tar.gz .
二、安装解压
tar -xvf mysql-5.7.37-el7-x86_64.tar.gz -C ~ mv -v mysql-5.7.37-el7-x86_64 mysql
echo "PATH=$HOME/mysql/bin:$PATH" >> $HOME/.bash_profile
source $HOME/.bash_profile
三、初始化
mkdir ~/data
mysql --help|grep my.cnf|grep etc|xargs rm -rvf # 初始化之前,查找my.cnf文件并删除
mysqld --initialize-insecure --user=$(whoami) --basedir=$HOME/mysql --datadir=$HOME/data
四、启动
mysqld --user=mantis --basedir=$HOME/mysql --datadir=$HOME/data
五、数据库连接方式
1.TCP/IP连接
mysql -h 127.0.0.1 -P3306
2.命令行
mysql -S ~/mysql/mysql.sock
六、问题
1.本地连接服务端报错
$ mysql ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
错误原因: 默认连接localhost,配置文件中未指定sock文件,默认使用/tmp/mysql.sock文件。
查看my.cnf文件: mysql --help|grep my.cnf
解决方案:修改my.cnf文件。新增
[mysql]
socket=/app/mantis/mysql/mysql.sock
2.远程连接服务端报错
root用户的 host为localhost。需要修改为'%',才能支持远程登录.
[mantis@aliyun mysql]$ mysql -h127.0.0.1 -P3306 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 24 Server version: 5.7.37 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> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec) 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> select * from user where user='root'\G; *************************** 1. row *************************** Host: localhost User: root Select_priv: Y Insert_priv: Y Update_priv: Y Delete_priv: Y Create_priv: Y Drop_priv: Y Reload_priv: Y Shutdown_priv: Y Process_priv: Y File_priv: Y Grant_priv: Y References_priv: Y Index_priv: Y Alter_priv: Y Show_db_priv: Y Super_priv: Y Create_tmp_table_priv: Y Lock_tables_priv: Y Execute_priv: Y Repl_slave_priv: Y Repl_client_priv: Y Create_view_priv: Y Show_view_priv: Y Create_routine_priv: Y Alter_routine_priv: Y Create_user_priv: Y Event_priv: Y Trigger_priv: Y Create_tablespace_priv: Y ssl_type: ssl_cipher: x509_issuer: x509_subject: max_questions: 0 max_updates: 0 max_connections: 0 max_user_connections: 0 plugin: mysql_native_password authentication_string: password_expired: N password_last_changed: 2022-04-01 10:20:11 password_lifetime: NULL account_locked: N 1 row in set (0.00 sec) ERROR: No query specified mysql> update user set host='%' where user='root'; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
七、查询历史sql记录
在MySQL中,您可以通过查询information_schema.processlist
表来获取当前的SQL执行记录。但请注意,information_schema.processlist
表仅显示当前正在执行的进程。如果您需要查看历史SQL执行记录,可以开启MySQL的慢查询日志或者通过general_log
来记录所有执行的语句。
-
慢查询日志(MySQL 5.1及以上版本):
-- 查看慢查询日志是否开启及位置
SHOW VARIABLES LIKE 'slow_query_log_file';
-- 开启慢查询日志
SET GLOBAL slow_query_log = 'ON';
-- 设置慢查询阈值(例如,设置为10秒)
SET GLOBAL long_query_time = 10;
慢查询日志文件中将记录执行时间超过阈值的SQL语句。
-
general_log:
-- 查看general_log是否开启
SHOW VARIABLES LIKE 'general_log';
-- 开启general_log
SET GLOBAL general_log = 'ON';
general_log将记录所有MySQL服务器接收到的语句。
开启这些日志之后,就可以通过读取对应的日志文件来获取SQL执行记录。
如果需要查看当前正在执行的查询,可以使用以下SQL语句:
SELECT * FROM information_schema.processlist WHERE command != 'Sleep';
请注意,如果执行时间较长或者执行频率很高,这些日志文件可能会非常大,需要适当管理。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· Qt个人项目总结 —— MySQL数据库查询与断言