Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1791839
  • 博文数量: 335
  • 博客积分: 4690
  • 博客等级: 上校
  • 技术积分: 4341
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-08 21:38
个人简介

无聊之人--除了技术,还是技术,你懂得

文章分类

全部博文(335)

文章存档

2016年(29)

2015年(18)

2014年(7)

2013年(86)

2012年(90)

2011年(105)

分类: Mysql/postgreSQL

2014-05-24 16:15:14

      前两天 参加了一个需求讨论会,有了一点想法,因此打算细细的咀嚼一下mysql的源码,看看你能不能从中受到启发。因此本文的形式有点流水账的形式,但是更多的是将自己的知识形成一个体系。这篇的内容相对来比较简单,但是涉及的东西也比较多,比较杂,对于那些想从源码出发的人可以有一个参考。
本文是在ubuntu平台上,使用mariadb的源码,通过CMAKE工具,编译源码,以及将自己从中遇到的问题进行了一下简单的总结。
需要注意的是本文是多实例mysql编译,即ubuntu自带了一个mysql,我在此基础之上,依据源码有编译了一个mysql实例。
platform :

点击(此处)折叠或打开

  1. ubuntu@:~/Desktop/sc$ cat /etc/lsb-release
  2. DISTRIB_ID=Ubuntu
  3. DISTRIB_RELEASE=12.04
  4. DISTRIB_CODENAME=precise
  5. DISTRIB_DESCRIPTION="Ubuntu 12.04.4 LTS"
  6. ubuntu@:~/Desktop/sc$ uname -r
  7. 3.2.0-61-generic
  8. ubuntu@:~/Desktop/sc$ uname -v
  9. #93-Ubuntu SMP Fri May 2 21:33:33 UTC 2014
mysql source code download url :
wget
unzip&untar

点击(此处)折叠或打开

  1. tar -xvf mariadb-5.5.37.tar.gz
cmake & install 如何定制自己的特定目录,端口

点击(此处)折叠或打开

  1. ubuntu@:~/Desktop/sc/sc5.5.37/bld_debug$ cmake .. \
  2. -DCMAKE_BUILD_TYPE=Debug  \
  3. -DCMAKE_INSTALL_PREFIX=$HOME/ssql \
  4. -DMYSQL_DATADIR=$HOME/ssql/var   \
  5. -DMYSQL_UNIX_ADDR=$HOME/ssql/tmp/mysql.sock \
  6. -DMYSQL_TCP_PORT=3307
  7. -----                                                                                                                                                                                                                                        
  8. -- Configuring done
    -- Generating done
    -- Build files have been written to: /home/ubuntu/Desktop/sc/sc5.3.37/bld_debug
check option of config

点击(此处)折叠或打开

  1. ubuntu@:~/Desktop/sc/sc5.3.37/bld_debug$ cat cmake.check | grep 'prefix'
  2. // Install path prefix, prepended onto install directories.
  3. ubuntu@:~/Desktop/sc/sc5.3.37/bld_debug$ cat cmake.check | grep 'PREFIX'
  4. CMAKE_INSTALL_PREFIX:PATH=/home/ubuntu/ssql
  5. ubuntu@:~/Desktop/sc/sc5.3.37/bld_debug$ cat cmake.check | grep 'DATADIR'
  6. MYSQL_DATADIR:PATH=/home/ubuntu/ssql/var
测试,编译,安装

点击(此处)折叠或打开

  1. make test
  2. make install
version info

点击(此处)折叠或打开

  1. ubuntu@:~/ssql/bin$ ./mysqld -V
  2. ./mysqld Ver 5.5.37-MariaDB-debug for Linux on i686 (Source distribution)
第二实例的mysql授权表的初始化,

点击(此处)折叠或打开

  1. ubuntu@:~/ssql/scripts$ sudo ./mysql_install_db --user=root --datadir=../var --basedir=..
  2. [sudo] password for ubuntu:
  3. Installing MariaDB/MySQL system tables in '../var' ...
  4. 140523 3:06:50 [Warning] Ignoring user change to 'root' because the user was set to 'mysql' earlier on the command line


  5. Installation of system tables failed! Examine the logs in
  6. ../var for more information.

  7. The problem could be conflicting information in an external
  8. my.cnf files. You can ignore these by doing:

  9.     shell> ../scripts/scripts/mysql_install_db --defaults-file=~/.my.cnf

  10. You can also try to start the mysqld daemon with:

  11.     shell> ../bin/mysqld --skip-grant --general-log &

  12. and use the command line tool ../bin/mysql
  13. to connect to the mysql database and look at the grant tables:

  14.     shell> ../bin/mysql -u root mysql
  15.     mysql> show tables

  16. Try 'mysqld --help' if you have problems with paths. Using
  17. --general-log gives you a log in ../var that may be helpful.
针对DATADIR=VAR进行授权

点击(此处)折叠或打开

  1. root@ubuntu-virtual-machine:/home/ubuntu/ssql# cat /etc/group| grep 'mysql'
  2. mysql:x:125:

  3. root@ubuntu-virtual-machine:/home/ubuntu/ssql# cat /etc/passwd | grep 'mysql'
  4. mysql:x:114:125:MySQL Server,,,:/nonexistent:/bin/false

  5. root@ubuntu-virtual-machine:/home/ubuntu/ssql# chown mysql var &&  ls -ali |grep 'var'
    1059652 drwxr--r--  4 mysql  ubuntu   4096 May 23 03:06 var



点击(此处)折叠或打开

  1. ubuntu@:~/ssql/scripts$ sudo ./mysql_install_db --user=mysql --datadir=../var --basedir=.. --no-defaults
  2. Warning: 1024 bytes lost, allocated at mysys/array.c:65, mysys/hash.c:92, mysys/thr_mutex.c:183, mysys/thr_mutex.c:320, psi/mysql_thread.h:618, mysys/my_thr_init.c:339, mysys/my_thr_init.c:197, mysys/my_init.c:104
  3. Warning: 1024 bytes lost, allocated at mysys/array.c:65, mysys/hash.c:92, mysys/thr_mutex.c:178, mysys/thr_mutex.c:320, psi/mysql_thread.h:618, mysys/my_thr_init.c:339, mysys/my_thr_init.c:197, mysys/my_init.c:104
  4. Warning: 112 bytes lost, allocated at mysys/mulalloc.c:51, mysys/thr_mutex.c:160, mysys/thr_mutex.c:320, psi/mysql_thread.h:618, mysys/my_thr_init.c:339, mysys/my_thr_init.c:197, mysys/my_init.c:104, extra/my_print_defaults.c:163
  5. Memory lost: 2160 bytes in 3 chunks
  6. Installing MariaDB/MySQL system tables in '../var' ...
  7. Warning: 1 bytes lost, allocated at mysys/my_new.cc:33, sql/events.cc:826, sql/mysqld.cc:5020, sql/main.cc:26, 0x2d64d3, 0x8175731
  8. Memory lost: 1 bytes in 1 chunks
  9. OK
  10. Filling help tables...
  11. Warning: 1 bytes lost, allocated at mysys/my_new.cc:33, sql/events.cc:826, sql/mysqld.cc:5020, sql/main.cc:26, 0x4e34d3, 0x8175731
  12. Memory lost: 1 bytes in 1 chunks
  13. OK
usage of mysql_install-db:(权限表的特殊之处)
initial grant table && create var dir & & database mysql& test&& my.cnf
according to doc(url;http://dev.mysql.com/doc/refman/5.7/en/unix-postinstallation.html)

点击(此处)折叠或打开

  1. The script also creates privilege table entries for root and anonymous-user accounts. The accounts have no passwords initially.
  2. Briefly, these privileges permit the MySQL root user to do anything, and permit anybody to create or use databases with a name of test or starting with test_.
  3. For a more secure installation, invoke mysql_install_db with the --random-passwords option. This causes it to assign a random password to the MySQL root accounts, set the password expired flag for those accounts, and remove the anonymous-user MySQL accounts.
(verify this later by different mysql,root ,xxxx,and we will see different previllege)

点击(此处)折叠或打开

  1. ubuntu@:~/ssql/bin$ ./mysqld_safe --no-defaults --user=mysql &
  2. ./mysqld_safe: 138: ./mysqld_safe: cannot create /home/ubuntu/ssql/var/ubuntu-virtual-machine.err: Permission denied
  3. rm: cannot remove `/home/ubuntu/ssql/var/ubuntu-virtual-machine.pid': Permission denied
  4. ./mysqld_safe: 1: eval: cannot create /home/ubuntu/ssql/var/ubuntu-virtual-machine.err: Permission denied
  5. touch: cannot touch `/home/ubuntu/ssql/var/ubuntu-virtual-machine.err': Permission denied
  6. chown: cannot access `/home/ubuntu/ssql/var/ubuntu-virtual-machine.err': Permission denied
  7. chmod: cannot access `/home/ubuntu/ssql/var/ubuntu-virtual-machine.err': Permission denied
  8. 140523 03:41:43 mysqld_safe mysqld from pid file /home/ubuntu/ssql/var/ubuntu-virtual-machine.pid ended
  9. ./mysqld_safe: 138: ./mysqld_safe: cannot create /home/ubuntu/ssql/var/ubuntu-virtual-machine.err: Permission denied


点击(此处)折叠或打开

  1. ubuntu@:~/ssql$ sudo chmod -R a+rwx var
  2. ubuntu@:~/ssql$ cd var
  3. ubuntu@:~/ssql/var$ ls
  4. aria_log.00000001 aria_log_control mysql performance_schema test
  5. ubuntu@:~/ssql/var$ ls -lai
  6. total 40
  7. 1059652 drwxrwxrwx 5 mysql ubuntu 4096 May 23 03:39 .
  8.  942314 drwxrwxr-x 15 ubuntu ubuntu 4096 May 23 03:06 ..
  9. 1059102 -rwxrwxrwx 1 mysql mysql 16384 May 23 03:39 aria_log.00000001
  10. 1059101 -rwxrwxrwx 1 mysql mysql 52 May 23 03:39 aria_log_control
  11. 1059665 drwxrwxrwx 2 mysql root 4096 May 23 03:39 mysql
  12. 1059175 drwxrwxrwx 2 mysql mysql 4096 May 23 03:39 performance_schema
  13. 1059667 drwxrwxrwx 2 mysql root 4096 May 23 03:06 test    
  14. ubuntu@:~/ssql/bin$ ./mysqld_safe --no-defaults
  15. Warning: 1024 bytes lost, allocated at mysys/array.c:65, mysys/hash.c:92, mysys/thr_mutex.c:183, mysys/thr_mutex.c:320, psi/mysql_thread.h:618, mysys/my_thr_init.c:339, mysys/my_thr_init.c:197, mysys/my_init.c:104
  16. Warning: 1024 bytes lost, allocated at mysys/array.c:65, mysys/hash.c:92, mysys/thr_mutex.c:178, mysys/thr_mutex.c:320, psi/mysql_thread.h:618, mysys/my_thr_init.c:339, mysys/my_thr_init.c:197, mysys/my_init.c:104
  17. Warning: 112 bytes lost, allocated at mysys/mulalloc.c:51, mysys/thr_mutex.c:160, mysys/thr_mutex.c:320, psi/mysql_thread.h:618, mysys/my_thr_init.c:339, mysys/my_thr_init.c:197, mysys/my_init.c:104, extra/my_print_defaults.c:163
  18. Memory lost: 2160 bytes in 3 chunks
  19. Warning: 1024 bytes lost, allocated at mysys/array.c:65, mysys/hash.c:92, mysys/thr_mutex.c:183, mysys/thr_mutex.c:320, psi/mysql_thread.h:618, mysys/my_thr_init.c:339, mysys/my_thr_init.c:197, mysys/my_init.c:104
  20. Warning: 1024 bytes lost, allocated at mysys/array.c:65, mysys/hash.c:92, mysys/thr_mutex.c:178, mysys/thr_mutex.c:320, psi/mysql_thread.h:618, mysys/my_thr_init.c:339, mysys/my_thr_init.c:197, mysys/my_init.c:104
  21. Warning: 112 bytes lost, allocated at mysys/mulalloc.c:51, mysys/thr_mutex.c:160, mysys/thr_mutex.c:320, psi/mysql_thread.h:618, mysys/my_thr_init.c:339, mysys/my_thr_init.c:197, mysys/my_init.c:104, extra/my_print_defaults.c:163
  22. Memory lost: 2160 bytes in 3 chunks
  23. 140523 03:44:41 mysqld_safe Logging to '/home/ubuntu/ssql/var/ubuntu-virtual-machine.err'.
  24. 140523 03:44:41 mysqld_safe Starting mysqld daemon with databases from /home/ubuntu/ssql/var

点击(此处)折叠或打开

  1. ubuntu@:~/ssql/bin$ ./mysql -uroot -p               [log in to the binary distribution]
  2. Enter password:
  3. Welcome to the MariaDB monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 55
  5. Server version: 5.5.37-0ubuntu0.12.04.1 (Ubuntu)

  6. Copyright (c) 2000, 2014, Oracle, Monty Program Ab and others.

  7. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
   here i make a mistake,because of that there a two mysqld instance on my test pc.you will log in to the binary distribution if you
not specifed the port number.when you run script/mysql_install_db,it initialized the database and make mysqld run as the user you
specifed ,here is mysql,so the mysql user and mysql group must have the previlige to read and write the data dir
first instance

点击(此处)折叠或打开

  1. ubuntu@:~$ ps -e | grep 'mysql'
  2.   946 ? 00:00:01 mysqld
  3. ubuntu@:~$ mysql -uroot -p
  4. Enter password:
  5. Welcome to the MySQL monitor. Commands end with ; or \g.
  6. Your MySQL connection id is 40
  7. Server version: 5.5.37-0ubuntu0.12.04.1 (Ubuntu)

  8. Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

  9. Oracle is a registered trademark of Oracle Corporation and/or its
  10. affiliates. Other names may be trademarks of their respective
  11. owners.

  12. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

  13. mysql> exit
  14. Bye
source distribution

点击(此处)折叠或打开

  1. ubuntu@:~/ssql/bin$ ./mysqld_safe --no-defaults --user=mysql &
  2. [1] 4089

点击(此处)折叠或打开

  1. mysqld_safe is the recommended way to start a mysqld server on Unix. mysqld_safe adds some safety features such as restarting the server when an error occurs and logging runtime information to an error log file. A description of error logging is given later in this section.

  2. mysqld_safe tries to start an executable named mysqld. To override the default behavior and specify explicitly the name of the server you want to run, specify a --mysqld or --mysqld-version option to mysqld_safe.


点击(此处)折叠或打开

  1. ubuntu@:~$ ps -e | grep 'mysql'
  2.   946 ? 00:00:01 mysqld
  3.  4089 pts/0 00:00:00 mysqld_safe
  4.  4168 pts/0 00:00:08 mysqld

点击(此处)折叠或打开

  1. ubuntu@:~/ssql/bin$ ./mysql --no-defaults --port=3307 -umysql -p
  2. Enter password:
  3. Welcome to the MariaDB monitor. Commands end with ; or \g.
  4. Your MariaDB connection id is 3
  5. Server version: 5.5.37-MariaDB-debug Source distribution

  6. Copyright (c) 2000, 2014, Oracle, Monty Program Ab and others.

  7. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

  8. MariaDB [(none)]> show databases;
  9. +--------------------+
  10. | Database |
  11. +--------------------+
  12. | information_schema |
  13. | test |
  14. +--------------------+
  15. 2 rows in set (0.01 sec)
使用root以及匿名用户的测试,可以看到不用的用户看到的DB不同,初始密码均为空

点击(此处)折叠或打开

  1. ubuntu@:~/ssql/bin$ ./mysql --no-defaults --port=3307 -uroot -p
  2. Enter password:
  3. Welcome to the MariaDB monitor. Commands end with ; or \g.
  4. Your MariaDB connection id is 4
  5. Server version: 5.5.37-MariaDB-debug Source distribution

  6. Copyright (c) 2000, 2014, Oracle, Monty Program Ab and others.

  7. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

  8. MariaDB [(none)]> show databases;
  9. +--------------------+
  10. | Database |
  11. +--------------------+
  12. | information_schema |
  13. | mysql |
  14. | performance_schema |
  15. | test |
  16. +--------------------+
  17. 4 rows in set (0.00 sec)

点击(此处)折叠或打开

  1. ubuntu@:~/ssql/bin$ ./mysql --no-defaults --port=3307 -uxxxx -p
  2. Enter password:
  3. Welcome to the MariaDB monitor. Commands end with ; or \g.
  4. Your MariaDB connection id is 5
  5. Server version: 5.5.37-MariaDB-debug Source distribution

  6. Copyright (c) 2000, 2014, Oracle, Monty Program Ab and others.

  7. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

  8. MariaDB [(none)]> show databases;
  9. +--------------------+
  10. | Database |
  11. +--------------------+
  12. | information_schema |
  13. | test |
  14. +--------------------+
  15. 2 rows in set (0.00 sec)

check runnning after install

点击(此处)折叠或打开

  1. ubuntu@:~/ssql/bin$ ./mysqladmin --no-defaults version
  2. ./mysqladmin Ver 9.0 Distrib 5.5.37-MariaDB, for Linux on i686
  3. Copyright (c) 2000, 2014, Oracle, Monty Program Ab and others.

  4. Server version        5.5.37-MariaDB-debug
  5. Protocol version    10
  6. Connection        Localhost via UNIX socket
  7. UNIX socket        /home/ubuntu/ssql/tmp/mysql.sock
  8. Uptime:            58 min 2 sec

  9. Threads: 1 Questions: 48 Slow queries: 0 Opens: 15 Flush tables: 2 Open tables: 41 Queries per second avg: 0.013
  10. ubuntu@:~/ssql/bin$ ./mysqlshow --no-defaults
  11. +--------------------+
  12. | Databases |
  13. +--------------------+
  14. | information_schema |
  15. | test |
  16. +--------------------+
  17. ubuntu@:~/ssql/bin$ ./mysqladmin --no-defaults -uroot shutdown
  18. [1]+ Done ./mysqld_safe --no-defaults --user=mysql
workbench test:

点击(此处)折叠或打开

  1. 3306--> 3307

  2. ubuntu@:~/ssql/mysql-test$ ./mysql-test-run --extern socket=../tmp/mysql.sock
  3. Logging: ./mysql-test-run --extern socket=../tmp/mysql.sock
  4. vardir: /home/ubuntu/ssql/mysql-test/var
  5. Checking supported features...
实例数据库的安装
mysql mysql < file_name
employee data (large dataset, includes data and test/verification suite) Launchpad View US Ltr | A4
world database (MyISAM version, used in MySQL certifications and training) Gzip | Zip View US Ltr | A4
world database (InnoDB      version, used in MySQL certifications and training) Gzip | Zip View US Ltr | A4
sakila database (requires MySQL 5.0 or later) TGZ | Zip View US Ltr | A4
menagerie database
  下载地址 http://dev.mysql.com/doc/sakila/en/
1 download 
2 untar 
3 install
4  import to mysql 

点击(此处)折叠或打开

  1. ubuntu@:~/Downloads$ mysql -uroot -p -D wmyisam < world.sql
  2. Enter password:
  3. ubuntu@:~/Downloads$ mysql -uroot -p -D winnodb < world_innodb.sql
  4. Enter password:


点击(此处)折叠或打开

  1. mysql> show databases;
  2. +--------------------+
  3. | Database |
  4. +--------------------+
  5. | information_schema |
  6. | mysql |
  7. | sakila |
  8. +--------------------+
  9. 3 rows in set (0.12 sec)
  10. mysql> \u sakila;
  11. ERROR 1049 (42000): Unknown database 'sakila;'
  12. mysql> \u sakila
  13. Reading table information for completion of table and column names
  14. You can turn off this feature to get a quicker startup with -A
  15. Database changed
  16. mysql> show tables;
  17. +----------------------------+
  18. | Tables_in_sakila |
  19. +----------------------------+
  20. | actor |
  21. | actor_info |
  22. | address |
  23. | category |
  24. | city |

点击(此处)折叠或打开

  1. mysql> show variables like 'data%' \G;
  2.     *************************** 1. row ***************************
  3.     Variable_name: datadir
  4.             Value: /var/lib/mysql/
  5.     1 row in set (0.00 sec)
  6.     ERROR:
  7.     No query specified
  8.     mysql> system ls /var/lib/mysql/sakila
  9.     actor.frm del_film.TRN nicer_but_slower_film_list.frm
  10.     actor.ibd film_actor.frm payment_date.TRN
  11.     actor_info.frm film_actor.ibd payment.frm
  12.     address.frm film_category.frm payment.ibd
  13.     address.ibd film_category.ibd payment.TRG
  14.     category.frm film.frm rental_date.TRN
  15.     category.ibd film.ibd rental.frm
  16.     city.frm film_list.frm rental.ibd
  17.     city.ibd film_text.frm rental.TRG
  18.     country.frm film_text.MYD sales_by_film_category.frm
  19.     country.ibd film_text.MYI sales_by_store.frm
  20.     customer_create_date.TRN film.TRG staff.frm
下面我们就开始源码的探索之路吧
source code client

点击(此处)折叠或打开

  1. #include "client_priv.h"
  2. #include <m_ctype.h>
  3. #include <stdarg.h>
  4. #include <my_dir.h>
  5. #ifndef __GNU_LIBRARY__
  6. #define __GNU_LIBRARY__         // Skip warnings in getopt.h
  7. #endif
  8. #include "my_readline.h"
  9. #include <signal.h>
  10. #include <violite.h>


点击(此处)折叠或打开

  1. /* Common defines for all clients */

  2. #include <my_global.h>
  3. #include <my_sys.h>
  4. #include <m_string.h>
  5. #include <mysql.h>
  6. #include <errmsg.h>
  7. #include <my_getopt.h>
    connection的内部表示:

点击(此处)折叠或打开

  1. typedef struct st_mysql
  2. {
  3.   NET        net;            /* Communication parameters */
  4.   unsigned char    *connector_fd;        /* ConnectorFd for SSL */
  5.   char        *host,*user,*passwd,*unix_socket,*server_version,*host_info;
  6.   char *info, *db;
  7.   const struct charset_info_st *charset;
  8.   MYSQL_FIELD    *fields;
  9.   MEM_ROOT    field_alloc;
  10.   my_ulonglong affected_rows;
  11.   my_ulonglong insert_id;        /* id if insert on table with NEXTNR */
  12.   my_ulonglong extra_info;        /* Not used */
  13.   unsigned long thread_id;        /* Id for connection in server */
  14.   unsigned long packet_length;
  15.   unsigned int    port;
  16.   unsigned long client_flag,server_capabilities;
  17.   unsigned int    protocol_version;
  18.   unsigned int    field_count;
  19.   unsigned int     server_status;
  20.   unsigned int server_language;
  21.   unsigned int    warning_count;
  22.   struct st_mysql_options options;
  23.   enum mysql_status status;
  24.   my_bool    free_me;        /* If free in mysql_close */
  25.   my_bool    reconnect;        /* set to 1 if automatic reconnect */

  26.   /* session-wide random string */
  27.   char     scramble[SCRAMBLE_LENGTH+1];
  28.   my_bool unused1;
  29.   void *unused2, *unused3, *unused4, *unused5;

  30.   LIST *stmts; /* list of all statements */
  31.   const struct st_mysql_methods *methods;
  32.   void *thd;
  33.   /*
  34.     Points to boolean flag in MYSQL_RES or MYSQL_STMT. We set this flag
  35.     from mysql_stmt_close if close had to cancel result set of this object.
  36.   */
  37.   my_bool *unbuffered_fetch_owner;
  38.   /* needed for embedded server - no net buffer to store the 'info' */
  39.   char *info_buffer;
  40.   void *extension;
  41. } MYSQL;


阅读(2376) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~