Linux下安装配置MySQL
系统:Ubuntu16.04LTS
1\官网下载mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz
2\建立工作组:
$su #groupaddmysql #useradd-r-gmysqlmysql
3\创建目录
#mkdir/usr/local/mysql #mkdir/usr/local/mysql/data
4\解压mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz,并拷贝至/usr/local/mysql
#tar-zxvfmysql-5.7.18-linux-glibc2.5-x86_64.tar.gz #cp-r/home/jieyamulu/mysql-5.7.18-linux-glibc2.5-x86_64/*/usr/local/mysql
5\修改mysql用户对mysql以下及其子文件夹以下文件的权限,修改后可使用ll查看权限
root@Ice-***:/usr/local#chown-Rmysql:mysqlmysql root@Ice-***:/usr/local#ll 总用量44 drwxr-xr-x11rootroot40965月1907:39./ drwxr-xr-x11rootroot40962月1604:30../ drwxr-xr-x2rootroot40962月1604:19bin/ drwxr-xr-x2rootroot40962月1604:19etc/ drwxr-xr-x2rootroot40962月1604:19games/ drwxr-xr-x2rootroot40962月1604:19include/ drwxr-xr-x4rootroot40962月1604:23lib/ lrwxrwxrwx1rootroot93月2914:11man->share/man/ drwxr-xr-x10mysqlmysql40965月1907:48mysql/ drwxr-xr-x2rootroot40962月1604:19sbin/ drwxr-xr-x8rootroot40962月1604:34share/ drwxr-xr-x2rootroot40962月1604:19src/ root@Ice-***:/usr/local#cdmysql/ root@Ice-***:/usr/local/mysql#ll 总用量64 drwxr-xr-x10mysqlmysql40965月1907:48./ drwxr-xr-x11rootroot40965月1907:39../ drwxr-xr-x2mysqlmysql40965月1907:48bin/ -rw-r--r--1mysqlmysql179875月1907:48COPYING drwxr-xr-x2mysqlmysql40965月1907:41data/ drwxr-xr-x2mysqlmysql40965月1907:48docs/ drwxr-xr-x3mysqlmysql40965月1907:48include/ drwxr-xr-x5mysqlmysql40965月1907:48lib/ drwxr-xr-x4mysqlmysql40965月1907:48man/ -rw-r--r--1mysqlmysql24785月1907:48README drwxr-xr-x28mysqlmysql40965月1907:48share/ drwxr-xr-x2mysqlmysql40965月1907:48support-files/
6\修改(或新建)/etc/my.cnf配置文件
root@Ice-***:/usr/local/mysql#vim/etc/my.cnf [mysqld]basedir=/usr/local/mysql/ datadir=/usr/local/mysql/data :wq
7\最关键的初始化
#cd/usr/local/mysql/ root@Ice-***:/usr/local/mysql#./bin/mysqld--user=mysql--basedir=/usr/local/mysql--datadir=/usr/local/mysql/data--initialize 2017-05-19T00:15:46.529420Z0[Warning]TIMESTAMPwithimplicitDEFAULTvalueisdeprecated.Pleaseuse--explicit_defaults_for_timestampserveroption(seedocumentationformoredetails). 2017-05-19T00:15:47.066125Z0[Warning]InnoDB:Newlogfilescreated,LSN=45790 2017-05-19T00:15:47.213711Z0[Warning]InnoDB:Creatingforeignkeyconstraintsystemtables. 2017-05-19T00:15:47.286951Z0[Warning]NoexistingUUIDhasbeenfound,soweassumethatthisisthefirsttimethatthisserverhasbeenstarted.GeneratinganewUUID:4e958344-3c28-11e7-8334-c8d3ffd2db82. 2017-05-19T00:15:47.292857Z0[Warning]Gtidtableisnotreadytobeused.Table'mysql.gtid_executed'cannotbeopened. 2017-05-19T00:15:47.294758Z1[Note]Atemporarypasswordisgeneratedforroot@localhost:YjaotQk*2ew4
初始密码要记住
这里可能会出现很多问题,比如:
InstallingMySQLsystemtables..../bin/mysqld:errorwhileloadingsharedlibraries:libaio.so.1:cannotopensharedobjectfile:Nosuchfileordirectory
缺少依赖关系包
解决方法:sudoapt-getinstalllibaio-dev
还有可能是之前步骤不对导致了操作data文件的权限不够等等.按照步骤来,缺少什么装什么(有提示),到了这里应该就初始化成功了,那几个Warning,值得注意的是Gtidtableisnotreadytobeused.Table'mysql.gtid_executed'cannotbeopened.有时间可以查下是怎么回事,这条忽略也没影响.
8\不要着急启动,现在还启动不了.执行代码,将mysql下除了data文件夹外的文件变为root权限
root@Ice-***:/usr/local/mysql#chown-Rroot. root@Ice-***:/usr/local/mysql#chown-Rmysqldata
9\启动
root@Ice-***:/usr/local/mysql#bin/mysqld_safe--user=mysql&
敲回车
root@Ice-***:/usr/local/mysql#/usr/local/mysql/bin/mysql-uroot-p Enterpassword: WelcometotheMySQLmonitor.Commandsendwith;or\g. YourMySQLconnectionidis3 Serverversion:5.7.18 Copyright(c)2000,2017,Oracleand/oritsaffiliates.Allrightsreserved. OracleisaregisteredtrademarkofOracleCorporationand/orits affiliates.Othernamesmaybetrademarksoftheirrespective owners. Type'help;'or'\h'forhelp.Type'\c'toclearthecurrentinputstatement. mysql>showdatabases; ERROR1820(HY000):YoumustresetyourpasswordusingALTERUSERstatementbeforeexecutingthisstatement. mysql>
10\重置密码
mysql>SETPASSWORD=PASSWORD('newpasswd'); QueryOK,0rowsaffected,1warning(0.00sec) mysql>showdatabases; +--------------------+ |Database| +--------------------+ |information_schema| |mysql| |performance_schema| |sys| +--------------------+ 4rowsinset(0.00sec) mysql>quit Bye
11\设置开机启动
root@Ice-***:/usr/local/mysql#cp/usr/local/mysql/support-files/mysql.server/etc/init.d/mysqld root@Ice-***:/usr/local/mysql#chmod755/etc/init.d/mysqld
12\安装mysql-server和mysql-client
root@Ice-***:~#apt-getinstallmysql-server root@Ice-***:~#apt-getinstallmysql-client root@Ice-***:~#apt-getinstalllibmysqlclient-dev E:Sub-process/usr/bin/dpkgreturnedanerrorcode(1)
解决办法:
1.$sudomv/var/lib/dpkg/info/var/lib/dpkg/info_old//现将info文件夹更名
2.$sudomkdir/var/lib/dpkg/info//再新建一个新的info文件夹
3.$sudoapt-getupdate,
$apt-get-finstall//修复依赖树
4.$sudomv/var/lib/dpkg/info/*/var/lib/dpkg/info_old//执行完上一步操作后会在新的info文件夹下生成一些文件,现将这些文件全部移到info_old文件夹下
5.$sudorm-rf/var/lib/dpkg/info//把自己新建的info文件夹删掉
6.$sudomv/var/lib/dpkg/info_old/var/lib/dpkg/info//把以前的info文件夹重新改回名字
最后,如果是Ubuntu系统可能无法向表中插入汉字,无法从表中查询汉字..
解决办法:
关闭数据库服务
servicemysqlstop ~$sudovim/etc/mysql/mysql.conf.d/mysqld.cnf
在[mysqld]下添加一行character_set_server=utf8
配置文件选段:
[mysqld] # #*BasicSettings # user=mysql pid-file=/var/run/mysqld/mysqld.pid socket=/var/run/mysqld/mysqld.sock port=3306 basedir=/usr datadir=/var/lib/mysql tmpdir=/tmp lc-messages-dir=/usr/share/mysql character_set_server=utf8就是这一行,原来是没有的,要手动添加! skip-external-locking #
将上述文件拷贝至/etc/mysql/my.cnf
~$sudocp/etc/mysql/mysql.conf.d/mysqld.cnf/etc/mysql/my.cnf
重启数据库服务
~$/etc/init.d/mysqlrestart
如果查看字符集能得到如下结果就说明成功了.
mysql>showvariableslike'collation_%'; +----------------------+-----------------+ |Variable_name|Value| +----------------------+-----------------+ |collation_connection|utf8_general_ci| |collation_database|utf8_general_ci| |collation_server|utf8_general_ci| +----------------------+-----------------+ 3rowsinset(0.00sec) mysql>showvariableslike'character_set_%'; +--------------------------+----------------------------+ |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/| +--------------------------+----------------------------+ 8rowsinset(0.01sec)