1、登录服务器,如果非root用户则切root用户
sudo su -
2、创建用户
groupadd mysql
useradd mysql -g mysql -M -s /sbin/nologin
3、在/usr/tmp目录上传MySQL安装包
4、将安装包移到/opt/byd目录
mv /usr/tmp/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz /opt/byd/
5、解压 & 重命名
tar -zxvf mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.38-linux-glibc2.12-x86_64 mysql5
6、创建data目录 & 赋权
mkdir -p /data/mysql/data
chown -R mysql:mysql /data/mysql
7、配置my.cnf
touch /etc/my.cnf
Vim /etc/my.cnf
[mysqld] basedir=/opt/byd/mysql5 datadir=/opt/byd/mysql5/data port=3306 socket=/opt/byd/mysql5/mysql.sock log-error=/opt/byd/mysql5/log/mysqld.log pid-file=/opt/byd/mysql5/mysqld.pid symbolic-links=0 [mysqld_safe] log-error=/opt/byd/mysql5/run/mariadb.log pid-file=/opt/byd/mysql5/mariadb.pid # # include all files from the config directory # !includedir /etc/my.cnf.d |
8、创建文件
mkdir -p /opt/byd/mysql5/run-->touch /opt/byd/mysql5/run/mariadb.log
mkdir -p /opt/byd/mysql5/log-->touch /opt/byd/mysql5/log/mysqld.log
# 更改目录属主、属组
chown -R mysql:mysql /opt/byd/mysql5
9、初始化mysql、安全启动
1)cd /opt/byd/mysql5/bin/
2)./mysqld --initialize --user=mysql --basedir=/opt/byd/mysql5--datadir=/opt/byd/mysql5/data
报错: Can't find error-message file '/opt/byd/mysql5--datadir=/opt/byd/mysql5/data/share/errmsg.sys'. Check error-message file location and 'lc-messages-dir' configuration directive.
解决:在/etc/my.cnf添加
10、加入系统管理
cp -a /opt/byd/mysql5/support-files/mysql.server /etc/init.d/mysqld
# 赋予可执行权限
chmod 777 /etc/init.d/mysqld
# 添加服务
chkconfig --add mysqld
11、重新启动mysql
service mysqld restart
12、连接数据库
mysql -u root -p-->不输密码直接回车
报错1:-bash: mysql: command not found
解决:建立软链接
ln -s /opt/byd/mysql5/bin/mysql /usr/bin
报错2:Can't connect to local MySQL server through socket '/tmp/mysql.sock'
解决:在/etc/my.cnf添加
报错3:Access denied for user 'root'@'localhost' (using password: YES)
解决:
1)在/etc/my.cnf添加
2)重启mysql
13、修改密码
# 刷新权限
flush privileges;
# 密码设置为123456,永不过期
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456' PASSWORD EXPIRE NEVER;
14、开放远程连接
# 切换至mysql数据库 use mysql; # 远程用户建立,更新登录的用户所允许的IP地址为任意 grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option; # 第一个*是数据库,可以改成允许访问的数据库名称 # 第二个* 是数据库的表名称,*代表允许访问任意的表 # root代表远程登录使用的用户名,可以自定义 # %代表允许任意ip登录,如果你想指定特定的IP,可以把%替换掉就可以了 # 123456代表远程登录时使用的密码 # 刷新权限 flush privileges; # 查看配置是否生效 select host,user from user; |