centos7 安装mysql5.7 yum和源码包安装
安装数据库mysql 5.7.22
用rpm方式安装 下载rpm包 yum安装
wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
yum -y install mysql57-community-release-el7-10.noarch.rpm
yum -y install mysql-community-server
开启数据库
systemctl start mysqld.service
systemctl status mysqld.service
systemctl enable mysqld.service
查看密码
grep "password" /var/log/mysqld.log
进入数据库后初始化密码,要不无法操作
更改密码
alter user 'root'@'localhost' identified by '#20as3SElksds0ew98';
刷新
flush privileges;
查看数据库
show databases;
进入数据库授权
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '#20as3SElksds0ew98';
刷新权限
FLUSH PRIVILEGES;
更改密码长度和策略
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password_length=0;
Query OK, 0 rows affected (0.00 sec)
mysql> alter user 'root'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
更改存储目录
mkdir /home/data/
复制
cp -R /var/lib/mysql /home/data/
授予权限
chown -R mysql:mysql /home/data/mysql
修改配置文件/etc/my.cnf
datadir=/home/data/mysql
重启mysql
systemctl restart mysqld
查看mysql的存储目录
mysql> show variables like '%datadir%';
+---------------+-------------------+
| Variable_name | Value |
+---------------+-------------------+
| datadir | /home/data/mysql/ |
+---------------+-------------------+
1 row in set (0.00 sec)
源码包安装
下载mysql 源码包
cd /usr/local/src/
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.20.tar.gz
依赖 boost
wget --no-check-certificate http://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz
yum安装依赖的软件包
yum install -y cmake bison bison-devel libaio-devel gcc gcc-c++ git ncurses-devel
解压MySQL
tar xf mysql-5.7.20.tar.gz
把boost转移到mysql目录下
mv boost_1_59_0.tar.gz mysql-5.7.20
cd mysql-5.7.20
解压
tar xf boost_1_59_0.tar.gz
mkdir configure
cd configure
用 make编译
cmake .. -DBUILD_CONFIG=mysql_release \
-DINSTALL_LAYOUT=STANDALONE \
-DCMAKE_BUILD_TYPE=RelWithDebInfo \
-DENABLE_DTRACE=OFF \
-DWITH_EMBEDDED_SERVER=OFF \
-DWITH_INNODB_MEMCACHED=ON \
-DWITH_SSL=bundled \
-DWITH_ZLIB=system \
-DWITH_PAM=ON \
-DCMAKE_INSTALL_PREFIX=/var/mysql/ \
-DINSTALL_PLUGINDIR="/var/mysql/lib/plugin" \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EDITLINE=bundled \
-DFEATURE_SET=community \
-DCOMPILATION_COMMENT="MySQL Server (GPL)" \
-DWITH_DEBUG=OFF \
-DWITH_BOOST=..
创建mysql用户
useradd -s /sbin/nologin mysql
[root@localhost configure]# mkdir /mysql_data
[root@localhost configure]# mkdir /var/mysql/log
[root@localhost configure]# chown -R mysql:mysql /mysql_data/
[root@localhost configure]# chown -R mysql:mysql /var/mysql/log
[root@localhost configure]# vi /etc/my.cnf
配置文件
[mysqld]
port=3306
explicit_defaults_for_timestamp=true
#socket=/var/lib/mysql/mysql.sock
datadir=/mysql_data
log_error=/var/mysql/log/error.log
basedir=/var/mysql/
复制到启动文件
cp /var/mysql/support-files/mysql.server /etc/init.d/mysqld
修改配置文件
vi /etc/init.d/mysqld
basedir=/var/mysql/
datadir=/mysql_data
改变环境变量
vi /root/.bash_profile
PATH=$PATH:$HOME/bin:/var/mysql/bin
source /root/.bash_profile
初始化数据库
/var/mysql/bin/mysqld --initialize --user=mysql
开启数据库
/etc/init.d/mysqld start
查看密码
cat /var/mysql/log/error.log |grep ' password'
2018-08-01T11:17:56.528561Z 1 [Note] A temporary password is generated for root@localhost: fr!n_l06JNa(
进入数据库
mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.20
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> exit
更改密码
alter user 'root'@'localhost' identified by '#20as3SElksds0ew98';