centos7 mysql-5.6.40 服务源码安装
新手上路,踩个脚印
原文链接:https://blog.csdn.net/chenliang1038/article/details/82229908
安装地址:/usr/local/
安装MySQL版本为:mysql-5.6.40
检查是否安装了mariadb
[root@jack169 ~]#rpm -qa | grep mariadb
如果检查到有mariadb则卸载
[root@jack169 ~]#rpm -e --nodeps 文件名(检查到的文件)
删除掉已有的配置文件/etc/my.cnf
[root@jack169 ~]#rm -rf /etc/my.cnf
1、检查是否安装了mysql
[root@jack169 ~]#rpm -qa | grep mysql
如果检查到有mysql则卸载
[root@jack169 ~]#rpm -e –nodeps 文件名(检查到的文件)
2、下载MySQL-5.6.40
[root@jack169 ~]# wget http://ftp.ntu.edu.tw/MySQL/Downloads/MySQL-5.6/mysql-5.6.40.tar.gz
另附MySQL其他版本源码下载地址,文中下载链接可能失效了。
https://blog.csdn.net/AinUser/article/details/81414627
下载其他版本可能会报错确实boots库,解决办法:
https://blog.csdn.net/u012767761/article/details/78185768
3、解压MySQL-5.6.40
[root@jack169 ~]# tar -xzvf mysql-5.6.40.tar.gz
[root@jack169 ~]# yum install cmake ncurses-devel ncurses gcc-c++ openssl-devel tcp_wrappers-devel bison-devel bison -y
4、把解压包移动至/usr/local/
[root@jack169 ~]# mv mysql-5.6.40 /usr/local/
5、新建用户组
[root@jack169 ~]#groupadd mysql
[root@jack169 ~]#useradd -g mysql -s /sbin/nologin mysql
6、新建目录/usr/local/mysql和/usr/local/mysql/data
[root@jack169 ~]#mkdir /usr/local/mysql #MySQL安装目录存放位置
[root@jack169 ~]#mkdir /usr/local/mysql/data #mysql数据库存放位置
7、mysql用户赋予权限
[root@jack169 ~]#chown -R mysql:mysql /usr/local/mysql
8、预编译MySQL
[root@jack169 ~]# cd /usr/local/mysql-5.6.40
[root@jack169 mysql-5.6.40]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DSYSCONFDIR=/etc -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DMYSQL_UNIX_ADDR=/var/lib/mysql/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
解释其含义:
DCMAKE_INSTALL_PREFIX=/usr/local/mysql #cmake预编译
DMYSQL_DATADIR=/usr/local/mysql/data #MySQL数据库存放路径
DSYSCONFDIR=/etc #配置文件路径
DWITH_MYISAM_STORAGE_ENGINE=1 #开启MYISAM引擎支持
DWITH_INNOBASE_STORAGE_ENGINE=1 #开启InnoDB引擎支持
DWITH_MEMORY_STORAGE_ENGINE=1 #开启MEMORY引擎支持
DWITH_PARTITION_STORAGE_ENGINE=1 #开启PARTITION引擎支持
DWITH_READLINE=1 #启用快捷键功能
DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock #socket通信文件为至
DMYSQL_TCP_PORT=3306 #MySQL监听端口
DMYSQL_USER=mysql #mysql运行用户
DENABLED_LOCAL_INFILE=1 #允许从本地导入数据
DEXTRA_CHARSETS=all #安装所有扩展字符集
DDEFAULT_CHARSET=utf8 #默认字符集为utf8
DDEFAULT_COLLATION=utf8_general_ci #检查字符
注意:如果编译失败时 需要重新编译时,一定要清除旧的对象文件和缓存信息
清除旧的对象文件: make clean
清除缓存信息:rm -rf CMakeCache.txt
清除配置文件:rm -rf /etc/my.cnf
9、编译和安装
[root@jack169 mysql-5.6.40]# make && make install
这个过程需要30分钟左右耐心等待(看个人的电脑配置,所需要的时间不一样)
10、对mysql数据库进行初始化
[root@jack169 mysql-5.6.40]#cd /usr/local/mysql
[root@jack169 mysql]#./scripts/mysql_install_db -basedir=/usr/local/mysql -datadir=/usr/local/mysql/data -user=mysql
11、拷贝服务脚本到init.d目录
[root@jack169 mysql]#cp support-files/mysql.server /etc/init.d/mysql
[root@jack169 mysql]#chkconfig mysql on
[root@jack169 mysql]#ln -s /usr/local/mysql/bin/* /usr/bin/
12、新建文件/var/log/mariadb和/var/log/mariadb/mariadb.log和 /var/lib/mysql
[root@jack169 mysql]#mkdir /var/log/mariadb #新建mariadb目录
[root@jack169 mysql]#touch /var/log/mariadb/mariadb.log 新建mariadb.log文件
注意:不能用下面的这种创建文件,我尝试过如果是下面这种是会报错的
[root@jack169 mysql]#mkdir -p /var/log/mariadb/mariadb.log
[root@jack169 mysql]#chown -R mysql:mysql /var/log/mariadb/ #给mysql用户权限
[root@jack169 mysql]#mkdir /var/lib/mysql #新建/mysql目录
[root@jack169 mysql]#chown -R mysql:mysql /var/lib/mysql #给mysql用户权限
注意:如果你不建这个mariadb目录下mariadb.log文件,还有mysql目录。你就启动MySQL,会一直报错,很头痛,
*13、以上设置还不能成功启动MySQL需要修改MySQL的配置文*件/etc/my.cnf****
上面的两个位置要一样
[root@jack169 ~]# vim /etc/my.cnf
[mysqld]
datadir=/usr/local/mysql/data
socket=/var/lib/mysql/mysql.sock
把datadir的路径给为自己设置的路径
14、启动MySQL
[root@jack169 ~]#service mysql start
15、设置环境变量
[root@jack169 ~]#vim /etc/profile
export PATH=$PATH:/usr/local/mysql/bin
[root@jack31 ~]#source /etc/profile
OK!!!!!!
16、MySQL初始化密码设置
[root@jack711 ~]# service mysql start
[root@jack711 ~]# mysql
~~~~~~~~
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> update user set password=password('123456') where user='root';
mysql> flush privileges
mysql> exit
[root@jack711 ~]# mysql -uroot -p
Enter password: ***你的密码***
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.40 Source distribution
17、在密码忘记了时:破解登录MySQL数据库
1、编辑配置文件
[root@jack711 ~]# vim /etc/my.cnf
找到如下类似如下内容:
[mysqld]
datadir=/usr/local/mysql/data
socket=/var/lib/mysql/mysql.sock
并添加如下内容:
skip-grant-tables
1
保存退出
2、重启mysql数据库,就可以免密码登录
[root@jack711 ~]# service mysql start
[root@jack711 ~]# mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> update user set password=password('123456') where user='root';
mysql> flush privileges
mysql> exit
[root@jack711 ~]# mysql -uroot -p
Enter password: ***你的密码***
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.40 Source distribution
忘记密码:
停止服务
[root@jack711 ~]#mysqld_safe --skip-grant-tables --user=mysql &
[root@jack711 ~]#
mysql -S /usr/local/mysql57/data/mysql.sock
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 1
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
报错:
Starting MySQL.Logging to '/usr/local/mysql/data/localhost.localdomain.err'.
. ERROR! The server quit without updating PID file (/usr/local/mysql/data/localhost.localdomain.pid).
解决:
进入/var/lib目录下创建一个mysql文件夹解决
[root@bogon mysql]# cd /var/lib
[root@bogon lib]# mkdir mysql