Linux--MySQL源码安装及配置

  上篇博客http://blog.xiaoxiaomo.com/2016/02/22/Linux-软件安装之Mysql/,使用了一种相对比较简单的安装方式。本片博客,在讲解另外一种安装方式。

准备

安装

  • 1.先安装cmake
    1
    2
    3
    4
    5
    6
    [root@iZ94r8hgrjcZ up]# tar -zxvf cmake-3.0.2.tar.gz 
    [root@iZ94r8hgrjcZ up]# mv cmake-3.0.2 /opt/
    [root@iZ94r8hgrjcZ up]# cd /opt/cmake-3.0.2/
    [root@iZ94r8hgrjcZ cmake-3.0.2]# ./configure ##如报错,请看下面解决办法
    [root@iZ94r8hgrjcZ cmake-3.0.2]# make
    [root@iZ94r8hgrjcZ cmake-3.0.2]# make install

问题一:

1
2
3
4
5
6
7
8
9
10
11
12
[root@iZ94r8hgrjcZ cmake-3.0.2]# ./configure 
---------------------------------------------
CMake 3.0.2, Copyright 2000-2014 Kitware, Inc.
---------------------------------------------
Error when bootstrapping CMake:
Cannot find appropriate C compiler on this system.
Please specify one using environment variable CC.
See cmake_bootstrap.log for compilers attempted.

---------------------------------------------
Log of errors: /opt/cmake-3.0.2/Bootstrap.cmk/cmake_bootstrap.log
---------------------------------------------

解决办法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
1. ##安装gcc环境
yum -y install gcc

2. 然后运行:[root@iZ94r8hgrjcZ
3.
4. cmake-3.0.2]# ./bootstrap #报错如下
---------------------------------------------
CMake 3.0.2, Copyright 2000-2014 Kitware, Inc.
C compiler on this system is: cc
---------------------------------------------
Error when bootstrapping CMake:
Cannot find appropriate C++ compiler on this system.
Please specify one using environment variable CXX.
See cmake_bootstrap.log for compilers attempted.
---------------------------------------------
Log of errors: /opt/cmake-3.0.2/Bootstrap.cmk/cmake_bootstrap.log
---------------------------------------------

##缺少c++的环境
yum -y install gcc-c++

[root@iZ94r8hgrjcZ cmake-3.0.2]# ./bootstrap
[root@iZ94r8hgrjcZ cmake-3.0.2]# gmake
[root@iZ94r8hgrjcZ cmake-3.0.2]# gmake install
  • 2.创建mysql的安装目录及数据库存放目录

    1
    2
    [root@iZ94r8hgrjcZ opt]# mkdir -p /opt/mysql //安装mysql 
    [root@iZ94r8hgrjcZ opt]# mkdir -p /opt/mysql/data //存放数据库
  • 3.创建mysql用户及用户组

    1
    2
    [root@iZ94r8hgrjcZ opt]# groupadd mysql
    [root@iZ94r8hgrjcZ opt]# useradd -r -g mysql mysql
  • 4.安装mysql

    1
    2
    3
    4
    5
    [root@iZ94r8hgrjcZ up]# tar -zxvf mysql-5.5.40\ linux.tar.gz 
    [root@iZ94r8hgrjcZ up]# cd mysql-5.5.40
    [root@iZ94r8hgrjcZ mysql-5.5.40]# cmake . -DCMAKE_INSTALL_PREFIX=/opt/mysql -DMYSQL_DATADIR=/opt/mysql/data -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all -DENABLED_LOCAL_INFILE=1 //注意:上面是一条命令
    [root@iZ94r8hgrjcZ mysql-5.5.40]# make
    [root@iZ94r8hgrjcZ mysql-5.5.40]# make install
  • 5.问题解决(博主报了如下错误)

错误一: 在cmake时,出现如下错误:

1
2
3
4
5
6
7
8
9
-- Could NOT find Curses (missing:  CURSES_LIBRARY CURSES_INCLUDE_PATH) 
CMake Error at cmake/readline.cmake:83 (MESSAGE):
Curses library not found. Please install appropriate package,

remove CMakeCache.txt and rerun cmake.On Debian/Ubuntu, package name is libncurses5-dev, on Redhat andevel.
Call Stack (most recent call first):
cmake/readline.cmake:127 (FIND_CURSES)
cmake/readline.cmake:217 (MYSQL_USE_BUNDLED_LIBEDIT)
CMakeLists.txt:369 (MYSQL_CHECK_READLINE)

解决办法:

1
2
3
yum -y install ncurses-devel
yum -y install bison
make && make install #编译完成

错误二: 在make时,出现了如下错误:

1
make: *** No targets specified and no makefile found.  Stop.

解决办法:

1
2
3
4
5
6
1、wget http://ftp.gnu.org/pub/gnu/ncurses/ncurses-5.6.tar.gz
2、tar zxvf ncurses-5.6.tar.gz
3、cd ncurses-5.6
4、 ./configure -prefix=/opt -with-shared -without-debug
./configure --with-shared --without-debug --with-ticlib
5、make && make install

注意:如果执行cmake . -DCMAKE_INSTALL_PREFIX=/opt/mysql -DMYSQL_DATADIR=/opt/mysql/data -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all -DENABLED_LOCAL_INFILE=1还是报错:

1
2
3
4
5
6
remove CMakeCache.txt and rerun cmake.On Debian/Ubuntu, package name is libncurses5-dev, 
on Redhat and derivates it is ncurses-devel.
Call Stack (most recent call first):
cmake/readline.cmake:127 (FIND_CURSES)
cmake/readline.cmake:217 (MYSQL_USE_BUNDLED_LIBEDIT)
CMakeLists.txt:257 (MYSQL_CHECK_READLINE)

记得清除旧的对象文件和缓存

1
2
# make clean
# rm -f CMakeCache.txt

  • 6.参数说明:

    -DCMAKE_INSTALL_PREFIX=/opt/mysql //安装目录
    -DINSTALL_DATADIR=/opt/mysql/data //数据库存放目录
    -DDEFAULT_CHARSET=utf8 //使用utf8字符
    -DDEFAULT_COLLATION=utf8_general_ci //校验字符
    -DEXTRA_CHARSETS=all //安装所有扩展字符集
    -DENABLED_LOCAL_INFILE=1 //允许从本地导入数据

配置

  • 一、设置目录权限
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@iZ94r8hgrjcZ mysql-5.5.40]# cd /opt/mysql/
[root@iZ94r8hgrjcZ mysql]# ll
total 200
drwxr-xr-x 2 root root 4096 Apr 25 19:24 bin
-rw-r--r-- 1 root root 17987 Sep 8 2014 COPYING
drwxr-xr-x 3 root root 4096 Apr 25 19:24 data
drwxr-xr-x 2 root root 4096 Apr 25 19:24 docs
drwxr-xr-x 3 root root 4096 Apr 25 19:24 include
-rw-r--r-- 1 root root 132608 Sep 8 2014 INSTALL-BINARY
drwxr-xr-x 3 root root 4096 Apr 25 19:24 lib
drwxr-xr-x 4 root root 4096 Apr 25 19:24 man
drwxr-xr-x 10 root root 4096 Apr 25 19:24 mysql-test
-rw-r--r-- 1 root root 2496 Sep 8 2014 README
drwxr-xr-x 2 root root 4096 Apr 25 19:24 scripts
drwxr-xr-x 27 root root 4096 Apr 25 19:24 share
drwxr-xr-x 4 root root 4096 Apr 25 19:24 sql-bench
drwxr-xr-x 2 root root 4096 Apr 25 19:24 support-files、

[root@iZ94r8hgrjcZ mysql]# chown -R root:mysql .
[root@iZ94r8hgrjcZ mysql]# chown -R mysql:mysql data
  • 二、将mysql的启动服务添加到系统服务中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@iZ94r8hgrjcZ mysql]# cp support-files/my-medium.cnf /etc/my.cnf //my-medium.cnf不仅限于这一个文件

//查看可以使用的文件
[root@iZ94r8hgrjcZ mysql]# cd support-files/
[root@iZ94r8hgrjcZ support-files]# ll
#下面列出的×.cnf结尾的都可以,但是每一个都有不同的用处
total 152
-rwxr-xr-x 1 root mysql 1153 May 29 11:14 binary-configure
-rwxr-xr-x 1 root mysql 4528 May 29 11:14 config.huge.ini
-rwxr-xr-x 1 root mysql 2382 May 29 11:14 config.medium.ini
-rwxr-xr-x 1 root mysql 1626 May 29 11:14 config.small.ini
-rw-r--r-- 1 root mysql 773 Jul 2 2012 magic
-rw-r--r-- 1 root mysql 4691 May 29 11:14 my-huge.cnf
-rw-r--r-- 1 root mysql 19759 May 29 11:14 my-innodb-heavy-4G.cnf
-rw-r--r-- 1 root mysql 4665 May 29 11:14 my-large.cnf
-rw-r--r-- 1 root mysql 4676 May 29 11:14 my-medium.cnf
-rw-r--r-- 1 root mysql 2840 May 29 11:14 my-small.cnf
-rwxr-xr-x 1 root mysql 1061 May 29 11:14 mysqld_multi.server
-rwxr-xr-x 1 root mysql 839 May 29 11:14 mysql-log-rotate
-rwxr-xr-x 1 root mysql 10650 May 29 11:14 mysql.server
-rwxr-xr-x 1 root mysql 1326 May 29 11:14 ndb-config-2-node.ini
  • 三、创建系统数据库的表
1
2
[root@iZ94r8hgrjcZ mysql]# cd /opt/mysql
[root@iZ94r8hgrjcZ mysql]# scripts/mysql_install_db --user=mysql
  • 四、设置环境变量

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    [root@iZ94r8hgrjcZ mysql]# vi /etc/profile
    # Path manipulation
    if [ "$EUID" = "0" ]; then
    pathmunge /sbin
    pathmunge /usr/sbin
    pathmunge /usr/local/sbin
    pathmunge /opt/mysql/bin/##添加行
    fi

    [root@iZ94r8hgrjcZ mysql]# source /etc/profile
  • 五、将mysql添加到系统服务中

    1
    [root@iZ94r8hgrjcZ mysql]# cp support-files/mysql.server /etc/init.d/mysqld //将mysql的启动服务添加到系统服务中
  • 六、启动mysql的方法(mysql已经被添加到系统服务中)

    1
    2
    3
    4
    [root@iZ94r8hgrjcZ mysql]# service mysql start  //启动mysql服务
    ***********************************************
    [root@iZ94r8hgrjcZ mysql]# service mysql stop  //停止mysql服务
    [root@iZ94r8hgrjcZ mysql]# service mysql restart //重启mysql服务
  • 七、修改MySQL的root用户的密码以及打开远程连接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
[root@iZ94r8hgrjcZ mysql]#  mysql -u root -p
Enter password:
//这里MySQL的root用户还没有配置密码,直接点回车键即可。

mysql> use mysql; //使用mysql这个库
Database changed

mysql> GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY "root"; //添加远程连接
Query OK, 0 rows affected (0.01 sec)

mysql> update user set Password = password('xiaoxiaomo.com') where User='root'; //设置密码为xiaoxiaomo.com
Query OK, 5 rows affected (0.01 sec)
Rows matched: 5 Changed: 5 Warnings: 0

mysql> select Host,User,Password from user where User='root';
+--------------+------+-------------------------------------------+
| Host | User | Password |
+--------------+------+-------------------------------------------+
| localhost | root | *758A8EEE3D96A07C409995FD1E7C66493AC52206 |
| iz94r8hgrjcz | root | *758A8EEE3D96A07C409995FD1E7C66493AC52206 |
| 127.0.0.1 | root | *758A8EEE3D96A07C409995FD1E7C66493AC52206 |
| ::1 | root | *758A8EEE3D96A07C409995FD1E7C66493AC52206 |
| % | root | *758A8EEE3D96A07C409995FD1E7C66493AC52206 |
+--------------+------+-------------------------------------------+
5 rows in set (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye

[root@ iZ94r8hgrjcZ mysql]# mysql -u root -p

若还不能进行远程连接,则查看防火墙是否开启
  • 八、设置开机启动

    1
    [root@iZ94r8hgrjcZ mysql]# chkconfig --level 345 mysql on //设置开机启动(取消开机启动:把 on 改为 off)
  • 九、修改mysql的最大连接数

mysql默认安装的最大连接数是100。100一般是不够用的。要增大连接数,要怎样增加呢?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@iZ94r8hgrjcZ mysql]# vi /etc/my.cnf  

# The MySQL server
[mysqld]
port = 3306
socket = /tmp/mysql.sock
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 4M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
#一定要加在[mysqld]这段里面,根据你的需求自行更改数据
max_connections=1000 # 最大连接数
max_user_connections=500 # 每个用户最大连接数
wait_timeout=200 # 多少秒后关闭空闲(IDLE)

检测:

1
2
3
4
5
6
7
8
9
[root@iZ94r8hgrjcZ mysql]# service mysql restart  //重启mysql服务
[root@iZ94r8hgrjcZ mysql]# cd /opt/mysql/bin
[root@iZ94r8hgrjcZ bin]# mysqladmin -u root -p variables //检测
输入root数据库账号的密码后可看到 (屏幕上的数据有点多,慢慢找)

|max_connections | 1000
|max_user_connections | 500
|wait_timeout | 200
#改动已经生效。
  • 十、查看修改mysql编码方式

MySQL的默认编码是Latin1,不支持中文,要支持中文需要把数据库的默认编码修改为gbk或者utf8(java的一般都是用utf-8,乱码问题蛋疼啊,我就用utf-8演示)。

  1. 需要以root用户身份登陆才可以查看数据库编码方式

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    mysql> show variables like 'character%';
    +--------------------------+----------------------------------+
    | Variable_name | Value |
    +--------------------------+----------------------------------+
    | character_set_client | utf8 |
    | character_set_connection | utf8 |
    | character_set_database | latin1 |
    | character_set_filesystem | binary |
    | character_set_results | utf8 |
    | character_set_server | latin1 |
    | character_set_system | utf8 |
    | character_sets_dir | /opt/mysql/share/charsets/ |
    +--------------------------+----------------------------------+
    8 rows in set (0.00 sec)
  2. 停止mysql服务

    1
    2
    [root@iZ94r8hgrjcZ etc]# service mysql stop
    Shutting down MySQL. [ OK ]
  3. 编辑my.cnf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@iZ94r8hgrjcZ etc]# vi /etc/my.cnf
# The following options will be passed to all MySQL clients
[client]
#password = your_password
port = 3306
socket = /tmp/mysql.sock
default-character-set=utf8 ##为添加内容1

[mysqld]
port = 3306
max_connections=1000 # 最大连接数
max_user_connections=500 # 每个用户最大连接数
wait_timeout=200 # 多少秒后关闭空闲(IDLE)
character-set-server =utf8 ##为添加内容2
  1. 检查
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    [root@iZ94r8hgrjcZ etc]# service mysql start
    Starting MySQL.. [ OK ]
    [root@iZ94r8hgrjcZ etc]# mysql -u root -p
    Enter password:
    mysql> show variables like 'character%';
    +--------------------------+----------------------------------+
    | Variable_name | Value |
    +--------------------------+----------------------------------+
    | character_set_client | utf8 |
    | character_set_connection | utf8 |
    | character_set_database | utf8 |
    | character_set_filesystem | binary |
    | character_set_results | utf8 |
    | character_set_server | utf8 |
    | character_set_system | utf8 |
    | character_sets_dir | /opt/mysql/share/charsets/ |
    +--------------------------+----------------------------------+
    8 rows in set (0.00 sec)

当前网速较慢或者你使用的浏览器不支持博客特定功能,请尝试刷新或换用Chrome、Firefox等现代浏览器