0 导入mysql 案例文件:
将文件 world.sql 上传到linux上
mysql>create database world; use world;
mysql> source /opt/world.sql
world.sql 见附件
1、解压RPM包
[root@hadoop3 ~]# mkdir /usr/local/mysql
[root@hadoop3 soft]# tar vxf MySQL-5.6.24-1.el6.x86_64.rpm-bundle.tar -C /usr/local/mysql
2、安装RPM包
[root@hadoop3 ~]# cd /usr/local/mysql
[root@hadoop3 mysql]# ls
MySQL-client-5.6.24-1.el6.x86_64.rpm MySQL-shared-5.6.24-1.el6.x86_64.rpm
MySQL-devel-5.6.24-1.el6.x86_64.rpm MySQL-shared-compat-5.6.24-1.el6.x86_64.rpm
MySQL-embedded-5.6.24-1.el6.x86_64.rpm MySQL-test-5.6.24-1.el6.x86_64.rpm
MySQL-server-5.6.24-1.el6.x86_64.rpm
[root@hadoop3 mysql]# rpm -ivh MySQL-client-5.6.24-1.el6.x86_64.rpm
[root@hadoop3 mysql]# rpm -ivh MySQL-server-5.6.24-1.el6.x86_64.rpm
-------------------------------------------------------------------------------------------------------------------------------------------------
补充:删除旧版本mysql
(1)[root@hadoop3 mysql]# rpm -qa|grep -i mysql
qt-mysql-4.6.2-25.el6.x86_64
mysql-server-5.1.66-2.el6_3.x86_64
mysql-5.1.66-2.el6_3.x86_64
mysql-libs-5.1.66-2.el6_3.x86_64
perl-DBD-MySQL-4.013-3.el6.x86_64
(2)删除相关组建
[root@hadoop3 mysql]# rpm -e --nodeps qt-mysql-4.6.2-25.el6.x86_64
[root@hadoop3 mysql]# rpm -e --nodeps mysql-server-5.1.66-2.el6_3.x86_64
[root@hadoop3 mysql]# rpm -e --nodeps mysql-5.1.66-2.el6_3.x86_64
[root@hadoop3 mysql]# rpm -e --nodeps mysql-libs-5.1.66-2.el6_3.x86_64
[root@hadoop3 mysql]# rpm -e --nodeps perl-DBD-MySQL-4.013-3.el6.x86_64
(3)删除目录及文件
-----------------------------------------------------------------------------------------
3、查看root用户的随机密码 此时需要切换到root用户家目录
(1)[root@hadoop3 ~]# ls -la
-rw------- 1 root root 71 May 26 00:14 .my.cnf.2058
-rw------- 1 root root 1 May 26 00:14 .mysql.2058
-rw------- 1 root root 104 May 26 00:19 .mysql_secret
(2)[root@hadoop3 ~]# vi .mysql_secret
# The random password set for the root user at Tue May 26 00:19:14 2015 (local time): RoBd25NxiAWoBzGB
4、启动服务
[root@hadoop3 ~]# service mysql start
或
[root@hadoop3 ~]# /etc/init.d/mysql start
5、myql 安装 --这里会用到root的随机密码
[root@hadoop3 ~]# mysql_secure_installation
都yes就可以,在安装过程中,会先设置密码,然后再一路yes下去
6、修改my.cnf
(1)[root@hadoop3 usr]# find / -name my-default.cnf
/usr/share/mysql/my-default.cnf
/usr/share/doc/MySQL-server-5.6.24/my-default.cnf
(2)[root@hadoop3 usr]# cp /usr/share/doc/MySQL-server-5.6.24/my-default.cnf /etc/my.cnf
(3)[root@hadoop3 ~]# vi /etc/my.cnf
[mysql]
default-character-set = utf8 ----> 默认是latin
auto-rehash
[mysqld]
basedir = /usr
datadir = /var/lib/mysql
port = 3306
log_bin=/var/lib/mysql/1.000001
binlog_format=ROW
expire_logs_days = 5
slow_query_log = 1
slow_query_log_file = /var/lib/mysql/mysqlslow.log
long_query_time = 2
innodb_log_buffer_size = 5M
innodb_file_per_table=0
innodb_data_file_path=ibdata1:10M:autoextend
innodb_buffer_pool_size=500M
character_set_server = utf8 ----> 默认是latin
socket=/var/lib/mysql/mysql.sock
[mysqld_safe]
log-error=/var/lib/mysql/mysqlerror.log
[mysqldump]
quick
max_allowed_packet = 16M
(4)软连接
[root@hadoop3 ~]# ln -s /tmp/mysqld.sock /var/lib/mysql/mysql.sock
7、重启连接mysql
[root@hadoop3 ~]# service mysql restart
[root@hadoop3 ~]# mysql -uroot -p123456 (这里设置的mysql密码为123456)
8、说明
建议使用root用户,这里使用mysql为MySQL-5.6.24-1.el6.x86_64.rpm-bundle.tar
机器别名为hadoop3。
启动后, 通过命令 service mysql status来看是否运行
在安装好后,如果通过sqlyog等客户端来链接时,会报链接遭拒,可以做如下修改:
mysql命令行下:
例如,你想root使用123456从任何主机连接到mysql服务器的话。
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
FLUSH PRIVILEGES;
如果你想允许用户myuser从ip为192.168.1.6的主机连接到mysql服务器,并使用mypassword作为密码
GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'192.168.1.3' IDENTIFIED BY 'mypassword' WITH GRANT OPTION;
FLUSH PRIVILEGES;
如果你想允许用户myuser从ip为192.168.1.6的主机连接到mysql服务器的dk数据库,并使用mypassword作为密码
GRANT ALL PRIVILEGES ON dk.* TO 'myuser'@'192.168.1.3' IDENTIFIED BY 'mypassword' WITH GRANT OPTION;
FLUSH PRIVILEGES;
上面在 /etc/my.cnf 中修改了mysql默认字符集, 这样使用MySQL时有汉字也不会乱码了
查看mysql 使用的字符集:
mysql> show variables like 'character%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.34 sec)