mysql在linux上的安装

前提:

环境:workstation 11 + CentOS 7 + mysql-5.6.40

安装前先查看服务器里是否有老版本的mysql已经被安装了

rpm -qa|grep mysql

如果有就删除掉旧版本的mysql即可

rpm -e (上面那条命令得到的信息,即mysql软件包名) --nodeps

正式安装:

1.上传安装包到/usr/local/目录下,解压缩,重命名,配置环境变量

[root@hadoop ~]# cd /usr/local/
[root@hadoop local]# tar xzvf mysql-5.6.40-linux-glibc2.12-x86_64.tar.gz 
[root@hadoop local]# mv mysql-5.6.40-linux-glibc2.12-x86_64 mysql
[root@hadoop ~]# vi /etc/profile
添加变量:export MYSQL_HOME=/usr/local/mysql
在PATH后面添加 :$MYSQL_HOME/bin
[root@hadoop ~]# source /etc/profile

2.创建目录,添加用户组和用户

[root@hadoop local]# mkdir -p /data/mysql_data
[root@hadoop local]# mkdir -p /data/mysql_log
[root@hadoop local]# mkdir -p /data/log-bin
[root@hadoop local]# groupadd mysql
[root@hadoop local]# useradd -g mysql mysql
[root@hadoop local]# chown -R mysql.mysql /data/mysql_data /data/mysql_log/ /data/log-bin/
[root@hadoop local]# chown -R mysql.mysql /usr/local/mysql

3.配置my.cnf文件。根据自己需求填写,以下只是一个大概的例子

[root@hadoop local]# vi /etc/my.cnf
[mysqld]
# GENERAL #
user = mysql
#default-storage-engine = INNODB
default-storage-engine = MyISAM

socket=/data/mysql_data/mysql.sock
pid-file=/data/mysql_data/mysql.pid
port = 3306

# MyISAM #
key_buffer_size = 1000M
myisam-recover-options = FORCE,BACKUP

# SAFETY #
max_allowed_packet = 16M
max_connect_errors = 1000000
skip_name_resolve

# DATA STORAGE #
datadir = /data/mysql_data/
long_query_time = 1

# BINARY LOGGING #
binlog_format=ROW
log-bin = /data/log-bin/mysql-bin-3306
expire_logs_days =14
sync_binlog = 1
server-id = 1
max_binlog_size = 500M

# REPLICATION #
relay-log = /data/log-bin/relay-bin-3306
slave-net-timeout = 60

# CACHES AND LIMITS #
tmp_table_size = 32M
max_heap_table_size =32M
max_connections =500
thread_cache_size = 50
open_files_limit=65535
table_definition_cache = 4096
table_open_cache = 4096

# INNODB #
innodb_data_file_path = ibdatal:128M;ibdata2:10M:autoextend
innodb_flush_method = O_DIRECT
innodb_log_files_in_group = 2
innodb_lock_wait_timeout = 50
innodb_log_file_size = 256M
innodb_flush_log_at_trx_commit = 1
innodb_file_per_table = 1
innodb_thread_concurrency = 8
innodb_buffer_pool_size = 8G

# LOGGING #
log-error = /data/mysql_log/mysql-error-3306.log
log-queries-not-using-indexes = 1
slow-query-log = 1
long_query_time = 1
slow_query_log_file = /data/mysql_log/mysql-slow-3306.log

# FOR SLAVE #
#log-slave-updates = true
#gtib-mode = on 
#enforce-gtib-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
#report-port = 3306
#report-host = 192.168.42.133
my.cnf

4.配置系统服务

[root@hadoop local]# cp -af /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@hadoop local]# vi /etc/init.d/mysqld 
#修改两个变量,分别在46行和47行,set nu显示编辑文件行数
basedir=/usr/local/mysql
datadir=/data/mysql_data
[root@hadoop local]# chmod 755 /etc/init.d/mysqld 
[root@hadoop local]# chkconfig --add mysqld
[root@hadoop local]# chkconfig --level 345 mysqld on

5.初始化数据库

[root@hadoop mysql]# ./scripts/mysql_install_db --user=mysql --defaults-file=/etc/my.cnf

6.启动数据库

[root@hadoop ~]# service mysqld start

7.执行mysql命令报错并解决

[root@hadoop ~]# mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

解决方法:执行软链接

[root@hadoop ~]# ln -s /data/mysql_data/mysql.sock /tmp/mysql.sock
[root@hadoop ~]# mysql     #再次查看,成功登录mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.40-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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> exit
Bye
[root@hadoop ~]#
再次执行mysql查看,成功登录mysql

8.修改root用户密码,增加mysql的安全性

mysqladmin -u root password 'root' #设置密码后再执行mysql就无法登录mysql了
mysql -uroot -proot #登录mysql需要输入密码
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION; #允许root用户远程访问
mysql> FLUSH PRIVILEGES; #刷新权限
mysql> exit
[root@hadoop ~]# mysqladmin -u root password 'root' #设置密码
Warning: Using a password on the command line interface can be insecure.
[root@hadoop ~]# mysql #无法登录
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@hadoop ~]# mysql -uroot -p  #这样登录
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.40-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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> exit
Bye
[root@hadoop ~]# mysql -uroot -proot #或者这样登录
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 5
Server version: 5.6.40-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION; #远程访问
Query OK, 0 rows affected (0.03 sec)

mysql> FLUSH PRIVILEGES; #刷新权限
Query OK, 0 rows affected (0.00 sec)
View Code

9.删除匿名账户。因为mysql数据库安装完之后自带一个匿名账户,这非常容易增加数据库受到安全攻击的风险。

mysql> update mysql.user set password=PASSWORD('your password') where user='';
mysql> FLUSH PRIVILEGES;
mysql> update mysql.user set password=PASSWORD('your password') where user='';
Query OK, 2 rows affected (0.05 sec)
Rows matched: 2 Changed: 2 Warnings: 0

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
View Code

至此,我们的mysql就算安装完成了。

10.测试mysql是否可以远程登录。
由于HIVE有可能是调用远程的mysql进行元数据处理,我们需要测试一下是否可以远程登录mysql,为以后做准备。

如果你Windows本地装过mysql的话,直接cmd进入命令行,输入mysql -h 192.168.42.133 -uroot -proot,查看是否能登录。(我可以登录)

C:\Users\Administrator>mysql -h 192.168.42.133 -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 8
Server version: 5.6.40-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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>
View Code

如果你Windows本地没有安装过mysql的话,可以安装workbench,这个软件可以远程登录mysql进行管理和开发。

点击 Test Connection,显示连接成功,如下图

 然后点击ok-->ok,可建立远程连接。

 

posted @ 2018-07-27 10:40  zhengna  阅读(342)  评论(0编辑  收藏  举报