Linux下MySQL安装与配置

来源:互联网 发布:mysql的全文索引 编辑:程序博客网 时间:2024/04/29 21:37

1、下载安装包
http://dev.mysql.com/downloads/mysql/#downloads
推荐下载通用安装方法的TAR包
http://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.12-linux-glibc2.5-x86_64.tar

2、检查库文件是否存在,如有删除。

[root@localhost Desktop]$ rpm -qa | grep mysqlmysql-libs-5.1.52-1.el6_0.1.x86_64[root@localhost ~]# rpm -e mysql-libs-5.1.52.x86_64 --nodeps[root@localhost ~]#

3、检查mysql组和用户是否存在,如无创建

[root@localhost ~]# cat /etc/group | grep mysqlmysql:x:490:[root@localhost ~]# cat /etc/passwd | grep mysqlmysql:x:496:490::/home/mysql:/bin/bash

默认存在的情况,如无,执行添加命令:

[root@localhost ~]#groupadd mysql[root@localhost ~]#useradd -r -g mysql mysql

useradd -r参数表示mysql用户是系统用户,不可用于登录系统。

4、解压TAR包,更改所属的组和用户

[root@localhost ~]# cd /usr/local/[root@localhost local]# tar xvf mysql-5.7.12-linux-glibc2.5-x86_64.tar[root@localhost local]# ls -ltotal 1306432-rwxr--r--. 1 root root  668866560 Jun  1 15:07 mysql-5.7.12-linux-glibc2.5-x86_64.tar-rw-r--r--. 1 7161 wheel 638960236 Mar 28 12:54 mysql-5.7.12-linux-glibc2.5-x86_64.tar.gz-rw-r--r--. 1 7161 wheel  29903372 Mar 28 12:48 mysql-test-5.7.12-linux-glibc2.5-x86_64.tar.gz[root@localhost local]# tar xvfz mysql-5.7.12-linux-glibc2.5-x86_64.tar.gz[root@localhost local]# mv mysql-5.7.12-linux-glibc2.5-x86_64 mysql[root@localhost local]# ls -ltotal 1306436drwxr-xr-x. 2 root root       4096 Dec  4  2009 bindrwxr-xr-x. 2 root root       4096 Dec  4  2009 etcdrwxr-xr-x. 2 root root       4096 Dec  4  2009 gamesdrwxr-xr-x. 2 root root       4096 Dec  4  2009 includedrwxr-xr-x. 2 root root       4096 Dec  4  2009 libdrwxr-xr-x. 3 root root       4096 Dec  2 14:36 lib64drwxr-xr-x. 2 root root       4096 Dec  4  2009 libexecdrwxr-xr-x. 9 7161 wheel      4096 Mar 28 12:51 mysql-rw-r--r--. 1 7161 wheel 638960236 Mar 28 12:54 mysql-5.7.12-linux-glibc2.5-x86_64.tar.gzdrwxr-xr-x. 2 root root       4096 Dec  4  2009 sbindrwxr-xr-x. 6 root root       4096 Dec  2 14:36 sharedrwxr-xr-x. 2 root root       4096 Dec  4  2009 src[root@localhost local]# chown -R mysql:root mysql/[root@localhost local]# cd mysql/

5、安装和初始化数据库

[root@localhost mysql]# bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/2016-06-01 15:23:25 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize2016-06-01 15:23:30 [WARNING] The bootstrap log isn't empty:2016-06-01 15:23:30 [WARNING] 2016-06-01T22:23:25.491840Z 0 [Warning] --bootstrap is deprecated. Please consider using --initialize instead2016-06-01T22:23:25.492256Z 0 [Warning] Changed limits: max_open_files: 1024 (requested 5000)2016-06-01T22:23:25.492260Z 0 [Warning] Changed limits: table_open_cache: 431 (requested 2000)----------------------

这一步我安装的时候,第一次不行,所有的重新来了一次,然后出现的是如下提示:

[root@localhost mysql]# ./bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/Installing MySQL system tables...OKFilling help tables...OKTo start mysqld at boot time you have to copysupport-files/mysql.server to the right place for your systemPLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !To do so, start the server, then issue the following commands:  /usr/local/mysql//bin/mysqladmin -u root password 'new-password'  /usr/local/mysql//bin/mysqladmin -u root -h 127.0.1.1 password 'new-password'Alternatively you can run:  /usr/local/mysql//bin/mysql_secure_installationwhich will also give you the option of removing the testdatabases and anonymous user created by default.  This isstrongly recommended for production servers.See the manual for more instructions.You can start the MySQL daemon with:  cd /usr ; /usr/local/mysql//bin/mysqld_safe &You can test the MySQL daemon with mysql-test-run.pl  cd mysql-test ; perl mysql-test-run.plPlease report any problems at http://bugs.mysql.com/The latest information about MySQL is available on the web at  http://www.mysql.comSupport MySQL by buying support/licenses at http://shop.mysql.comNew default config file was created as /usr/local/mysql//my.cnf andwill be used by default by the server when you start it.You may edit this file to change server settingsWARNING: Default config file /etc/my.cnf exists on the systemThis file will be read by default by the MySQL serverIf you do not want to use this, either remove it, or use the--defaults-file argument to mysqld_safe when starting the server[root@localhost mysql]#  

6、创建mysqld起动和配置文件

[root@localhost mysql]#[root@localhost mysql]# cp -a ./support-files/my-default.cnf /etc/my.cnf[root@localhost mysql]# cp -a ./support-files/mysql.server  /etc/init.d/mysqld[root@localhost mysql]# cd bin/[root@localhost bin]# ./mysqld_safe --user=mysql &[1] 2932[root@localhost bin]# 2016-06-01T22:27:09.708557Z mysqld_safe Logging to '/usr/local/mysql/data/localhost.localdomain.err'.2016-06-01T22:27:09.854913Z mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data[root@localhost bin]# /etc/init.d/mysqld restartShutting down MySQL..2016-06-01T22:27:50.498694Z mysqld_safe mysqld from pid file /usr/local/mysql/data/localhost.localdomain.pid ended SUCCESS! Starting MySQL. SUCCESS! [1]+  Done                    ./mysqld_safe --user=mysql[root@localhost bin]# //设置开机启动[root@localhost bin]# chkconfig --level 35 mysqld on[root@localhost bin]#

另一种很好的设置MySQL自启动的方式:

[root@localhost bin]# echo "service mysqld start" >> /etc/rc.local或者进入/etc/目录,直接vim rc.local编辑rc.local文件,在最后一行添加“service mysqld start”,保存退出有时会遇到权限问题:bash: /etc/rc.local: Permission denied分析:bash 返回 /etc/rc.local: Permission denied    这是因为重定向符号 “>” 也是 bash 的命令。sudo 只是让 echo 命令具有了 root 权限,    但是没有让 “>” 命令也具有root 权限,所以 bash 会认为这个命令没有写入信息的权限。解决:使用 bash -c 参数[root@localhost bin]# sudo bash -c "echo "service mysqld start" >> /etc/rc.local"

6.初始化密码
mysql5.7会生成一个初始化密码,而在之前的版本首次登陆不需要登录。

[root@localhost bin]# cat /root/.mysql_secret # Password set for user 'root@localhost' at 2016-06-01 15:23:25 ,xxxxxR5H9[root@localhost bin]# ./mysql -uroot -pEnter password: Welcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 2Server version: 5.7.12Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> SET PASSWORD = PASSWORD('123456');Query OK, 0 rows affected, 1 warning (0.00 sec)mysql> flush privileges;Query OK, 0 rows affected (0.00 sec)

上面一步中,如果出现了提示密码过期,可用如下方法解决:

[root@localhost bin]# /usr/local/mysql/bin/mysqladmin -u root -p passwordEnter password:New password:Confirm new password:Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.或者:/usr/local/mysql/bin/mysqladmin -u root -p'<your temp password>' password '<your new password>'

7.添加远程访问权限

mysql> use mysql; Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql> update user set host = '%' where user = 'root';Query OK, 1 row affected (0.00 sec)Rows matched: 1  Changed: 1  Warnings: 0mysql> select host, user from user;+-----------+-----------+| host      | user      |+-----------+-----------+| %         | root      || localhost | mysql.sys |+-----------+-----------+

9、更改配置文件和服务的权限
此步一定要改,我当初没改就一直提示说是有一个服务绑定了3306,死都找不出来问题。
先添加环境变量:

[root@localhost bin]# vim /etc/profile最后一行填加:MYSQL_HOME=/usr/local/mysqlexport PATH=$PATH:$MYSQL_HOME/bin 让修改立即生效:[root@localhost bin]# source /etc/profile 

再修改两个文件的权限:

[root@localhost bin]# [root@localhost bin]# service mysqld stop [root@localhost bin]# chown -R root:root /etc/init.d/mysqld[root@localhost bin]# chown -R root:root /etc/my.cnf

10、修改配置文件

[root@localhost bin]# vim /etc/my.cnf我的配置文件如下:# For advice on how to change settings please see# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the# *** default location during install, and will be replaced if you# *** upgrade to a newer version of MySQL.[client]default-character-set=utf8#避免MySQL的外部锁定,减少出错几率增强稳定性。socket = /tmp/mysql.sock[mysql]local-infile=1loose-local-infile=1[mysqld]# Remove leading # and set to the amount of RAM for the most important data# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.# innodb_buffer_pool_size = 128M# Remove leading # to turn on a very important data integrity option: logging# changes to the binary log between backups.# log_bin# These are commonly set, remove the # and set as required.basedir = /usr/local/mysqldatadir = /usr/local/mysql/dataport = 3306server_id = 1character_set_server=utf8#skip-grant-tableslower_case_table_names=1#避免MySQL的外部锁定,减少出错几率增强稳定性。socket = /tmp/mysql.sock skip-external-lockingskip-name-resolvelog_bin=/usr/local/mysql/log/bin.loglog_error=/usr/local/mysql/log/error.loglong_query_time=3slow_query_log=ONslow_query_log_file="/usr/local/mysql/log/slowquery.log"general_log=ONgeneral_log_file=/usr/local/mysql/log/general.logexpire_logs_days = 10# socket = .....# Remove leading # to set options mainly useful for reporting servers.# The server defaults are faster for transactions and fast SELECTs.# Adjust sizes as needed, experiment to find the optimal values.# join_buffer_size = 128M# sort_buffer_size = 2M# read_rnd_buffer_size = 2M sql_mode=STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ENGINE_SUBSTITUTION,NO_ZERO_IN_DATE

修改后保存退出

11、重启生效

[root@localhost bin]# /etc/init.d/mysqld restart[root@localhost bin]# netstat -na | grep 3306,如果看到有监听说明服务启动了
1 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 儿童五周岁九个月比同龄矮怎么办 8个月宝宝长牙母乳喂养咬人怎么办 6周的孩子视力低常怎么办 宝宝吃了甜食生痰咳嗽怎么办 两个月的边牧抵抗力差怎么办 阴茎勃起后向上翘的厉害怎么办 5个月宝宝发烧38.5度怎么办 9个月宝宝发烧38.5度怎么办 八个月宝宝只吃母乳不吃奶粉怎么办 八个月母乳不够宝宝不吃奶粉怎么办 八个月宝宝吃母乳不吃奶粉怎么办 八个月宝宝戒奶不吃奶粉怎么办 刚满月的宝宝发烧38度怎么办 未满月的宝宝发烧38度怎么办 半月大的婴儿吃奶就漾奶怎么办 上司交给你不能完成的任务怎么办 电脑光驱里放入光碟放不出来怎么办 黑暗之魂3太难了怎么办 苹果手机下载的游戏闪退怎么办 宝宝两岁了不怎么爱拉大便怎么办? 小狗脖子发硬疼的直叫怎么办 厨房里有很多小虫子围着鸡蛋怎么办 狗生小狗后几天不吃饭怎么办 还没满月的小兔子突然死了怎么办 宝宝小鸡被蚊子咬后肿得很大怎么办 不知道是哪知兔子下的小兔怎么办 兔子生完小兔不吃东西了怎么办 人工喂养七天的小羊拉希怎么办 仔兔出生3天吃过奶就尿怎么办 小兔子买回来两天不拉屎怎么办 大狗生了小狗把小狗咬死了怎么办 狗妈妈一直咬小狗的脐带怎么办 狗狗体内驱虫驱不干净怎么办 打老鼠脚被老鼠咬了怎么办 天正画的cad打开显示空白怎么办 苹果手机信息被拉进群聊怎么办 空调的控制线的报验资料怎么办 窗窗户罩子护栏上的瓦楞板怎么办 酸洗好的带钢容易返锈怎么办 化肥撤到小树苗上现在变黑了怎么办 尿素液烧的太慢了怎么办