编译安装MySQL前预准备
首先检查系统中是否存在使用rpm安装的mysql或者mariadb,如果有需要先删除后再编译安装。
卸载完以后用 rpm -qa|grep mariadb 或者 rpm -qa|grep mysql 查看结果。
rpm -qa | grep mysql #查找软件安装包mysql和mariadb
rpm -qa | grep mariadb
rpm -e xxx #卸载
rpm -e --nodeps xxx #卸载不成功时使用此命令强制卸载
安装
(1)依赖以及软件
yum install -y libaio-*
mkdir -p /usr/local/mysql
cd /usr/local/src
wget http://zy-res.oss-cn-hangzhou.aliyuncs.com/mysql/mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
tar -xzvf mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
mv mysql-5.7.17-linux-glibc2.5-x86_64/* /usr/local/mysql/
(2)建立mysql组和用户,并将mysql用户添加到mysql组
groupadd mysql
useradd -g mysql -s /sbin/nologin mysql
(3)初始化mysql数据库
/usr/local/mysql/bin/mysqld --initialize-insecure --datadir=/usr/local/mysql/data/ --user=mysql
(4)更改mysql安装目录的属主属组
chown -R mysql:mysql /usr/local/mysql
(5)设置开机自启
#第三句:添加执行权限
cd /usr/local/mysql/support-files/
cp mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
vim /etc/rc.d/rc.local
/etc/init.d/mysqld start #写入rc.local 保存退出(:wq)
(6)设置环境变量
vi /root/.bash_profile
/usr/local/mysql/bin:/usr/local/mysql/lib #add
source /root/.bash_profile
(7)启动MySQL数据库
/etc/init.d/mysqld start
(8)修改密码
#登录mysql后操作
set password for'root'@'localhost'=password('jtnyHistm0!j');
(9)开机自启
systemctl enable mysqld.service
开放远程连接
mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'jtnyHistm0!j(这是密码)' WITH GRANT OPTION;
//远程连接数据库的时候需要输入用户名和密码
用户名:root
密码:'jtnyHistm0!j(这是密码)'
指点ip:%代表所有Ip,此处也可以输入Ip来指定Ip
输入后使修改生效还需要下面的语句
mysql>FLUSH PRIVILEGES;
修改密码
mysqladmin -uroot -p password newpassword
再输入旧密码 即可!
例如:
[root@iZwz9c4hgh9j5wntdcvzljZ etc]# mysqladmin -uroot -p password 'jtnyHistm0!j';
Enter password:
可能会出现的问题
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (111)
find / -name mysql.sock
# /tmp/mysql.sock
vi my.ini
[client]
character-set-server = utf8
port = 3306
socket = /tmp/mysql.sock
systemctl restart mysqld.service
重启后出现的问题
Starting MySQL./usr/local/mysql/bin/mysqld_safe: line 586: /var/lib/mysql/mysqld_safe.pid: No such f
awk: (FILENAME=- FNR=1) warning: error writing standard output (Broken pipe)
2017-08-16T15:38:07.442738Z mysqld_safe Directory '/var/lib/mysql' for UNIX socket file don't exists The server quit without updating PID file (/var/lib/mysql/i[FAILED]k450a71dbd83kZ.pid).
vi /etc/my.cnf
将所有的 #/var/run/mysql
改成 #/usr/local/mysql
The server quit without updating PID file (/usr/local/mysql/iZ2ze9ikxk450a71dbd83kZ.pid)
#在[mysqld]中添加:
vi /etc/my.cnf
datadir = /usr/local/mysql/data
log-error = /usr/local/mysql/data/error.log
pid-file = /usr/local/mysql/data/mysql.pid
user = mysql
tmpdir = /tmp
连接数据库时Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
vi /etc/my.cnf
[mysqld]
socket=/usr/local/var/mysql.sock
[client]
socket=/usr/local/var/mysql.sock