本文是进行一次尝试,目标是在MacOS操作系统(版本:10.15.7 Catalina)上完成一次MySQL源码的编译、运行
说是尝试,是因为网上所有教程几乎都是基于Linux操作系统的,虽说这二者都是Unix系列,但C源码编译安装涉及到许多依赖库,细节上有区别,不完全等价
我参考的教程有
- BILIBILI:怎样从源码安装MySQL数据库,这是一篇讲的比较简单的视频教程,其中有博客地址:Apache服务器基础教程-03从源码安装MySQL
- BILIBILI:MySQL源码包安装方式,这一篇教程对CMake参数讲的比较细致,应该是付费教程的一部分免费章节
先说结果,并不复杂,用Generic Linux版本的源代码,几个命令下来就能安好,但你可能会遇到其他奇怪的error,我本地没有遇到,可以评论留言,看到了会回复
步骤一:下载MySQL源码
打开 https://dev.mysql.com/downloads/mysql/
点击Looking for previous GA versions? 切换到5.7版本
选择SourceCode,选择Generic Linux版本,点击Download
选择No thanks, just start my download.
下载后解压缩包,tar -xzvf mysql-5.7.35.tar.gz,得到一个文件夹:mysql-5.7.35,里面就是源码
步骤二:下载boost包
点击More Downloads…
点击Version History
找到1.59.0,点击Download
为什么是1.59.0?
因为我最开始没有下载boost包,直接在mysql-5.7.35中执行cmake . ,报了如下错误,错误中提醒我需要1.59.0版本的boost
– BOOST_VERSION_NUMBER is
– BOOST_INCLUDE_DIR /usr/local/boost_1_59_0
– LOCAL_BOOST_DIR /usr/local/boost_1_59_0
– LOCAL_BOOST_ZIP LOCAL_BOOST_ZIP-NOTFOUND
– Could not find (the correct version of) boost.
– MySQL currently requires boost_1_59_0CMake Error at cmake/boost.cmake:88 (MESSAGE): You can download it
with -DDOWNLOAD_BOOST=1 -DWITH_BOOST=
怎么知道从哪里下载boost的?
从上面贴的视频教程中知道的,感谢这些up主,帮助我快速解决了问题
点击boost_1_59_0.tar.gz
Your download will start shortly…
我网不是很好,等了很久,才开始下载一个链接,如果你网也不好,可以试试直接下载:https://udomain.dl.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz (建议用迅雷)
下载完后可以得到一个压缩包,然后解压,tar -zxvf boost_1_59_0.tar.gz,得到一个文件夹:boost_1_59_0
视频中都将文件夹放到 /usr/local/ 目录下,那我们也来:sudo mv boost_1_59_0 /usr/local/
步骤三:生成makeFile
到 mysql-5.7.35 文件夹下,执行命令(注意cmake后面有个点)
sudo cmake . -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/boost_1_59_0
如果出现了如下字样Configuring done、Generating done、Build files have been written to: xxx,那就是成功了
如果失败了,请根据报错解决,解决不来请评论
成功后,mysql-5.7.35 文件夹下会多出许多文件,其中就有Makefile,下一步有用
步骤四:编译
到 mysql-5.7.35 文件夹下,执行命令:sudo make
这一步耗时很长,大概30分钟起步,我看到如下结果就是顺利完成了
[100%] Built target sql_embedded
[100%] Built target imported_crypto_location
[100%] Built target imported_openssl_location
[100%] Built target mysqlserver
[100%] Built target mysqltest_embedded
[100%] Built target mysql_client_test_embedded
[100%] Built target mysql_embedded
[100%] Built target my_safe_process
如果失败了,请根据报错解决,解决不来请评论
步骤五:安装
到 mysql-5.7.35 文件夹下,执行命令:sudo make install
同上
执行完后,会默认安装到 /usr/local/mysql 文件夹下
步骤六:启动mysql服务
cd /usr/local/mysql/bin
此时如果直接 sudo mysql,可能会报错:ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’ (2)
参考 https://stackoverflow.com/questions/15450091/error-2002-hy000-cant-connect-to-local-mysql-server-through-socket-tmp-mys,是由于没有启动mysql服务,而启动服务需要用mysqld命令
执行 sudo mysqld,会报如下错,是由于不允许直接通过root启动,但不通过root又会报权限错误,查了google,可以添加参数 --user=root 解决
2021-09-06T13:14:35.282085Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-09-06T13:14:35.282450Z 0 [Note] --secure-file-priv is set to NULL. Operations related to importing and exporting data are disabled
2021-09-06T13:14:35.282507Z 0 [Note] /usr/local/mysql/bin/mysqld (mysqld 5.7.35) starting as process 76402 ...
2021-09-06T13:14:35.287356Z 0 [Warning] Can't create test file /usr/local/mysql/data/peteryuanpandeMacBook-Pro.lower-test
2021-09-06T13:14:35.287406Z 0 [Warning] Can't create test file /usr/local/mysql/data/peteryuanpandeMacBook-Pro.lower-test
2021-09-06T13:14:35.287477Z 0 [ERROR] Fatal error: Please read "Security" section of the manual to find out how to run mysqld as root!
2021-09-06T13:14:35.287505Z 0 [ERROR] Aborting
2021-09-06T13:14:35.287547Z 0 [Note] Binlog end
2021-09-06T13:14:35.287611Z 0 [Note] /usr/local/mysql/bin/mysqld: Shutdown complete
再执行:sudo mysqld --user=root
我这里又遇到了错,参考https://stackoverflow.com/questions/34516664/cant-open-and-lock-privilege-tables-table-mysql-user-doesnt-exist,需要先initialize一下mysql服务
2021-09-06T13:18:33.080446Z 0 [ERROR] Fatal error: Can't open and lock privilege tables: Table 'mysql.user' doesn't exist
2021-09-06T13:18:33.080460Z 0 [ERROR] Fatal error: Failed to initialize ACL/grant/time zones structures or failed to remove temporary table files.
2021-09-06T13:18:33.080676Z 0 [ERROR] Aborting
...
2021-09-06T13:18:34.225160Z 0 [Note] mysqld: Shutdown complete
执行:sudo mysqld --user=root --initialize,这次很成功,从日志看初始化了root用户,且提供了默认密码
2021-09-06T13:19:44.280779Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-09-06T13:19:44.282581Z 0 [Warning] Setting lower_case_table_names=2 because file system for /usr/local/mysql/data/ is case insensitive
2021-09-06T13:19:44.357944Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-09-06T13:19:44.373054Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-09-06T13:19:44.429720Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 19dd289c-0f15-11ec-bae4-ca07c5c58b68.
2021-09-06T13:19:44.464877Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-09-06T13:19:44.627283Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2021-09-06T13:19:44.627296Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2021-09-06T13:19:44.627807Z 0 [Warning] CA certificate ca.pem is self signed.
2021-09-06T13:19:44.634909Z 1 [Note] A temporary password is generated for root@localhost: 9Yu(Ui&J/zpU
再执行:sudo mysqld --user=root,这次看起来OK了,进程没有结束,应该是服务启动起来了
2021-09-06T13:20:42.118240Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-09-06T13:20:42.118578Z 0 [Note] --secure-file-priv is set to NULL. Operations related to importing and exporting data are disabled
2021-09-06T13:20:42.118617Z 0 [Note] mysqld (mysqld 5.7.35) starting as process 76712 ...
2021-09-06T13:20:42.121555Z 0 [Warning] Setting lower_case_table_names=2 because file system for /usr/local/mysql/data/ is case insensitive
2021-09-06T13:20:42.123252Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2021-09-06T13:20:42.123268Z 0 [Note] InnoDB: Uses event mutexes
2021-09-06T13:20:42.123273Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
2021-09-06T13:20:42.123276Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
2021-09-06T13:20:42.123529Z 0 [Note] InnoDB: Number of pools: 1
2021-09-06T13:20:42.123637Z 0 [Note] InnoDB: Using CPU crc32 instructions
2021-09-06T13:20:42.125171Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
2021-09-06T13:20:42.135336Z 0 [Note] InnoDB: Completed initialization of buffer pool
2021-09-06T13:20:42.151940Z 0 [Note] InnoDB: Highest supported file format is Barracuda.
2021-09-06T13:20:42.158439Z 0 [Note] InnoDB: Creating shared tablespace for temporary tables
2021-09-06T13:20:42.158626Z 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
2021-09-06T13:20:42.166477Z 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
2021-09-06T13:20:42.167213Z 0 [Note] InnoDB: 96 redo rollback segment(s) found. 96 redo rollback segment(s) are active.
2021-09-06T13:20:42.167221Z 0 [Note] InnoDB: 32 non-redo rollback segment(s) are active.
2021-09-06T13:20:42.167417Z 0 [Note] InnoDB: Waiting for purge to start
2021-09-06T13:20:42.220627Z 0 [Note] InnoDB: 5.7.35 started; log sequence number 2749015
2021-09-06T13:20:42.221193Z 0 [Note] InnoDB: Loading buffer pool(s) from /usr/local/mysql/data/ib_buffer_pool
2021-09-06T13:20:42.221486Z 0 [Note] Plugin 'FEDERATED' is disabled.
2021-09-06T13:20:42.227480Z 0 [Note] InnoDB: Buffer pool(s) load completed at 210906 21:20:42
2021-09-06T13:20:42.239996Z 0 [Note] Found ca.pem, server-cert.pem and server-key.pem in data directory. Trying to enable SSL support using them.
2021-09-06T13:20:42.240024Z 0 [Note] Skipping generation of SSL certificates as certificate files are present in data directory.
2021-09-06T13:20:42.240072Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2021-09-06T13:20:42.240086Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2021-09-06T13:20:42.241174Z 0 [Warning] CA certificate ca.pem is self signed.
2021-09-06T13:20:42.241242Z 0 [Note] Skipping generation of RSA key pair as key files are present in data directory.
2021-09-06T13:20:42.250937Z 0 [Note] Server hostname (bind-address): '*'; port: 3306
2021-09-06T13:20:42.251063Z 0 [Note] IPv6 is available.
2021-09-06T13:20:42.251082Z 0 [Note] - '::' resolves to '::';
2021-09-06T13:20:42.251164Z 0 [Note] Server socket created on IP: '::'.
2021-09-06T13:20:42.266987Z 0 [Note] Event Scheduler: Loaded 0 events
2021-09-06T13:20:42.267095Z 0 [Note] mysqld: ready for connections.
Version: '5.7.35' socket: '/tmp/mysql.sock' port: 3306 Source distribution
2021-09-06T13:20:55.147335Z 2 [Note] Access denied for user 'xxx'@'localhost' (using password: NO)
步骤七:登录mysql
执行:mysql -u root -p
密码输入上面提供的初始密码
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.35
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.
mysql>
成功了!成功了!
执行show databases,提示我们必须更换一次密码才能使用,接下来的步骤就不写了,是普通的mysql命令使用方式
mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.