1.下载和解压mysql数据库
wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.11-linux-glibc2.5-x86_64.tar.gz
(32位的下载 wget 'http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.16-linux-glibc2.5-i686.tar.gz')
sudo tar -zxvf mysql-5.7.11-linux-glibc2.5-x86_64.tar.gz
sudo cp -r mysql-5.7.11-linux-glibc2.5-x86_64 /usr/local/
cd /usr/local
sudo mv mysql-5.7.11-linux-glibc2.5-x86_64/ mysql-5.7.11
2.建立软链接
sudo ln -s mysql-5.7.11 mysql
3.创建mysql用户和修改软件的权限(系统的默认配置指定了操作系统必须以mysql用户来运行mysqld进程,所以要创建mysql用户并确保其有相关权限)
sudo -s
useradd -r -M -s /sbin/nologin mysql
chown -R mysql.mysql /usr/local/mysql-5.7.11
chown -R mysql.mysql /usr/local/mysql
chgrp -R mysql /usr/local/mysql-5.7.11
4.安装和初始化数据库
cd /usr/local/mysql/
bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ --必须在mysql根目录安装mysql,否则出错
(有可能会会报错/usr/local/mysql/bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory 这个时候yum install libaio即可
或者执行bin/mysqld –initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ --defaults-file=/etc/my.cnf
)
sudo rm /etc/my.cnf
cp -a ./support-files/my-default.cnf /etc/my.cnf
sudo rm /etc/init.d/mysqld
cp -a ./support-files/mysql.server /etc/init.d/mysqld
cd bin/
/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf &
mysql (此时如果出现ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock',请检查my.cnf中socket = /var/lib/mysql/mysql.sock行是否被注释)
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) 看第5步
/etc/init.d/mysqld restart
Shutting down MySQL.. [ OK ]
Starting MySQL... [ OK ]
#开机启动
chkconfig --level 35 mysqld on
5.初始化密码
mysql5.7会生成一个初始化密码,而在之前的版本首次登陆不需要登录。
shell> cat /root/.mysql_secret
***
mysql -uroot -p
Enter password: ***
ERROR 1862 (HY000): Your password has expired. To log in you must change it using a client that supports expired passwords.
kill mysql pid
/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf --skip-grant-tables &
mysql
mysql> select user,host,password_expired from mysql.user;
+-----------+-----------+------------------+
| user | host | password_expired |
+-----------+-----------+------------------+
| root | localhost | Y |
| mysql.sys | localhost | N |
+-----------+-----------+------------------+
2 rows in set (0.00 sec)
mysql> update mysql.user set password_expired='N' where user='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update mysql.user set authentication_string=password('root') where user='root' and Host = 'localhost';
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)
mysql> exit
mysql -hlocalhost -uroot -proot
哈 大功告成!!