下载:https://dev.mysql.com/downloads/mysql/
1.先确保机器上没有安装mysql,可以通以下命令查看:
yum list installed |grep mysql
rpm -qa |grep mysql
find /* -name mysql
如果有mysql相关的目录的话,删除对应的目录就行了
2.创建用户和用户组
groupadd mysql
useradd -g mysql mysql
3.把包上传到/usr/local目录下并且解压:
然后重新命名一下,删除安装包:
过程如下:
[root@localhost local]# ls
bin etc games include lib lib64 libexec mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz sbin share src
[root@localhost local]# tar -xzvf mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz
......
[root@localhost local]# ls
bin etc games include lib lib64 libexec mysql-5.7.36-linux-glibc2.12-x86_64 mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz sbin share src
[root@localhost local]# mv mysql-5.7.36-linux-glibc2.12-x86_64 mysql
[root@localhost local]# rm -rf mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz
[root@localhost local]# ls
bin etc games include lib lib64 libexec mysql sbin share src
然后创建MySQL的配置文件,如果之前有安装过MySQL,建议删除或者备份/etc/my.cnf这个配置文件
vim /etc/my.cnf
[mysqld]
bind-address=0.0.0.0
port=3306
user=mysql
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
log-error=/var/log/mysql/mysql.log
pid-file=/usr/local/mysql/mysql.pid
symbolic-links=0
lower_case_table_names=1 #设置不区分大小
#innodb_buffer_pool_size=128M #有的机器不加这个会报错
#skip-grant-tables
注意:进入到mysql的bin目录下执行
./mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
如果有报错的话,大多是一些权限的问题,比如无法创建data目录等等。
如果报错: "[ERROR] Could not open file '/var/log/mysql/mysql.log' for error logging: No such file or directory"
可以进入 /usr/local 执行:chown -R mysql:mysql mysql (因为我的mysql是安装在/usr/lcoal下)
需要手动创建一下目录:mkdir -p /var/log/mysql
然后赋一下权限:chown -R mysql:mysql /var/log/mysql
然后再编译!
执行完没有报错后进入mysql目录下的support-files目录执行启动命令
[root@localhost support-files]# ./mysql.server start
Starting MySQL. SUCCESS!
然后进入mysql的bin目录执行登录命令
[root@localhost bin]# ./mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.36 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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>
登录成功!
设置开机启动并且设置密码登录
登录到mysql中执行命令,设置自己的密码
新:alter user 'root'@'localhost' identified by '密码';
旧:set password for root@localhost=password('密码');
设置所有人都可以连接数据库:
> use mysql;
> grant all on *.* to root@'%' identified by '密码';
> use mysql;
> select user,host from user;
+---------------+-----------+
| user | host |
+---------------+-----------+
| root | % |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+---------------+-----------+
5 rows in set (0.00 sec)
如果有报错:
1.修改MySQL数据库配置文件无密码登录后,修改密码报错:
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
2.找不到初始密码可以在/etc/my.conf中[mysqld] 添加:
skip-grant-tables
3.先执行:
flush privileges; #登录到mysql命令行里面执行
4.再执行修改密码命令,可以了
5.设置完后记得删除或者注释/etc/my.cnf配置文件中新添加的skip-grant-tables
操作如下:
mysql> alter user 'root'@'localhost' identified by '1';
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
mysql> flush privileges;
Query OK, 0 rows affected (0.04 sec)
mysql> alter user 'root'@'localhost' identified by '1';
Query OK, 0 rows affected (0.00 sec)
设置开机启动
设置开机启动
[root@localhost mysql]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
#inint.d文件都是用来放服务脚本的
[root@localhost etc]# ll |grep init.d
lrwxrwxrwx. 1 root root 11 11月 15 06:22 init.d -> rc.d/init.d
[root@localhost mysql]# systemctl enable mysql
mysql.service is not a native service, redirecting to /sbin/chkconfig.
Executing /sbin/chkconfig mysql on
修改环境变量
在/etc/profile 最后面新增一行
#mysql
export PATH=/usr/local/mysql/bin:$PATH
保存退出
[root@localhost mysql]# source /etc/profile
搞定!
设置systemctl启动
vim /usr/lib/systemd/system/mysql
[Unit]
Description=MySQL Server
After=network.target
After=syslog.target
[Service]
User=mysql
Group=mysql
Type=forking
PermissionsStartOnly=true
ExecStart= /etc/init.d/mysql start
ExecStop= /etc/init.d/mysql stop
ExecReload= /etc/init.d/mysql restart
LimitNOFILE = 5000
[Install]
WantedBy=multi-user.target
1.重新加载一下服务的配置文件
systemctl daemon-reload
2.开启mysql服务
systemctl start mysql
3.关闭mysql服务
systemctl stop mysql
4.查看mysql状态关闭mysql服务
systemctl status mysql
在安装的过程中可能还会报其他的错误,例如:
安装过程中会报错:
[root@localhost /]# cd /usr/local/mysql/
[root@localhost mysql]# ./support-files/mysql.server start
Starting MySQL... ERROR! The server quit without updating PID file (/data/mysql/mysql.pid).
[root@localhost mysql]# ps -ef |grep mysql
root 8256 7316 0 17:53 pts/0 00:00:00 tail -f mysql.log
root 8838 8261 0 17:56 pts/1 00:00:00 grep --color=auto mysq
发现mysql也没有进程
[root@localhost mysql]# mysql -uroot -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
然后进入到/tmp/mysql.sock下面:
[root@localhost mysql]# cd /tmp
[root@localhost tmp]# ls
mysql.sock
mysql.sock.lock
[root@localhost tmp]# ll
总用量 4
lrwxrwxrwx. 1 root root 25 12月 2 11:27 mysql.sock -> /var/lib/mysql/mysql.sock
发现以前安装的有软连接:mysql.sock -> /var/lib/mysql/mysql.sock
然后删除软连接:
[root@localhost tmp]# rm -rf mysql.sock
然后再进入mysql目录:
[root@localhost tmp]# cd /usr/local/mysql/
[root@localhost mysql]# ./support-files/mysql.server start
Starting MySQL. SUCCESS!
启动成功!
[root@localhost mysql]# mysql -u root -p
Enter password:123456
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.36 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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>
登录成功!
其他的报错情况:
[root@localhost bin]# ./mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
2021-12-06T09:13:56.979254Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-12-06T09:13:56.981531Z 0 [ERROR] Could not open file '/var/log/mysql/mysql.log' for error logging: No such file or directory
2021-12-06T09:13:56.981560Z 0 [ERROR] Aborting
[root@localhost bin]# chown -R mysql:mysql /var/log/mysql
chown: 无法访问"/var/log/mysql": 没有那个文件或目录
[root@localhost bin]# mkdir -p /var/log/mysql
[root@localhost bin]# chown -R mysql:mysql /var/log/mysql
[root@localhost bin]# ./mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
[root@localhost bin]# cd ../support-files/
[root@localhost support-files]# ls
magic mysqld_multi.server mysql-log-rotate mysql.server
[root@localhost support-files]# ./mysql.server start
Starting MySQL. ERROR! The server quit without updating PID file (/usr/local/mysql/mysql.pid).
[root@localhost support-files]# chown -R mysql:mysql /usr/local/mysql/
[root@localhost support-files]# ./mysql.server start
Starting MySQL. SUCCESS!
然后进入到mysql的bin目录下面执行:./mysql 就可以进入到命令行了。