linux上安装mysql5.7
1、下载tar包,这里使用wget从官网下载
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
2、将mysql安装到/usr/local/mysql下
# 解压
tar -xvf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
# 移动
mv mysql-5.7.22-linux-glibc2.12-x86_64 /usr/local/
# 重命名
mv /usr/local/mysql-5.7.22-linux-glibc2.12-x86_64 /usr/local/mysql
3、新建data目录
mkdir /usr/local/mysql/data
4、新建mysql用户、mysql用户组
# mysql用户组 groupadd mysql # mysql用户 useradd mysql -g mysql
5、将/usr/local/mysql的所有者及所属组改为mysql
chown -R mysql.mysql /usr/local/mysql
6、配置
/usr/local/mysql/bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data
# 如果出现以下错误:
2018-07-14 06:40:32 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize 2018-07-14 06:40:32 [ERROR] Child process: /usr/local/mysql/bin/mysqldterminated prematurely with errno= 32 2018-07-14 06:40:32 [ERROR] Failed to execute /usr/local/mysql/bin/mysqld --bootstrap --datadir=/usr/local/mysql/data --lc-messages-dir=/usr/local/mysql/share --lc-messages=en_US --basedir=/usr/local/mysql -- server log begin -- -- server log end --
# 则使用以下命令:
/usr/local/mysql/bin/mysqld --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data --initialize
# 如果出现以下错误:
/usr/local/mysql/bin/mysqld: error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory
# 则执行以下命令:
yum -y install numactl
yum -y install libiao
# 完成后继续安装:
/usr/local/mysql/bin/mysqld --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data --initialize
# 编辑/etc/my.cnf
[mysqld] datadir=/usr/local/mysql/data basedir=/usr/local/mysql socket=/tmp/mysql.sock user=mysql port=3306 character-set-server=utf8
# 取消密码验证
skip-grant-tables # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 # skip-grant-tables [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid
7、开启服务
# 将mysql加入服务
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
# 开机自启
chkconfig mysql on
# 开启
service mysql start
8、设置密码
# 登录(由于/etc/my.cnf中设置了取消密码验证,所以此处密码任意)
/usr/local/mysql/bin/mysql -u root -p
# 操作mysql数据库
mysql>use mysql;
# 修改密码
注意:
mysql> update user set password=password(“新密码”) where user=”用户名”;
执行后报错 ERROR 1054(42S22) Unknown column 'password' in ‘field list’ 错误的原因是 5.7版本下的mysql数据库下已经没有password这个字段了,password字段改成了authentication_string
请使用以下语句
mysql> update user set password=password("*******") where user="*******"; #修改密码报错 ERROR 1054 (42S22): Unknown column 'password' in 'field list' mysql> update mysql.user set authentication_string=password('*******') where user='*******'; #修改密码成功 Query OK, 1 row affected, 1 warning (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 1 mysql> flush privileges; #立即生效 Query OK, 0 rows affected (0.00 sec) mysql> quit Bye n>mysql -u ******* -p #以该用户登录成功. Enter password: ******** ………………………… mysql>
9、将/etc/my.cnf中的skip-grant-tables删除
10、登录再次设置密码(不知道为啥如果不再次设置密码就操作不了数据库了)
/usr/local/mysql/bin/mysql -u root -p
>>ALTER USER 'root'@'localhost' IDENTIFIED BY '修改后的密码';
>>exit;
11、允许远程连接
/usr/local/mysql/bin/mysql -u root -p
>>use mysql;
>>update user set host='%' where user = 'root';
>>flush privileges;
>>eixt;
12、添加快捷方式
ln -s /usr/local/mysql/bin/mysql /usr/bin
重置密码解决MySQL for Linux错误 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
一般这个错误是由密码错误引起,解决的办法自然就是重置密码。 假设我们使用的是root账户。 1.重置密码的第一步就是跳过MySQL的密码认证过程,方法如下: #vim /etc/my.cnf(注:windows下修改的是my.ini) 在文档内搜索mysqld定位到[mysqld]文本段: /mysqld(在vim编辑状态下直接输入该命令可搜索文本内容) 在[mysqld]后面任意一行添加“skip-grant-tables”用来跳过密码验证的过程,保存文档并退出 2.接下来我们需要重启MySQL: /etc/init.d/mysql restart(有些用户可能需要使用/etc/init.d/mysqld restart) 3.重启之后输入#mysql即可进入mysql。 4.接下来就是用sql来修改root的密码 mysql> use mysql; mysql> update user set password=password("你的新密码") where user="root"; mysql> flush privileges; mysql> quit 到这里root账户就已经重置成新的密码了。 5.编辑my.cnf,去掉刚才添加的内容,然后重启MySQL。大功告成!