第16章MySQL数据库安装与实现
16.1 安装MySQL
创建用户组和相应的虚拟用户
[root@localhost ~]# groupadd mysql
[root@localhost ~]# useradd mysql -g mysql -M -s /sbin/nologin
[root@localhost ~]# id mysql
uid=1004(mysql) gid=1004(mysql) 组=1004(mysql)
安装MySQL的软件
[root@localhost ~]# mkdir -p /home/girl/tools
[root@localhost ~]# cd /home/girl/tools
[root@localhost tools]# rz -y
rz waiting to receive.
zmodem trl+C ȡ
100% 23480 KB 23480 KB/s 00:00:01 0 Errors
[root@localhost tools]# tar xf mysql-5.1.72.tar.gz
[root@localhost tools]# ls
lrzsz-0.12.20 lrzsz-0.12.20.tar.gz mysql-5.1.72 mysql-5.1.72.tar.gz
[root@localhost tools]# ls -l
总用量 23768
drwxrwxrwx 9 406 disk 4096 10月 24 05:33 lrzsz-0.12.20
-rw-r--r-- 1 root root 280938 12月 31 1998 lrzsz-0.12.20.tar.gz
drwxrwxrwx 32 7155 wheel 4096 9月 10 2013 mysql-5.1.72
-rw-r--r-- 1 root root 24044338 10月 24 2019 mysql-5.1.72.tar.gz
配置文件
./configure \
--prefix=/application/mysql5.1.72/data \
--with-unix-socket-path=/application/mysql5.1.72/tmp/mysql.sock \
--localstatedir=/application/mysql5.1.72/data \
--enable-assembler \
--enable-thread-safe-client \
--with-mysqld-user=mysql \
--with-big-tables \
--without-debug \
--with-pthread \
--enable-assembler \
--with-extra-charsets=complex \
--with-readline \
--with-ssl \
--with-embedded-server \
--enable-local-infile \
--with-plugins=partition,innobase \
--with-mysqld-ldflags=-all-static \
--with-client-ldflags=-all-static
#--with-plugin-PLUGIN \
出现如下图:就可以安装了make && make install
创建软链接
[root@localhost mysql-5.1.72]# ln -s /application/mysql5.1.72/ /application/mysql
[root@localhost mysql-5.1.72]# ll /application/mysql/
总用量 12
drwxr-xr-x 2 root root 4096 10月 24 10:20 bin
drwxr-xr-x 9 root root 99 10月 25 04:31 data
drwxr-xr-x 3 root root 18 10月 24 10:20 include
drwxr-xr-x 3 root root 18 10月 24 10:20 lib
drwxr-xr-x 2 root root 38 10月 24 10:21 libexec
drwxr-xr-x 3 root root 17 10月 24 06:53 man
drwxr-xr-x 10 root root 4096 10月 24 10:21 mysql-test
drwxr-xr-x 7 root root 66 10月 24 10:21 share
drwxr-xr-x 5 root root 4096 10月 24 10:21 sql-bench
16.2 MySQL数据库启动
查看配置文件。下面五个文件都是数据库的配置文件,
[root@localhost tools]# ls mysql-5.1.72/support-files/my*.cnf
mysql-5.1.72/support-files/my-huge.cnf
mysql-5.1.72/support-files/my-innodb-heavy-4G.cnf
mysql-5.1.72/support-files/my-large.cnf
mysql-5.1.72/support-files/my-medium.cnf
mysql-5.1.72/support-files/my-small.cnf
将文件复制到my.cnf(my-small.cnf为默认配置文件名 /etc/my.cnf 为默认配置路径 )
[root@localhost tools]# cd mysql-5.1.72/support-files/
[root@localhost support-files]# cp my-small.cnf /etc/my.cnf
cp:是否覆盖"/etc/my.cnf"? \y
[root@localhost support-files]#
创建存放数据库文件的地方
[root@localhost support-files]# mkdir /application/mysql/data -p
[root@localhost support-files]# chown -R mysql.mysql /application/mysql/
#/application/mysql/bin/mysql_install_db(数据库中的文件)
--basedir=/application/mysql (原始目录存放地址)
--datadir=/application/mysql/data/ (生成的数据文件地址)
--user=mysql (所用额数据用户初始化)
[root@localhost support-files]# /application/mysql/bin/mysql_install_db --basedir=/application/mysql --datadir=/application/mysql/data/ --user=mysql
出现两个ok且没有报错就说明成功
完成后生成两个库,MySQL的库是系统文件库,不能够轻易删除,而test为测试库,可以删除。
[root@localhost support-files]# ll /application/mysql/data/
总用量 4
drwx------ 2 mysql root 4096 10月 26 00:58 mysql
drwx------ 2 mysql root 6 10月 26 00:58 test
启动数据库
[root@localhost support-files]# cp mysql.server.sh /etc/init.d/mysqld (启动脚本)
[root@localhost support-files]# chkconfig --add mysqld (添加管理)
[root@localhost support-files]# chkconfig mysqld on (启动)
[root@localhost support-files]# chmod +x /etc/init.d/mysqld (授权启动)
启动数据库
[root@localhost support-files]# cd /application/mysql/bin
[root@localhost bin]# cd ..
[root@localhost mysql]# ./bin/mysqld_safe &
[1] 72588
[root@localhost mysql]# 191026 01:28:55 mysqld_safe Logging to '/application/mysql/data/localhost.localdomain.err'.
191026 01:28:55 mysqld_safe Starting mysqld daemon with databases from /application/mysql/data
检测是否成功,查看端口,成功
[root@localhost mysql]# netstat -lntup|grep mysql
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 72691/mysqld
设置密码
[root@localhost mysql]# /application/mysql/bin/mysqladmin -u root password 'zxc123'
#设置完密码之后,就登陆不进去了
[root@localhost mysql]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
#使用用户名和密码进行登陆
[root@localhost mysql]# mysql -uroot -pzxc123
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.1.72 Source distribution
Copyright (c) 2000, 2013, 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>
16.3 MySQL数据库应用
进入MySQL数据库
(全路径的进入)
[root@localhost ~]# /application/mysql/bin/mysql
mysql>
mysql> quit
Bye
(将路径加入PATH中)
[root@localhost ~]# vi /etc/profile
PATH="/application/mysql/bin/:$PATH"
[root@localhost ~]#grep mysql /etc/profile PATH="/application/mysql/bin/:$PATH"
[root@localhost ~]# . /etc/profile
[root@localhost ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.72 Source distribution
Copyright (c) 2000, 2013, 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> q
-> ;
16.4 Mysql排错问题解决:
.checking for termcap functions library... configure: error: No curses/termcap library found
解决问题:
yum list|grep ncurses
yum -y install ncurses-devel
yum install ncurses-devel
2.../depcomp: 第 571 行:exec: g++: 未找到
make[1]: *** [my_new.o] 错误 127
make[1]: 离开目录“/home/girl/tools/mysql-5.1.72/mysys”
make: *** [all-recursive] 错误 1
解决方法:
[root@localhost mysql-5.1.72]# yum install -y gcc-c++
已加载插件:fastestmirror
3.../include/my_global.h:1110:15: 错误:对 C++ 内建类型‘bool’的重声明 [-fpermissive]
typedef char bool; /* Ordinary boolean values 0 1 */
^
解决方法
make[1]: *** [my_new.o] 错误 1
make[1]: 离开目录“/home/girl/tools/mysql-5.1.72/mysys”
make: *** [all-recursive] 错误 1
解决办法,删除Mysql的文件,重新解压。[root@localhost tools]# rm -rf mysql-5.1.72
4.最后在启动mysql时出错,
[root@localhost support-files]# /application/mysql/bin/mysqld_safe &
[2] 72431
[1] 退出 1 /application/mysql/bin/mysqld_safe
[root@localhost support-files]# /application/mysql/bin/mysqld_safe:行337: my_print_defaults: 未找到命令
/application/mysql/bin/mysqld_safe:行343: my_print_defaults: 未找到命令
再查看端口,有的话就ok ,netstat -lntup|grep mysql
用系统提供的启动命令也没有用,最后查了一下原因, 是官方已经承认的bug,5.519以后的版本就不再出现了。
解决办法:进入
[root@localhost support-files]# cd /application/mysql/bin
[root@localhost bin]# cd ..
[root@localhost mysql]# ./bin/mysqld_safe &
[1] 72588
[root@localhost mysql]# 191026 01:28:55 mysqld_safe Logging to '/application/mysql/data/localhost.localdomain.err'.
191026 01:28:55 mysqld_safe Starting mysqld daemon with databases from /application/mysql/data
16.5 彻底删除mysql
1、使用以下命令查看当前安装mysql情况,查找以前是否装有mysql
1
rpm -qa|grep -i mysql
可以看到如下图的所示:
显示之前安装了:
MySQL-client-5.5.25a-1.rhel5
MySQL-server-5.5.25a-1.rhel5
2、停止mysql服务、删除之前安装的mysql
删除命令:rpm -e –nodeps 包名
1
2
rpm -ev MySQL-client-5.5.25a-1.rhel5
rpm -ev MySQL-server-5.5.25a-1.rhel5
如果提示依赖包错误,则使用以下命令尝试
1
rpm -ev MySQL-client-5.5.25a-1.rhel5 --nodeps
如果提示错误:error: %preun(xxxxxx) scriptlet failed, exit status 1
则用以下命令尝试:
1
rpm -e --noscripts MySQL-client-5.5.25a-1.rhel5
3、查找之前老版本mysql的目录、并且删除老版本mysql的文件和库
1
find / -name mysql
查找结果如下:
1
2
3
4
5
find / -name mysql
/var/lib/mysql
/var/lib/mysql/mysql
/usr/lib64/mysql
删除对应的mysql目录
1
2
3
rm -rf /var/lib/mysql
rm -rf /var/lib/mysql
rm -rf /usr/lib64/mysql
具体的步骤如图:查找目录并删除
注意:卸载后/etc/my.cnf不会删除,需要进行手工删除
1
rm -rf /etc/my.cnf
4、再次查找机器是否安装mysql
1
rpm -qa|grep -i mysql