一、前言
在linux上安装mysql不外乎就两种:自动安装和手动安装。由于手动安装必须要求linux系统里面包含c和c++的编译器,但是可以指定安装位置,在整个过程中不需要联网:make && make install(只要linux系统能够编译之后),需要的环境会在后面介绍,注意:下载安装包,在找的时候一定要注意自己的linux版本,版本和厂商不一样,安装包也就不一样,本博客是以centos7的操作系统。自动安装全过程都需要联网。本博客采用从yum仓库里面下载安装。
硬件要求:
必须有联网的linux系统:安装教程请参照博客:
http://blog.csdn.net/qq_38776653/article/details/73550714
二、将mysql连接入yum仓库
查看yum仓库里面是否有mysql安装包
从图中可以看出yum仓库原来就没有mysql的安装包。
1、首先将mysql的仓库关联或者加入到yum仓库里面
参考mysql官网(mysql社区版的,mysql企业版支持分布式):
参考网址:https://dev.mysql.com/downloads/repo/yum/
wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
如果linux不支持 wget安装wget :
yum -y install wget
会在当前文件里面下载文件
安装mysql 的yum仓库
yum -y install mysql57-community-release-el7-11.noarch.rpm
查看yum 仓库里面的mysql软件:
我们在这里只需要安装服务端的MySQL就行。
安装mysql:
yum -y install mysql-community-server.x86_64
下面是手动安装mysql所需要的所有依赖:
自动安装mysql的目录位置:
1、数据库目录
/var/lib/mysql/(安装完成之后为空,启动mysql服务时会写入一些文件待会启动的时候可以查看一下)
2、配置文件
/usr/share/mysql(mysql.server命令及配置文件)mysql5.5之后的默认安装路径,mysql5.5之前的是/usr/local/mysql
下面是配置文件这里就不详细介绍(经常不用)
[root@centos mysql]# ll
总用量 1932
drwxr-xr-x 2 root root 24 9月 12 12:15 bulgarian
drwxr-xr-x. 2 root root 4096 9月 12 12:15 charsets
drwxr-xr-x. 2 root root 24 9月 12 12:15 czech
drwxr-xr-x. 2 root root 24 9月 12 12:15 danish
-rw-r--r-- 1 root root 25575 6月 22 22:13 dictionary.txt
drwxr-xr-x. 2 root root 24 9月 12 12:15 dutch
drwxr-xr-x. 2 root root 24 9月 12 12:15 english
-rw-r--r-- 1 root root 525548 6月 22 22:13 errmsg-utf8.txt
drwxr-xr-x. 2 root root 24 9月 12 12:15 estonian
-rw-r--r-- 1 root root 927189 6月 22 22:24 fill_help_tables.sql
drwxr-xr-x. 2 root root 24 9月 12 12:15 french
drwxr-xr-x. 2 root root 24 9月 12 12:15 german
drwxr-xr-x. 2 root root 24 9月 12 12:15 greek
drwxr-xr-x. 2 root root 24 9月 12 12:15 hungarian
-rw-r--r-- 1 root root 3999 6月 22 22:13 innodb_memcached_config.sql
-rw-r--r-- 1 root root 1812 6月 22 22:35 install_rewriter.sql
drwxr-xr-x. 2 root root 24 9月 12 12:15 italian
drwxr-xr-x. 2 root root 24 9月 12 12:15 japanese
drwxr-xr-x. 2 root root 24 9月 12 12:15 korean
-rw-r--r-- 1 root root 773 6月 22 22:13 magic
-rw-r--r-- 1 root root 844 6月 22 22:35 mysql-log-rotate
-rw-r--r-- 1 root root 1760 6月 22 22:13 mysql_security_commands.sql
-rw-r--r-- 1 root root 287110 6月 22 22:13 mysql_sys_schema.sql
-rw-r--r-- 1 root root 811 6月 22 22:13 mysql_system_tables_data.sql
-rw-r--r-- 1 root root 154624 6月 22 22:13 mysql_system_tables.sql
-rw-r--r-- 1 root root 10410 6月 22 22:13 mysql_test_data_timezone.sql
drwxr-xr-x. 2 root root 24 9月 12 12:15 norwegian
drwxr-xr-x. 2 root root 24 9月 12 12:15 norwegian-ny
drwxr-xr-x. 2 root root 24 9月 12 12:15 polish
drwxr-xr-x. 2 root root 24 9月 12 12:15 portuguese
drwxr-xr-x. 2 root root 24 9月 12 12:15 romanian
drwxr-xr-x. 2 root root 24 9月 12 12:15 russian
drwxr-xr-x. 2 root root 24 9月 12 12:15 serbian
drwxr-xr-x. 2 root root 24 9月 12 12:15 slovak
drwxr-xr-x. 2 root root 24 9月 12 12:15 spanish
drwxr-xr-x. 2 root root 24 9月 12 12:15 swedish
drwxr-xr-x. 2 root root 24 9月 12 12:15 ukrainian
-rw-r--r-- 1 root root 834 6月 22 22:35 uninstall_rewriter.sql
3、相关命令
/usr/bin(mysqladmin mysqldump等命令)
下面是mysql的操作命令,如果不配置环境变量就必须切换到该路径下执行命令(这里就不详细介绍每条命令作用,经常用的是mysql登录mysql,和mysqladmin修改数据库的密码):
[root@centos bin]# ll|grep mysql
-rwxr-xr-x 1 root root 11137352 6月 22 22:36 mysql
-rwxr-xr-x 1 root root 9880512 6月 22 22:36 mysqladmin
-rwxr-xr-x 1 root root 11876736 6月 22 22:36 mysqlbinlog
-rwxr-xr-x 1 root root 10173912 6月 22 22:36 mysqlcheck
-rwxr-xr-x 1 root root 8772824 6月 22 22:36 mysql_config_editor
-rwxr-xr-x 1 root root 3305 6月 22 22:35 mysqld_pre_systemd
-rwxr-xr-x 1 root root 10244584 6月 22 22:36 mysqldump
-rwxr-xr-x 1 root root 7464 6月 22 22:35 mysqldumpslow
-rwxr-xr-x 1 root root 9866496 6月 22 22:36 mysqlimport
-rwxr-xr-x 1 root root 11252160 6月 22 22:36 mysql_install_db
-rwxr-xr-x 1 root root 5670912 6月 22 22:36 mysql_plugin
-rwxr-xr-x 1 root root 19436080 6月 22 22:37 mysqlpump
-rwxr-xr-x 1 root root 9817488 6月 22 22:36 mysql_secure_installation
-rwxr-xr-x 1 root root 9825120 6月 22 22:36 mysqlshow
-rwxr-xr-x 1 root root 9936136 6月 22 22:36 mysqlslap
-rwxr-xr-x 1 root root 5980688 6月 22 22:35 mysql_ssl_rsa_setup
-rwxr-xr-x 1 root root 5192320 6月 22 22:35 mysql_tzinfo_to_sql
-rwxr-xr-x 1 root root 13400992 6月 22 22:36 mysql_upgrade
4、启动脚本
/etc/rc.d/init.d/(启动脚本文件mysql的目录)
5、日志文件:
/var/log/mysqld.log(里面包含安装和访问客户端访问服务端的所有信息,以及随机生成的mysql的密码,包括错误信息自己可以看一下。刚安装之后,大小为0,开启服务时自动写入随机密码)
三、开启mysql服务和修改mysql的登录密码
启动mysql服务:这里centos6和centos7的命令不一样,请注意一下,这是centos7的开启服务。
systemctl start mysqld#启动mysql服务
systemctl status mysqld #查看mysql服务的状态
启动成功的标志(注意红框的部分):
我们来看下日志文件的变化:
我们来看下日志的内容(里面是操作日志,存储的东西就不需要介绍了,如果安装错误就从这里面查找,具体问题(错误),针对性解决,日志内容不会覆盖,只会会追加(在一定的期限内)):
[root@centos log]# cat mysqld.log
2017-09-12T04:29:45.023022Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-09-12T04:29:45.362706Z 0 [Warning] InnoDB: New log files created, LSN=45790
2017-09-12T04:29:45.432389Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2017-09-12T04:29:45.504278Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 012ff463-9773-11e7-ad44-000c29547286.
2017-09-12T04:29:45.505692Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2017-09-12T04:29:45.507287Z 1 [Note] A temporary password is generated for root@localhost: o(hh_qPgz2pM
2017-09-12T04:29:50.183095Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-09-12T04:29:50.188108Z 0 [Note] /usr/sbin/mysqld (mysqld 5.7.19) starting as process 5328 ...
2017-09-12T04:29:50.295539Z 0 [Note] InnoDB: PUNCH HOLE support available
2017-09-12T04:29:50.295635Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2017-09-12T04:29:50.295644Z 0 [Note] InnoDB: Uses event mutexes
2017-09-12T04:29:50.295652Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
2017-09-12T04:29:50.295661Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.3
2017-09-12T04:29:50.295666Z 0 [Note] InnoDB: Using Linux native AIO
2017-09-12T04:29:50.300155Z 0 [Note] InnoDB: Number of pools: 1
2017-09-12T04:29:50.300394Z 0 [Note] InnoDB: Using CPU crc32 instructions
2017-09-12T04:29:50.302316Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
2017-09-12T04:29:50.313221Z 0 [Note] InnoDB: Completed initialization of buffer pool
2017-09-12T04:29:50.319164Z 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
2017-09-12T04:29:50.367260Z 0 [Note] InnoDB: Highest supported file format is Barracuda.
2017-09-12T04:29:50.387363Z 0 [Note] InnoDB: Creating shared tablespace for temporary tables
2017-09-12T04:29:50.387463Z 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
2017-09-12T04:29:50.428557Z 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
2017-09-12T04:29:50.429379Z 0 [Note] InnoDB: 96 redo rollback segment(s) found. 96 redo rollback segment(s) are active.
2017-09-12T04:29:50.429396Z 0 [Note] InnoDB: 32 non-redo rollback segment(s) are active.
2017-09-12T04:29:50.429925Z 0 [Note] InnoDB: Waiting for purge to start
2017-09-12T04:29:50.480692Z 0 [Note] InnoDB: 5.7.19 started; log sequence number 2539461
2017-09-12T04:29:50.481565Z 0 [Note] Plugin 'FEDERATED' is disabled.
2017-09-12T04:29:50.491951Z 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
2017-09-12T04:29:50.506137Z 0 [Note] InnoDB: Buffer pool(s) load completed at 170912 12:29:50
2017-09-12T04:29:50.534278Z 0 [Note] Found ca.pem, server-cert.pem and server-key.pem in data directory. Trying to enable SSL support using them.
2017-09-12T04:29:50.534721Z 0 [Warning] CA certificate ca.pem is self signed.
2017-09-12T04:29:50.558741Z 0 [Note] Server hostname (bind-address): '*'; port: 3306
2017-09-12T04:29:50.558916Z 0 [Note] IPv6 is available.
2017-09-12T04:29:50.558942Z 0 [Note] - '::' resolves to '::';
2017-09-12T04:29:50.558972Z 0 [Note] Server socket created on IP: '::'.
2017-09-12T04:29:50.574453Z 0 [Note] Event Scheduler: Loaded 0 events
2017-09-12T04:29:50.574727Z 0 [Note] /usr/sbin/mysqld: ready for connections.
Version: '5.7.19' socket: '/var/lib/mysql/mysql.sock' port: 3306 MySQL Community Server (GPL)
2017-09-12T04:29:50.574771Z 0 [Note] Executing 'SELECT * FROM INFORMATION_SCHEMA.TABLES;' to get a list of tables using the deprecated partition engine. You may use the startup option '--disable-partition-engine-check' to skip this check.
2017-09-12T04:29:50.574789Z 0 [Note] Beginning of list of non-natively partitioned tables
2017-09-12T04:29:50.589880Z 0 [Note] End of list of non-natively partitioned tables
查看随机产生的mysql的密码
grep "password" /var/log/mysqld.log
或者grep "temporary password" /var/log/mysqld.log
注意:这里随机产生的密码连标点符号和特殊字符都是密码。 修改随机密码(可以登录mysql里面修改,或者直接在外面修改,这里是在外面修改):
切换到mysql的命令路径:
cd /usr/bin
./mysqladmin -u root -p password
在linux里面密码的输入都不会显示,输入完后直接回车。
注意: 在新设的密码里必须包含大小写字母、数字、和特殊字符。如果不包含就不会修改成功。
登录mysql(切换到命令目录下)
mysql -u root -p
四、扩展
修改配置文件,可以在该用户下任何位置都可以执行mysql的任何命令:
修改用户的环境变量的配置文件(有全局环境变量配置文件和用户环境变量配置文件,并且有多个,修改任何一个都行),我已习惯修改.bashrc
有些时候修改不了,就应该是用户问题
vi ~/.bashrc
加入下面的内容
export MYSQL_HOME=/usr
export PATH=$PATH:$MYSQL_HOME/bin
加载资源
source ~/.bashrc
你就可以在该用户的任何文件下都可以执行mysql的所有命令。
五、结语
我查阅了很多资料,说在安装mysql之后再执行一些初始化的命令来生成随机初始化密码,但是我完全按照他们的来做,最后出现各种各样的文件权限问题。所以建议别使用那些命令,直接开启服务就OK。还有mysql的yum仓库最好去mysql的官网下载。