linux下安装mysql
官网下载地址:https://downloads.mysql.com/archives/community/
mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
cd 到安装包所在路径,例如:cd /usr/local/share/soft/
tar -xzvf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
将其解压到指定路径,并且通过mv命令修改名字为 mysql
mv 解压出来的文件名 mysql
[root@localhost ~]# groupadd mysql
[root@localhost ~]# useradd -r -g mysql mysql
[root@localhost ~]# chown -R mysql.mysql /usr/local/share/soft/mysql
[root@localhost mysql]# mkdir data #进入mysql文件夹
安装依赖包
[root@localhost mysql]# yum install libaio
进行初始化
[root@localhost mysql]# /usr/local/share/soft/mysql/bin/mysqld --user=mysql --basedir=/usr/local/share/soft/mysql --datadir=/usr/local/share/soft/mysql/data --initialize
本文my.cnf的配置如下:
vi /etc/my.cnf
在配置文件中写入以下内容:
[mysqld]
datadir=/usr/local/share/soft/mysql/data
basedir=/usr/local/share/soft/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
[root@localhost mysql]# cp /usr/local/share/soft/mysql/support-files/mysql.server /etc/init.d/mysql
[root@localhost mysql]# chkconfig mysql on
[root@localhost mysql]# service mysql start
重启:
[root@localhost mysql]# service mysql restart
停止:
[root@localhost mysql]# service mysql stop
在配置文件/etc/my.cnf中跳过密码登陆,所以可以免密登录
[root@localhost mysql]# /usr/local/share/soft/mysql/bin/mysql -uroot -p
可以加到环境变量
编辑配置文件
[root@localhost mysql]# vim /etc/profile
[root@localhost mysql]# source /etc/profile
这是可以使用
[root@localhost mysql]# mysql -u root -p
上面我们在配置文件中写了skip-grant-tables 是跳过密码验证不用登录的,我们可以通过如下操作设置密码.
/usr/local/share/soft/mysql/bin/mysql -uroot -p
mysql>use mysql;
mysql> ALTER USER 'root'@'%'IDENTIFIED BY 'Aw5332302@';
mysql>flush privileges;
如果执行修改密码时报错:
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables
这个时候只需要刷新一下权限即可:
mysql>flush privileges;
然后按照上面修改密码的方式,再次尝试进行修改密码,即可修改成功。
mysql>exit;
版本不同修改密码的语句可能不同,如果有一下错误可进行其他方式修改密码:
报错信息:
ERROR 1054 (42S22): Unknown column ‘password’ in ‘field list’
原因:
是从mysql 5.7开始,password字段被替换为了authentication_string
修改:
update user set authentication_string=password(‘填入新密码”’) where user=‘root’;
ALTER USER ‘root’@'localhost’IDENTIFIED BY ‘Zw55350002@’;
mysql> use mysql;
mysql> select user,host from user;
mysql> update user set host='%' where user = 'root';
mysql>flush privileges;
mysql>exit;