Fork me on GitHub

CentOS6.7源码安装MySQL5.6

1.源码安装MySQL5.6

# CentOS6操作系统安装完成后,默认会在/etc目录下存在一个my.cnf,
# 强制卸载了mysql-libs之后,my.cnf就会消失
rpm -qa | grep mysql 
rpm -e --nodeps mysql-libs-5.1.71-1.el6.x86_64
yum -y install make gcc-c++ cmake bison-devel ncurses-devel

groupadd mysql
useradd -g mysql mysql -M -s /sbin/nologin
mkdir -p /server/tools
cd /server/tools
wget http://cdn.mysql.com//archives/mysql-5.6/mysql-5.6.21.tar.gz
tar xf mysql-5.6.21.tar.gz
mv /server/tools/mysql-5.6.21 /opt/mysql-5.6.21/
ln -s  /opt/mysql-5.6.21/ /application/mysql
cd /application/mysql/
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/data/mysql/data \
-DSYSCONFDIR=/usr/local/mysql -DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 -DMYSQL_UNIX_ADDR=/usr/local/mysql/var/mysql.sock \
-DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci
make && make install

CMake Warning:
Manually-specified variables were not used by the project:
WITH_READLINE
WITH_MEMORY_STORAGE_ENGINE
CMake Warning手动指定的变量未被项目使用,在这里倒没什么事

目录说明:

a.安装完成后不会生成配置文件,不会给你创建/usr/local/mysql/var/目录,

不会给你创建/data/mysql/data,这些都需要手动创建;

b./opt/mysql-5.6.21/是解压目录;

c.-DCMAKE_INSTALL_PREFIX=/usr/local/mysql/是所有配置文件存放目录;

d.-DMYSQL_DATADIR=/data/mysql/data/是数据及日志存放目录;

e.-DSYSCONFDIR=/usr/local/mysql是my.cnf存放目录;

f.-DMYSQL_UNIX_ADDR是socket存放目录.

2.配置并启动

mkdir -p /data/mysql/data
mkdir -p /usr/local/mysql/var
chown -R mysql:mysql /usr/local/mysql
chown -R mysql:mysql /data/mysql/data
cd /usr/local/mysql
# 如果不小心删除了这个数据目录或这个目录下的文件被误操作删除了,可以利用这个命令重新初始化
./scripts/mysql_install_db --basedir=/usr/local/mysql \
--datadir=/data/mysql/data --user=mysql
cp support-files/mysql.server /etc/init.d/mysqld
chkconfig mysqld on
echo "export PATH=/usr/local/mysql/bin:$PATH" >> /etc/profile
source /etc/profile

my.cnf

cat > /usr/local/mysql/my.cnf << EOF
[client]
port = 3306
socket = /usr/local/mysql/var/mysql.sock
  
[mysqld]
port = 3306
socket = /usr/local/mysql/var/mysql.sock
  
basedir = /usr/local/mysql
datadir = /data/mysql/data
pid-file = /data/mysql/data/mysql.pid
user = mysql
bind-address = 0.0.0.0
server-id = 1
sync_binlog=1
log_bin = mysql-bin
  
skip-name-resolve
#skip-networking
back_log = 600
  
max_connections = 3000
max_connect_errors = 3000
##open_files_limit = 65535
table_open_cache = 512
max_allowed_packet = 16M
binlog_cache_size = 16M
max_heap_table_size = 16M
tmp_table_size = 256M
  
read_buffer_size = 1024M
read_rnd_buffer_size = 1024M
sort_buffer_size = 1024M
join_buffer_size = 1024M
key_buffer_size = 8192M
  
thread_cache_size = 8
  
query_cache_size = 512M
query_cache_limit = 1024M
  
ft_min_word_len = 4
  
binlog_format = mixed
expire_logs_days = 30
  
log_error = /data/mysql/data/mysql-error.log
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /data/mysql/data/mysql-slow.log
  
performance_schema = 0
explicit_defaults_for_timestamp
  
##lower_case_table_names = 1
  
skip-external-locking
  
default_storage_engine = InnoDB
##default-storage-engine = MyISAM
innodb_file_per_table = 1
innodb_open_files = 500
innodb_buffer_pool_size = 4096M
innodb_write_io_threads = 1000
innodb_read_io_threads = 1000
innodb_thread_concurrency = 8
innodb_purge_threads = 1
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 4M
innodb_log_file_size = 32M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
  
bulk_insert_buffer_size = 8M
myisam_sort_buffer_size = 8M
myisam_max_sort_file_size = 2G
myisam_repair_threads = 1
  
interactive_timeout = 28800
wait_timeout = 28800
  
[mysqldump]
quick
max_allowed_packet = 16M
  
[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M
  
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
port = 3306
EOF

3.授权

# 第一次给root设置密码
mysqladmin -u root password 'oldboy123'
# 给root修改密码
mysqladmin -uroot -poldboy123 password'oldboy123456'
# 设置root用户可以远程访问
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'Root123!@#' WITH GRANT OPTION;
FLUSH PRIVILEGES;

4.注意事项及常见报错

a.在启动MySQL服务时,会按照一定次序搜索my.cnf,先在/etc目录下找,找不到,则会搜索"$basedir/my.cnf",
在本例中就是 /usr/local/mysql/my.cnf,这是新版MySQL的配置文件的默认位置;

b.在CentOS 6.4版操作系统最小安装完成后,默认会在/etc目录下存在一个my.cnf,
需要将此文件更名为其他的名字,如:/etc/my.cnf.bak(上面的强制卸载,已经把这个文件给删了)
否则,该文件会干扰源码安装的MySQL的正确配置,造成无法启动;

c.在使用"yum update"更新系统后,需要检查下/etc目录下是否会多出一个my.cnf,如果多出,
将它重命名成别的.否则,MySQL将使用这个配置文件启动,可能造成无法正常启动等问题.

d.用ps -ef查看mysql进程,出现两条斜线是因为配置文件中多加了一个斜线:
basedir = /usr/local/mysql/
/usr/local/mysql//bin/mysqld_safe

e.登陆mysql,若是报错如下:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
解决办法:

mkdir /var/lib/mysql/

ln -s /usr/local/mysql/var/mysql.sock /var/lib/mysql/mysql.sock

f.如果启动报错:

service mysql start

Starting MySQL.Logging to '/data/mysql/data/mysql-error.log'.

mysqld_safe Directory '/usr/local/mysql/var' for UNIX socket file don't exists.

ERROR! The server quit without updating PID file (/data/mysql/data/mysql.pid).

解决办法:

mkdir /usr/local/mysql/var
chown -R mysql:mysql /usr/local/mysql
service mysql start
出这个错的原因应该是:在cmake的时候这一句---DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock
导致的,改成指向配置文件中的就应该不会再报错了.

5.centos7下启动mysql时的一个报错

service mysql start

mysqld_safe error: log-error set to '/var/log/mariadb/mariadb.log',

mysqld_safe error: log-error set to '/var/log/mariadb/mariadb.log',however file don't exists.

Create writable for user 'mysql'.The server quit without updating PID file (/data/mysql/data[FAILED]id).

该问题为mysql的一个小bug,临时处理办法为:直接创建一个 log-error 指定的空文件,

并给适当的属主、属组权限即可.该问题已经在版本 5.5.55, 5.6.36, 5.7.18, 8.0.1中修复.

mkdir -p /var/log/mariadb
touch /var/log/mariadb/mariadb.log
chown -R mysql.mysql /var/log/mariadb
service mysql start
ln -s /var/log/mariadb/mariadb.log /data/mysql/data/mysql-error.log
/etc/init.d/mysql restart

 

参考博客:http://www.cnblogs.com/kevingrace/p/6109679.html

posted @ 2018-12-24 17:24  法外狂徒  阅读(370)  评论(0编辑  收藏  举报