Zihao

Make small but daily progress

0%

CentOS 编译 mysql

本文介绍了如何在 CentOS 上进行 Mysql 编译。

下载源码

1
2
3
wget http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.23.tar.gz

tar zxvf mysql-5.6.23.tar.gz

安装必要的包

1
sudo yum install cmake gcc gcc-c++ ncurses-devel perl-Data-Dumper cmake ncurses-devel bison autoconf automake zlib* fiex* libxml* libmcrypt* libtool-ltdl-devel*

进入mysql源码目录,生成makefile

1
2
cd mysql-5.6.23
cmake .

生成失败的的话

1
rm -f CMakeCache.txt

查看问题,进行修复

编译并安装

1
2
make
sudo make install

mysql将会安装到/usr/local/mysql路径。

添加mysql用户和组

先查看是否存在

1
2
3
cat /etc/passwd | grep mysql

cat /etc/group | grep mysql

添加

1
2
3
sudo groupadd mysql
sudo useradd -r -g mysql mysql
chown -R mysql:mysql /usr/local/mysql

修改目录和文件权限,安装默认数据库

1
2
cd /usr/local/mysql
sudo scripts/mysql_install_db --user=mysql?--basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

至此,mysql就可以启动运行了。

启动mysql

CentOS7自带MariaDB的支持,/etc下默认存在my.cnf文件干扰mysql运行,需要先删掉

1
2
cd /etc
sudo rm -fr my.cnf my.cnf.d

然后再/etc下重建my.cnf文件,内容如下

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Foradvice onhow tochange settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
[mysqld]
# Remove leading # andsettothe amount ofRAM forthe most important data
# cache inMySQL. Start at70% oftotal RAM fordedicated server, else10%.
# innodb_buffer_pool_size = 128M
# Remove leading # toturn ona very important data integrity option: logging
# changes tothe binarylog betweenbackups.
# log_bin
# These are commonly set, remove the # andsetasrequired.
# basedir = .....
# datadir = /data/mysql/data
?port = .....
# server_id = .....
# socket = .....
# Remove leading # tosetoptions mainly useful forreporting servers.
# The server defaults are faster fortransactions andfast SELECTs.
# Adjust sizes asneeded, experiment tofind the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
max_connection = 10000
sql_mode = NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
#binarylog
log-bin = mysql-bin
binlog_format = mixed
expire_logs_day = 30
#slow query log
slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow.log
long_query_time = 3
log-queries-not-using-indexes
log-slow-admin-statements
?
innodb_log_files_in_group=2
innodb_data_home_dir = /opt/data/mysql/
innodb_data_file_path = ibdata1:2000M;ibdata2:2000M;ibdata3:20M:autoextend
innodb_log_group_home_dir = /opt/data/mysql/

# 4G RAM
innodb_buffer_pool_size = 1G
innodb_log_file_size = 256M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit=0
innodb_thread_concurrency=8
innodb_flush_method=O_DIRECT

# perform
tmp_table_size = 512M
max_heap_table_size=128M

现在可以启动mysql了

1
sudo /usr/local/mysql/bin/mysqld_safe --user=mysql &

CentOS7 不能使用service控制mysql服务,而源码安装的mysql也没有提供Systemd的控制脚本。

开机启动

于是编辑/etc/rc.d/rc.local文件,添加mysql的开机启动命令。

1
/usr/local/mysql/bin/mysqld_safe --user=mysql &

然后给/etc/rc.d/rc.local添加可执行权限

1
sudo chmod a+x /etc/rc.d/rc.local

如果开启时报错 ,重新初始化,注意用户名别用错了

1
sudo /usr/local/mysql/bin/mysqld_safe --user=mysql &

修改root密码

1
2
3
4
5
6
7
/usr/local/mysql/bin/mysql -uroot
use mysql;
select * from mysql?WHERE?user=?'root';
UPDATE user SET?password= PASSWORD('123456') WHERE?user= 'root';

GRANT ALL PRIVILEGES ON *.* TO root@'%' IDENTIFIED BY '123456';
FLUSH PRIVILEGES;

至此,安装基本完成了,一个mysql就能用了。

服务启动mysql

添加服务,拷贝服务脚本到init.d目录,并设置开机启动

1
2
3
cp support-files/mysql.server /etc/init.d/mysql
chkconfig mysql on
service mysql start –启动MySQL

查看是否启动成功

1
netstat -lntp | grep 3306

欢迎关注我的其它发布渠道