Download source code
Download the source code directly from github. git address: https://github.com/mysql/mysql-server
download path such as: /work/mysql-server
Compile
rely
The mac system depends on the environment cmake, boost
cmake installation
brew install cmake
Boost download: https://dl.bintray.com/boostorg/release/1.72.0/source/boost_1_72_0.tar.gz
Compile
##mysql directory
cd /work/mysql-server
mkdir build && cd build
execute cmake
cmake .. -DCMAKE_INSTALL_PREFIX=/Users/softwork/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DENABLED_LOCAL_INFILE=ON \
-DWITH_INNODB_MEMCACHED=ON \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DDOWNLOAD_BOOST=1 \
-DWITH_BOOST=/work/mysql-server/mysql-server/boost/boost_1_72_0
DCMAKE_INSTALL_PREFIX is the installation directory of mysql, specify by yourself, DWITH_BOOST is the download path of boost
Execute make
make & make install
Add my.cnf configuration
[client]
port = 3306
socket = /Users/data/mysql/mysql.sock
[mysqld]
#Mysql服务的唯一编号 每个mysql服务Id需唯一
server-id = 1
#服务端口号 默认3306
port = 3306
#mysql安装根目录
basedir = /Users/softwork/mysql
#mysql数据文件所在位置
datadir = /Users/data/mysql/data
#pid
pid-file = /Users/data/mysql/mysql.pid
#设置socke文件所在目录
socket = /Users/data/mysql/mysql.sock
#设置临时目录
tmpdir = /Users/data/mysql/tmp
# 用户
#user = mysql
# 允许访问的IP网段
bind-address = 0.0.0.0
# 跳过密码登录
#skip-grant-tables
#主要用于MyISAM存储引擎,如果多台服务器连接一个数据库则建议注释下面内容
skip-external-locking
#只能用IP地址检查客户端的登录,不用主机名
skip_name_resolve = 1
#事务隔离级别,默认为可重复读,mysql默认可重复读级别(此级别下可能参数很多间隙锁,影响性能)
transaction_isolation = READ-COMMITTED
#数据库默认字符集,主流字符集支持一些特殊表情符号(特殊表情符占用4个字节)
character-set-server = utf8mb4
#数据库字符集对应一些排序等规则,注意要和character-set-server对应
collation-server = utf8mb4_general_ci
#设置client连接mysql时的字符集,防止乱码
init_connect='SET NAMES utf8mb4'
#是否对sql语句大小写敏感,1表示不敏感
lower_case_table_names = 1
#最大连接数
max_connections = 400
#最大错误连接数
max_connect_errors = 1000
#TIMESTAMP如果没有显示声明NOT NULL,允许NULL值
explicit_defaults_for_timestamp = true
#SQL数据包发送的大小,如果有BLOB对象建议修改成1G
max_allowed_packet = 128M
#MySQL连接闲置超过一定时间后(单位:秒)将会被强行关闭
#MySQL默认的wait_timeout 值为8个小时, interactive_timeout参数需要同时配置才能生效
interactive_timeout = 1800
wait_timeout = 1800
#内部内存临时表的最大值 ,设置成128M。
#比如大数据量的group by ,order by时可能用到临时表,
#超过了这个值将写入磁盘,系统IO压力增大
tmp_table_size = 134217728
max_heap_table_size = 134217728
#禁用mysql的缓存查询结果集功能
#后期根据业务情况测试决定是否开启
#大部分情况下关闭下面两项
#query_cache_size = 0
#query_cache_type = 0
#数据库错误日志文件
log_error = /Users/data/mysql/logs/error.log
#慢查询sql日志设置
slow_query_log = 1
slow_query_log_file = slow.log
#检查未使用到索引的sql
log_queries_not_using_indexes = 1
#针对log_queries_not_using_indexes开启后,记录慢sql的频次、每分钟记录的条数
log_throttle_queries_not_using_indexes = 5
#作为从库时生效,从库复制中如何有慢sql也将被记录
log_slow_slave_statements = 1
#慢查询执行的秒数,必须达到此值可被记录
long_query_time = 8
#检索的行数必须达到此值才可被记为慢查询
min_examined_row_limit = 100
#mysql binlog日志文件保存的过期时间,过期后自动删除
#expire_logs_days = 5
Initialize the database
cd to mysql to the installation directory
/Users/softwork/mysql/bin to
initialize, initialize should be placed after defaults-file
./mysqld --defaults-file=/Users/lhj/work/mysql-server/my.cnf --initialize --user=user
Note: datadir = /Users/data/mysql/data, the directory before data may need to be created manually, data cannot be manually created, it will be created during initialization
Source code debugging
I use Visual Studio Code, download address: https://visualstudio.microsoft.com/zh-hans/ After
downloading, unzip it and you can
vs code basic panel
Import and download to the mysql source code. When importing, it will prompt to download the necessary plug-in.
Install the debug plug-in
debug configuration
Lanuch configuration
- type is the plugin type
- program compiles mysql source code to mysql startup directory
- args is the parameters needed for startup, here specify the my.cnf directory and startup user, or specify the user in my.cnf
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Launch",
"program": "/Users/softwork/mysql/bin/mysqld",
"args": ["--defaults-file=/work/mysql-server/my.cnf","--user=user"],
}
]
}
Configured to save
debug
Everything is ok, here is not going away
At this point, you can open the debug mode happily
mysql start the main function: mysqld_main method of sql/mysqld.cc