大家好,今天给大家分享mysql相关的技术。
我们在深入学习mysql的时候,我们不会在window安装mysql,总需要在CentOS下搭建一个mysql server,然而,如果采用源码包的安装方式会特别费时间,需要安装各种软件依赖,然后make&&make install,并且会经常安装失败。今天我给大家分享一下mysql二进制包的安装(针对mysql5.7,与mysql5.6安装方式略有不同)。
1、下载mysql
输入网址,www.mysql.com,找到mysql的下载页,我们选择5.7版本64位的包点击下载会得到一个tar.gz的压缩包,或者直接用wget进行下载。
2、安装前目录规划
为了方便起见,我们将mysql安装在/usr/local下,解压,做软连接
创建mysql用户组和mysql用户,修改mysql安装目录权限和数据文件目录权限
修改配置文件datadir=/data/mysql
3、初始化数据库
命令:
cd /usr/local/mysql/bin
./mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/data/mysql/ --user=mysql --initialize
4、启动mysql
命令
cd /usr/local/mysql/bin/
./mysqld_safe --defaults-file=/etc/my.cnf &
启动失败,填坑
报错1:找不到/var/log/mariadb/mariadb.log
方法:创建/var/log/mariadb/mariadb.log文件并修改权限
2019-07-17T08:57:21.589621Z mysqld_safe error: log-error set to '/var/log/mariadb/mariadb.log', however file don't exists. Create writable for user 'mysql'.
[root@localhost bin]# mkdir /var/log/mariadb
[root@localhost bin]# touch /var/log/mariadb/mariadb.log
[root@localhost bin]# chown mysql:mysql -R /var/log/mariadb/
[root@localhost bin]# cd /var/log/mariadb/
[root@localhost mariadb]# ll
total 0
-rw-r--r--. 1 mysql mysql 0 Jul 17 17:06 mariadb.log
[root@localhost mariadb]# chmod 777 mariadb.log
[root@localhost mariadb]# chmod 777 /var/log/mariadb/
[root@localhost mariadb]# ll
total 0
-rwxrwxrwx. 1 mysql mysql 0 Jul 17 17:06 mariadb.log
再启动,报错2:找不到/var/lib/mysql目录
[root@localhost bin]# ./mysqld_safe --defaults-file=/etc/my.cnf &
[1] 6487
[root@localhost bin]# 2019-07-17T09:11:05.236306Z mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
2019-07-17T09:11:05.239773Z mysqld_safe Directory '/var/lib/mysql' for UNIX socket file don't exists.
方法:创建相应目录并修改权限
[root@localhost bin]# mkdir /var/lib/mysql
[root@localhost bin]# chown mysql:mysql /var/lib/mysql/
[root@localhost bin]# chmod 777
[root@localhost bin]# chown mysql:mysql -R /var/lib/mysql/
[root@localhost bin]# chmod 777 /var/lib/mysql/
再启动,报错3:找不到/var/run/mariadb/mariadb.pid文件
[root@localhost bin]# ./mysqld_safe --defaults-file=/etc/my.cnf &
[1] 22072
[root@localhost bin]# 2019-07-17T09:20:17.388351Z mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
2019-07-17T09:20:17.416303Z mysqld_safe Starting mysqld daemon with databases from /data/mysql
2019-07-17T09:20:17.935778Z mysqld_safe mysqld from pid file /var/run/mariadb/mariadb.pid ended
方法:创建相应目录并修改权限
[root@localhost bin]# mkdir /var/run/mariadb
[root@localhost bin]# chown mysql:mysql -R /var/run/mariadb/
[root@localhost bin]# chmod 777 /var/run/mariadb/
再启动,启动成功
5、登录mysql,修改临时密码
还记的在初始化mysql时生成的那个临时密码吗?对,我们就用那个密码登录。但是登录报错,原因是/tmp/下没有找到mysql.sock文件。于是我们把/var/lib/mysql/mysql.sock做一个软链到/tmp/mysql.sock,再次登陆,登陆成功。
别忘了修改再初始化时给root生成的临时密码哦。
修改root密码,并设置为永远不过期,刷新权限。
mysql> set password='root';
Query OK, 0 rows affected (0.00 sec)
mysql> alter user 'root'@'localhost' password expire never;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql>
退出后再次登录:登录成功
[root@localhost bin]# ./mysql -uroot -proot
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.26 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
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>
6、mysql的关闭
[root@localhost mysql]# cd /usr/local/mysql/bin/
[root@localhost bin]# ./mysqladmin -uroot -proot shutdown
7、小提示
我们可以在mysql库的user表查到用户的相关信息,在mysql5.7之前,用user表中的password字段来表示用户的密码,在mysql5.7及以后,改用authentication_string字段来表示,如下:
mysql> select user,host,authentication_string from user;
+---------------+-----------+-------------------------------------------+
| user | host | authentication_string |
+---------------+-----------+-------------------------------------------+
| root | localhost | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| mysql.session | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| mysql.sys | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
+---------------+-----------+-------------------------------------------+
3 rows in set (0.00 sec)
好了,今天的分享就到这里,添加笔者微信可以获得更多使用的后端技术哦,谢谢大家