mysql源码编译安装


首先去官网http://dev.mysql.com/downloads/mysql/ 下载mysql源码。我下的是5.7.10

 源码选择的是 Generic Linux (Architecture Independent), Compressed TAR Archive

 安装cmake(mysql5.5以后源码安装都得通过cmake编译,并安装了ncurses ncurses-devel

# yum -y install cmake ncurses ncurses-devel
# groupadd mysql
# useradd -g mysql mysql




mysql依赖boost,要安装boost

官方的安装文档:http://dev.mysql.com/doc/refman/5.5/en/installing-source-distribution.html,官方文档说大概如下:

# Preconfiguration setup
shell> groupadd mysql
shell> useradd -r -g mysql -s /bin/false mysql
# Beginning of source-build specific instructions
shell> tar zxvf mysql-VERSION.tar.gz
shell> cd mysql-VERSION
shell> cmake .
shell> make
shell> make install
# End of source-build specific instructions
# Postinstallation setup
shell> cd /usr/local/mysql
shell> chown -R mysql .
shell> chgrp -R mysql .
shell> scripts/mysql_install_db --user=mysql
shell> chown -R root .
shell> chown -R mysql data
# Next command is optional
shell> cp support-files/my-medium.cnf /etc/my.cnf
shell> bin/mysqld_safe --user=mysql &
# Next command is optional
shell> cp support-files/mysql.server /etc/init.d/mysql.server

linux mysql默认安装路径

Installations created from Linux RPM distributions result in files under the following system directories:
/etc/logrotate.d/mysql
/etc/rc.d/init.d/mysql
/var/lib/mysql
/var/lib/mysql/mysql
/var/lock/subsys/mysql
/usr/lib/mysql
/usr/include/mysql
/usr/share/mysql
/usr/bin/mysql

编译安装:

sudo cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DSYSCONFDIR=/etc -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DENABLED_LOCAL_INFILE=1 -DMYSQL_DATADIR=/usr/local/mysql/data -DMYSQL_USER=mysql -DWITH_DEBUG=0

另一个:

cmake . \                                        #开始编译 
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \      #安装到/usr/local/mysql目录 
-DMYSQL_DATADIR=/data/mysql \                  #数据存放到/data/mysql目录 
-DMYSQL_UNIX_ADDR=/data/mysql/mysqld.sock \    #sock存放到/data/mysql目录 
-DWITH_INNOBASE_STORAGE_ENGINE=1 \             #innoDB引擎
-DENABLED_LOCAL_INFILE=1 \ 
-DDEFAULT_CHARSET=utf8 \                       #字符集
-DDEFALUT_CHARSETS=all \                       #支持所有字符集
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \ 
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \ 
-DWITH_PARTITION_STORAGE_ENGINE=1 \ 
-DDEFAULT_COLLATION=utf8_general_ci \          #字符集校验
-DMYSQL_USER=mysql \ 
-DEXTRA_CHARSETS=all \ 
-DMYSQL_TCP_PORT=3306 \ 
-DWITH_DEBUG=0\

 SYSCONFDIR 配置文件所在目录

-DSYSCONFDIR=dir_name

The default my.cnf option file directory.

This location cannot be set at server startup, but you can start the server with a given option file using the --defaults-file=file_name option, where file_name is the full path name to the file.

    1. sudo make -j4 #-j 数字 表示以多核心运行
    2. sudo make install

 LOAD DATA INFILE.

This option controls client-side LOCAL capability, but the capability can be set on the server side at server startup with the --local-infile option. See Section 6.1.6, “Security Issues with LOAD DATA LOCAL”.

配置选项:http://dev.mysql.com/doc/refman/5.5/en/source-configuration-options.html

4.相关设置

4.1配置开机启动
  1. sudo cp ./support-files/my-default.cnf /etc/my.cnf
  2. sudo cp ./support-files/mysql.server  /etc/init.d/mysqld  #把Mysql加入系统启动
  3. sudo chmod 755 /etc/init.d/mysqld
  4. sudo chkconfig mysqld on
4.2常用命令软连接
  1. sudo ln -s /usr/local/mysql/bin/mysql /usr/bin
  2. sudo ln -s /usr/local/mysql/bin/mysqladmin /usr/bin
4.3 初始化数据库
  1. sudo /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --skip-name-resolve --user=mysql
4.4 修改配置文件
  1. sudo vi /etc/my.cnf
文件末尾增加:
  1. #增加默认存储类型和去掉反向解析
  2. default-storage-engine=MyISAM
  3. skip-name-resolve

 

修改mysql默认存储引擎:

1、查看mysql存储引擎命令,在mysql>提示符下搞入show engines;字段 Support为:Default表示默认存储引擎  
2、设置InnoDB为默认引擎:在配置文件my.cnf中的 [mysqld] 下面加入default-storage-engine=INNODB 一句
3、重启mysql服务器:mysqladmin -u root -p shutdown或者service mysqld restart 登录mysql数据库,

 --datadir=dir_name-h dir_name http://dev.mysql.com/doc/refman/5.5/en/server-options.html#option_mysqld_datadir

datadir= /usr/local/mysql/data 

有时候,想改下datadir的目录。就改下datadir为对应的目录。

出现问题:

'./mysql-bin.index' not found (Errcode: 13) 的解决方法

出现:

The server quit without updating PID file

权限问题:执行 “chown -R mysql:mysql /var/data” “chmod -R 755 /usr/local/mysql/data”

一份示例:

log-error                      = /usr/local/log/mysql-error.log
log-queries-not-using-indexes  = 1
slow-query-log                 = 1 
slow-query-log-file            = /usr/local/log/mysql-slow.log

The --log-queries-not-using-indexes option is available for the purpose of logging queries that do not use indexes to the slow query log.

其中log-queries-not-using-indexes会记录所有没有用上索引全表扫描的语句,就算你的SQL没有超过long_query_time设置的时间。有些只有一二十条记录的表全表扫描并不是什么大事也会记录到你的慢查日志中,对慢查分析有一定的坏处,你需要斟酌加上这个参数。

问题:http://bbs.chinaunix.net/thread-4079318-1-1.html 

出现一中情况:关于log-queries-not-using-indexes这个参数:如果运行的SQL语句没有使用索引,则mysql数据库同样会将这条SQL语句记录到慢查询日志文件中。

手册:
This option does not necessarily mean that no index is used. For example, a query that uses
a full index scan uses an index but would be logged because the index would not limit the number of rows.

可以用mysqldumpslow用来分析sql语句慢查询

[Mysql系统学习]slow query log慢查询日志功能

http://www.51itstudy.com/18261.html

慢查询相关的 参数long_query_time 

当查询超过long_query_time指定的时间,那么就会记录在慢查询日志文件中,默认是10秒

如果出现The server quit without updating PID file,请根据创建pid并更改权限775  

6)配置环境变量
为了直接调用mysql,需要将mysql的bin目录加入PATH环境变量。
编辑/etc/profile文件:
sudo vim /etc/profile
在文件最后 添加如下两行:
PATH=$PATH:/usr/local/mysql/bin
export PATH
关闭文件,运行下面的命令,让配置立即生效:
source /etc/profile


(7)修改root密码(因为默认密码为空)
mysql -h127.0.0.1 -uroot -p
update mysql.user set password=password("mysqldba") where user='root';
flush privileges;


测试一下:

  1. 3.7.检查所有数据库  
  2. #/usr/local/mysql02/bin/mysqlcheck --all-databases -uroot -p123456     

select now(),user(),version();
show databases;

参考: http://yanue.net/post-111.html   安装后用navciat连接报1130错误。RROR 1130: Host 192.168.2.159 is not allowed to connect to this MySQL server  
猜想是无法给远程连接的用户权限问题。结果这样子操作mysql库,即可解决。在本机登入mysql后,更改 “mysql” 数据库里的 “user” 表里的 “host” 项,从”localhost”改称'%'。。 
mysql -u root -p 
mysql>use mysql; 
mysql>select 'host' from user where user='root'; 
mysql>update user set host = '%' where user ='root';  (

mysql> update user set host = '%' where user ='root';
ERROR 1062 (23000): Duplicate entry '%-root' for key 'PRIMARY'

然后查看了下数据库的host信息如下: 
MySQL> select host from user where user = 'root'; 
+-----------------------+ 
| host | 
+-----------------------+ 
| % | 
| 127.0.0.1 | 
| localhost.localdomain | 
+-----------------------+ 
3 rows in set (0.00 sec) 
host已经有了%这个值,所以直接运行命令: MySQL>flush privileges; 

然后连接成功)。


mysql>flush privileges; 
mysql>select 'host'   from user where user='root'; 
第一句是以权限用户root登录 
第二句:选择mysql库 
第三句:查看mysql库中的user表的host值(即可进行连接访问的主机/IP名称) 
第四句:修改host值(以通配符%的内容增加主机/IP地址),当然也可以直接增加IP地址 
第五句:刷新MySQL的系统权限相关表 
第六句:再重新查看user表时,有修改。。 

重起mysql服务即可完成。

改密:

 mysql> use mysql;

  mysql> UPDATE user SET Password = PASSWORD('newpass') WHERE user = 'root';

  mysql> FLUSH PRIVILEGES;

新增用户,

 select user,host from user;

使用create user新增用户

1.mysql>CREATE USER 'user1'@'localhost' IDENTIFIED BY 'pass1';

说明:username – 你将创建的用户名, host – 指定该用户在哪个主机上可以登陆,如果是本地用户可用localhost,  如 果想让该用户可以从任意远程主机登陆,可以使用通配符%. password –  该用户的登陆密码,密码可以为空,如果为空则该用户可以不需要密码登 陆服务器.

例子:

CREATE USER 'dog'@'localhost' IDENTIFIED BY '123456' CREATE USER 'pig'@'192.168.1.101_' IDENDIFIED BY '123456' CREATE USER 'pig'@'%' IDENTIFIED BY '123456' CREATE USER 'pig'@'%' IDENTIFIED BY '' CREATE USER 'pig'@'%';   授权 我使用的是:  grant all privileges on *.* to root@'%' ; 然后flush privileges;      

2.mysql>GRANT SELECT,INSERT,UPDATE,DELETE ON *.* TO 'user1'@'localhost';

OR

简单的方法:

mysql>GRANT ALL ON *.* TO 'user1'@'localhost';

 或者

grant all on *.* to 'root'@'%' identified by  '密码' 这样会自动生成用户名,并且给予指定的密码

If you think the method above is so difficult that you can't accept, you can use the method below:

mysql>GRANT ALL ON *.* TO 'user2'@'localhost' IDENTIFIED BY 'pass1';

And there is another direct method that you can have a try!

mysql>INSERT INTO user (Host,User,Password,Select_priv,Insert_priv)

VALUES('localhost','user4',PASSWORD('pass3'),'Y','Y');

mysql对用户登录ip段授权的方法  

 GRANT ALL PRIVILEGES ON *.* TO abc@'192.168.2.%' 即可。

GRANT ALL PRIVILEGES ON *.* TO abc@'192.168.%' 即可。

或者

GRANT ALL PRIVILEGES ON *.* TO abc@'192.168.%' 即可。'192.168.0.0/255.255.0.0'

user表范围字段可以如下被指定:

  • 一个Host值可以是主机名或一个IP数字,或'localhost'指出本地主机。
  • http://dev.mysql.com/doc/refman/5.1/zh/database-administration.html#connection-access

    另外的方法:

    http://www.cnblogs.com/fly1988happy/archive/2011/12/15/2288554.html

    centos mysql iptables配置  

    在CentOS系统中防火墙默认是阻止3306端口的,我们要是想访问mysql数据库,我们需要这个端口,命令如下:

    1 /sbin/iptables -I INPUT -p tcp --dport 3036 -j ACCEPT

    我们需要保存我们的操作,命令如下:

    1  /etc/rc.d/init.d/iptables save

    此时我们可以查看端口的状态,命令如下:

    1 /etc/init.d/iptables status

    当然如果你打开其他端口也一样,只需要把这个端口号换乘你需要的端口号即可。

    #关闭防火墙
    /etc/init.d/iptables stop
    service iptables stop # 停止服务
    #查看防火墙信息
    /etc/init.d/iptables status

    #开放端口:8080
    /sbin/iptables -I INPUT -p tcp --dport 8080 -j ACCEPT
    #重启防火墙以便改动生效:(或者直接重启系统)
    /etc/init.d/iptables restart
    #将更改进行保存
    /etc/rc.d/init.d/iptables save

    另外直接在/etc/sysconfig/iptables中增加一行:
    -A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 8080 -j ACCEPT

    #永久关闭防火墙
    chkconfig –level 35 iptables off

    mysql修改端口

    参考:

    http://www.cnblogs.com/siqi/p/3567551.html 很好的一个总结。

    http://my.oschina.net/u/1179414/blog/202377

    mysql配置多实例:

    #创建一个新的空数据库
    [root@test-206 ~]# mkdir /var/lib/mysql_3307
    [root@test-206 ~]# mysql_install_db --datadir=/var/lib/mysql_3307 --user=mysql

    3、给数据文件赋予mysql用户与用户组

    [root@test-206 ~]# chown -R mysql.mysql /var/lib/mysql_3307

    4、创建multi的配置cnf文件,用于启动这个mysql实例(如3307)载入执行

    文件中写入你想要的配置,如下为典型配置

    [mysqld_multi]
    mysqld     = /usr/bin/mysqld_safe
    mysqladmin = /usr/bin/mysqladmin
    user       = root    #用于登陆和关闭此服务
    password   = 123456   #同上
    
    [mysqld3307]
    socket     = /tmp/mysql_3307.sock
    port       = 3307
    pid-file   = /var/lib/mysql_3307/3307.pid
    datadir    = /var/lib/mysql_3307/
    log        = /var/lib/mysql_3307/3307.log
    character-set-server    = utf8
    user       = mysql

     5、启动你的多实例

    [root@test-206 ~]# mysqld_multi --defaults-extra-file=/usr/local/my_multi.cnf start 3307

    6、检查是否启动成功

    [root@test-206 ~]# netstat -ntlp
    tcp        0      0 :::3306                     :::*                       LISTEN      3919/mysqld
    tcp        0      0 :::3307                     :::*                        LISTEN      15027/mysqld

    如果没有发现你要的端口号mysql实例,可以检查下/var/lib/mysql_3307/3307.log文件,排除问题。

    my.cnf里面的client和mysql都有port,有有什么不同

    # 以下选项会被MySQL客户端应用读取。 # 注意只有MySQL附带的客户端应用程序保证可以读取这段内容。 # 如果你想你自己的MySQL应用程序获取这些值。 # 需要在MySQL客户端库初始化的时候指定这些选项。   # [client] #password = [your_password] port = @MYSQL_TCP_PORT@ socket = @MYSQL_UNIX_ADDR@   # *** 应用定制选项 ***   # MySQL 服务端 # [mysqld]   # 一般配置选项 port = @MYSQL_TCP_PORT@ socket = @MYSQL_UNIX_ADDR@

    我启动出现这个问题:

    160419 11:06:56 mysqld_safe The file /usr/local/mysql/bin/mysqld
    does not exist or is not executable. Please cd to the mysql installation
    directory and restart this script from there as follows:
    ./bin/mysqld_safe&

    加个软连接:

    问题存在原因:

    1、mysqld_safe只认识/usr/local/mysql/bin/mysqld(也许是配置问题)。


    [root@localhost bin]# ./mysqld_multi --help
    mysqld_multi version 2.16 by Jani Tolonen

    Description:
    mysqld_multi can be used to start, or stop any number of separate
    mysqld processes running in different TCP/IP ports and UNIX sockets.

    mysqld_multi can read group [mysqld_multi] from my.cnf file. You may
    want to put options mysqld=... and mysqladmin=... there. Since
    version 2.10 these options can also be given under groups [mysqld#],
    which gives more control over different versions. One can have the
    default mysqld and mysqladmin under group [mysqld_multi], but this is
    not mandatory. Please note that if mysqld or mysqladmin is missing
    from both [mysqld_multi] and [mysqld#], a group that is tried to be
    used, mysqld_multi will abort with an error.

    mysqld_multi will search for groups named [mysqld#] from my.cnf (or
    the given --defaults-extra-file=...), where '#' can be any positive
    integer starting from 1. These groups should be the same as the regular
    [mysqld] group, but with those port, socket and any other options
    that are to be used with each separate mysqld process. The number
    in the group name has another function; it can be used for starting,
    stopping, or reporting any specific mysqld server.

    Usage: mysqld_multi [OPTIONS] {start|stop|report} [GNR,GNR,GNR...]
    or mysqld_multi [OPTIONS] {start|stop|report} [GNR-GNR,GNR,GNR-GNR,...]

    The GNR means the group number. You can start, stop or report any GNR,
    or several of them at the same time. (See --example) The GNRs list can
    be comma separated or a dash combined. The latter means that all the
    GNRs between GNR1-GNR2 will be affected. Without GNR argument all the
    groups found will either be started, stopped, or reported. Note that
    syntax for specifying GNRs must appear without spaces.

    Options:

    These options must be given before any others:
    --no-defaults Do not read any defaults file
    --defaults-file=... Read only this configuration file, do not read the
    standard system-wide and user-specific files
    --defaults-extra-file=... Read this configuration file in addition to the
    standard system-wide and user-specific files
    Using:

    --example Give an example of a config file with extra information.
    --help Print this help and exit.
    --log=... Log file. Full path to and the name for the log file. NOTE:
    If the file exists, everything will be appended.

    --

    启动报错:

    Can't find messagefile '/usr/share/mysql/errmsg.sys'

    解决方法是:

    root@debian:/usr/local# vi mysql3306/my.cnf

    加入language配置项,由于我是使用二进制分发包进行安装的,而默认的情况下,mysql会到/usr/local/mysql/share/english下查找该文件,

    [mysqld]
    pid-file        = /home/www/mysql/m3306/mysql3306.pid
    log-error       = /home/www/mysql/m3306/mysql3306.err
    language        = /usr/local/mysql3306/share/english            #此处为language配置项

    成功启动后:

    mysql -P3307 -uroot --socket=/tmp/mysql2.sock 

    要指定socket文件。

    查看mysql状态:

    ./mysqladmin   --socket=/tmp/mysql2.sock  ping

    mysqld is alive

     ---------------------

    mysql更换data

    有时候我们要把复制过来的data数据弄到mysql里。

     Please read "Security" section of the manual to find out how to run mysqld as root!错误解决

     与使用root用户启动mysqld相比,更好的方法是使用一个普通的、没有高级权限的用户帐户允许mysqld,例如创建一个名为mysql的用户帐户来专门管理MySQL。使用其帐启

       动MySQL的方法是在mysqld命令后面加上一个用户选项,这个用户属于mysqld用户组并且位于my.cnf配置文件中。例如在创建mysql帐户后,可以将下面的内容添加到my.cnf文

       件中:

    [mysqld]

          user=mysql

      This causes the server to start as the designated user whether you
      start it manually or by using *Note `mysqld_safe': mysqld-safe. or
      *Note `mysql.server': mysql-server. For more details, see *Note
      changing-mysql-user::.

     mysql 5.6编译安装:

    5.6不依赖boost

    5.7有2个版本,依赖boost和不依赖boost.

    MySQL5.6之后默认root密码的问题

    这样就可以了, 但是为什么和我之前安装的5.1 不一样呢,原来从MySQL5.6.8开始,MySQL RPM安装包用了更安全的安装方式,再不是以前的密码为空了,mysql会给root账号随机分配一个密码,安装MySQL后,root的这个随机密码会写在文件:.mysql_secret中,但这个密码不能做任何事情,只能登录。

    mysql里面的mysql.user表为空?


    mysql> select * from user;
    Empty set (0.00 sec)

    mysql> use mysql;
    Database changed
    mysql> grant all on *.* to 'root'@'%' identified by '123456';
    ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
    mysql> CREATE USER 'user1'@'localhost' IDENTIFIED BY '123456';