本文参考了:
http://freeloda.blog.51cto.com/2033581/1286592
本文介绍使用 MySQL 的 rpm 包进行安装配置。
注:系统环境为 CentOS 6.8
目录:
-
1.安装前准备
- 安装 ntp
- 安装 epel-release
- 时间同步
- 关闭防火墙
- 关闭 SELinux
2.下载 rpm 包
3.安装 MySQL-server,MySQL-compat
4.安装 MySQL-client
5.MySQL 客户端命令
6.修改密码
7.查看 mysql 用户表
8.修改配置文件
1.安装前准备
安装 ntp:
[root@lamp1 ~]# yum install -y ntp
安装 epel 源:
[root@lamp1 ~]# yum install -y epel-release
或者:
rpm -ivh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
时间同步:
ntpdate 202.120.2.101
关闭防火墙:
service iptables stop
chkconfig iptables off
关闭 SELinux:
[root@lamp1 ~]# setenforce 0
[root@lamp1 ~]# vi /etc/selinux/config
SELINUX=disabled
[root@lamp1 ~]# getenforce
Disabled
2.下载安装 MySQL-server,MySQL-shared-compat
除了 MySQL 官方网站,也可从国内的一些镜像网站下载 MySQL 的 rpm 安装包,这是 sohu 的镜像地址:
http://mirrors.sohu.com/mysql/MySQL-5.6/
下载安装 MySQL-bundle:
wget http://mirrors.sohu.com/mysql/MySQL-5.6/MySQL-5.6.30-1.el6.x86_64.rpm-bundle.tar
解压:
[root@lamp1 ~]# tar -xvf MySQL-5.6.30-1.el6.x86_64.rpm-bundle.tar
MySQL-devel-5.6.30-1.el6.x86_64.rpm
MySQL-shared-5.6.30-1.el6.x86_64.rpm
MySQL-test-5.6.30-1.el6.x86_64.rpm
MySQL-embedded-5.6.30-1.el6.x86_64.rpm
MySQL-client-5.6.30-1.el6.x86_64.rpm
MySQL-shared-compat-5.6.30-1.el6.x86_64.rpm
MySQL-server-5.6.30-1.el6.x86_64.rpm
其中包括 MySQL 客户端,服务端,开发包等。
现在尝试安装 MySQL-server,会遇到冲突:
file /usr/share/mysql/czech/errmsg.sys from install of MySQL-server-5.6.30-1.el6.x86_64
conflicts with file from package mysql-libs-5.1.73-7.el6.x86_64
原来是与系统上安装的 mysql-libs 包冲突了,看看其中有些什么:
[root@lamp1 ~]# rpm -ql mysql-libs
/etc/ld.so.conf.d/mysql-x86_64.conf
/etc/my.cnf
/usr/lib64/mysql
/usr/lib64/mysql/libmysqlclient.so.16
/usr/lib64/mysql/libmysqlclient.so.16.0.0
/usr/lib64/mysql/libmysqlclient_r.so.16
/usr/lib64/mysql/libmysqlclient_r.so.16.0.0
主要是几个依赖库,它被系统上的 postfix 包所依赖,所以不能删除,通过下载 MySQL-compat 可以解决冲突:
wget http://mirrors.sohu.com/mysql/MySQL-5.6/MySQL-shared-compat-5.6.30-1.el6.x86_64.rpm
其中包含有 mysql-libs 所提供的依赖包
[root@lamp1 ~]# rpm -qpl MySQL-shared-compat-5.6.30-1.el6.x86_64.rpm
warning: MySQL-shared-compat-5.6.30-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
/usr/lib64/libmysqlclient.so.12
/usr/lib64/libmysqlclient.so.12.0.0
/usr/lib64/libmysqlclient.so.14
/usr/lib64/libmysqlclient.so.14.0.0
/usr/lib64/libmysqlclient.so.15
/usr/lib64/libmysqlclient.so.15.0.0
/usr/lib64/libmysqlclient.so.16
/usr/lib64/libmysqlclient.so.16.0.0
/usr/lib64/libmysqlclient_r.so.12
/usr/lib64/libmysqlclient_r.so.12.0.0
/usr/lib64/libmysqlclient_r.so.14
/usr/lib64/libmysqlclient_r.so.14.0.0
/usr/lib64/libmysqlclient_r.so.15
/usr/lib64/libmysqlclient_r.so.15.0.0
/usr/lib64/libmysqlclient_r.so.16
/usr/lib64/libmysqlclient_r.so.16.0.0
现在可删除 mysql-libs:
rpm -e mysql-libs
再尝试安装 MySQL-server:
[root@lamp1 ~]# rpm -ihv MySQL-server-5.6.30-1.el6.x86_64.rpm
安装成功了。
以默认配置启动 mysql:
service mysql start
查看 mysql 数据文件,默认在 /var/lib/mysql/ 路径下:
[root@lamp1 ~]# ll -h /var/lib/mysql/
total 109M
-rw-rw---- 1 mysql mysql 56 Jun 24 10:54 auto.cnf
-rw-rw---- 1 mysql mysql 12M Jun 24 10:54 ibdata1
-rw-rw---- 1 mysql mysql 48M Jun 24 10:54 ib_logfile0
-rw-rw---- 1 mysql mysql 48M Jun 24 10:47 ib_logfile1
-rw-r----- 1 mysql root 2.1K Jun 24 10:54 lamp1.err
-rw-rw---- 1 mysql mysql 5 Jun 24 10:54 lamp1.pid
drwx--x--x 2 mysql mysql 4.0K Jun 24 10:47 mysql
srwxrwxrwx 1 mysql mysql 0 Jun 24 10:54 mysql.sock
drwx------ 2 mysql mysql 4.0K Jun 24 10:47 performance_schema
-rw-r--r-- 1 root root 111 Jun 24 10:47 RPM_UPGRADE_HISTORY
-rw-r--r-- 1 mysql mysql 111 Jun 24 10:47 RPM_UPGRADE_MARKER-LAST
drwxr-xr-x 2 mysql mysql 4.0K Jun 24 10:47 test
3.安装 MySQL-client
安装 mysql 客户端:
[root@lamp1 ~]# rpm -ihv MySQL-client-5.6.30-1.el6.x86_64.rpm
4.登陆并修改密码
对于 MySQL 5.6 版,初始 root 密码在 如下文件中:
[root@lamp1 ~]# cat /root/.mysql_secret
# The random password set for the root user at Fri Jun 24 10:47:26 2016 (local time): jSDBE4M3r7e40CMC
第一次使用 root 登陆,必须使用 SET PASSWORD 修改密码(想想这是如何做到的,提示:password expired),否则无法使用任何其他指令:
[root@lamp1 ~]# mysql -u root -pjSDBE4M3r7e40CMC
mysql>
修改密码的命令:
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('new_password');
mysql也建议你使用下面这命令修改密码,它是交互式的命令,根据提示进行一步一步地操作,很简单:
/usr/bin/mysql_secure_installation
也可以使用 shell 命令行工具 mysqladmin 进行修改:
/usr/bin/mysqladmin -u root password 'new-password'
刚才我们已经登陆了,现在使用第一个命令修改 root 密码:
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('guli123');
Query OK, 0 rows affected (0.01 sec)
查看 mysql 账户列表:
mysql> SELECT User, Host, Password From mysql.user;
+------+-----------+-------------------------------------------+
| User | Host | Password |
+------+-----------+-------------------------------------------+
| root | localhost | *F0BC5A026769654BD0A7EFA74872CA595563919A |
| root | lamp1 | *2C98D570B669DDAADBBB051EEEDBEACA5A50BF69 |
| root | 127.0.0.1 | *2C98D570B669DDAADBBB051EEEDBEACA5A50BF69 |
| root | ::1 | *2C98D570B669DDAADBBB051EEEDBEACA5A50BF69 |
+------+-----------+-------------------------------------------+
4 rows in set (0.01 sec)
5.修改配置文件
MySQL-5.6 生成的配置文件在 /usr/my.cnf,内容非常简单,需要重新创建。
可通过 mysqld --verbose --help 命令查看 mysqld 读取的配置文件:
[root@lamp1 ~]# mysqld --help --verbose | less
Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf /usr/etc/my.cnf ~/.my.cnf
我们可将 /usr/my.cnf 移动到 /etc/my.cnf,添加如下内容:
[root@lamp1 ~]# mv /usr/my.cnf /etc/my.cnf
[root@lamp1 ~]# vi /etc/my.cnf
binlog-format=ROW
log-bin=master-bin.log
log-slave-updates=true
gtid-mode=on
enforce-gtid-consistency=true
master-info-repository=TABLE
relay-log-info-repository=TABLE
sync-master-info=1
slave-parallel-workers=2
binlog-checksum=CRC32
master-verify-checksum=1
slave-sql-verify-checksum=1
binlog-rows-query-log_events=1
server-id=1
report-port=3306
port=3306
#datadir=/mydata/data
socket=/tmp/mysql.sock
report-host=master.test.com
下面需要重新初始化 mysql 的数据文件。
关闭 mysql,删除全部数据文件,并重新初始化:
[root@lamp1 ~]# service mysql stop
[root@lamp1 ~]# rm -rf /var/lib/mysql/*
[root@lamp1 ~]# mysql_install_db --user=mysql --datadir=/var/lib/mysql
[root@lamp1 ~]# ll /var/lib/mysql/
total 110604
-rw-rw---- 1 mysql mysql 12582912 Jun 24 11:12 ibdata1
-rw-rw---- 1 mysql mysql 50331648 Jun 24 11:12 ib_logfile0
-rw-rw---- 1 mysql mysql 50331648 Jun 24 11:12 ib_logfile1
drwx------ 2 mysql mysql 4096 Jun 24 11:12 mysql
drwx------ 2 mysql mysql 4096 Jun 24 11:12 performance_schema
drwx------ 2 mysql mysql 4096 Jun 24 11:12 test
这又生成 /usr/my.cnf,删除它,然后启动 mysql:
rm -f /usr/my.cnf
service mysql start
这次可直接登陆,没有密码,因为我们做了重新初始化:
[root@lamp1 ~]# mysql
mysql> SELECT User,Host,Password FROM mysql.user;
+------+-----------+----------+
| User | Host | Password |
+------+-----------+----------+
| root | localhost | |
| root | lamp1 | |
| root | 127.0.0.1 | |
| root | ::1 | |
| | localhost | |
| | lamp1 | |
+------+-----------+----------+
现在可按照前面所讲的修改 root 密码。
我们可创建一个可远程连接的普通用户,赋予其对所有库有查询权限,允许从本地域网连接:
mysql> GRANT SELECT ON *.* TO 'tuser'@'192.168.0.%' IDENTIFIED BY 'guli123';
Query OK, 0 rows affected (0.02 sec)
mysql> SELECT User,Host,Password FROM mysql.user;
+-------+----------------+-------------------------------------------+
| User | Host | Password |
+-------+----------------+-------------------------------------------+
| root | localhost | |
| root | lamp1 | |
| root | 127.0.0.1 | |
| root | ::1 | |
| | localhost | |
| | lamp1 | |
| tuser | 192.168.0.% | *F0BC5A026769654BD0A7EFA74872CA595563919A |
+-------+----------------+-------------------------------------------+
7 rows in set (0.00 sec)
远程测试:
[root@vm_mac ~]# mysql -u tuser -h 192.168.0.171 -p
Enter password: # 输入密码
...
mysql>
测试成功。